/* * res * * Access to resources * * © 1993-1998 Straylight */ /*----- Licensing note ----------------------------------------------------* * * This file is part of Straylight's Steel library. * * Steel is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * Steel is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Steel. If not, write to the Free Software Foundation, * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include "os.h" #include "swiv.h" #include "swis.h" #include "wimpt.h" #include "res.h" #include "buffer.h" static char res__prefix[40]; /* * * void res_init(char *progname) * * Use * Sets up res to use a certain path for resources. Mainly for * compatibility with older applications. * * Parameters * char *progname == the program name (sets up path as ) */ void res_init(char *progname) { sprintf(res__prefix,"<%s$Dir>",progname); } /* * void res_setPrefix(char *prefix) * * Use * Sets up a full prefix for resources. This means you can keep resources * in separate directories. * * Parameters * char *prefix == the prefix */ void res_setPrefix(char *prefix) { strcpy(res__prefix,prefix); } /* * BOOL res_fileExists(char *name) * * Use * Informs the caller if a given file exists * * Parameters * char *name == the name of the file * * Returns * TRUE if the file really does exist */ BOOL res_fileExists(char *name) { os_filestr f; f.action=17; f.name=name; if (os_file(&f)) return (FALSE); return (!!f.action); } /* * BOOL res__trySuffix(char *name) * * Use * Tries to find a file with a possible WIMP-style mode suffix (24,23,22 * etc.) If it did, it updates the name in the caller's buffer, and * returns TRUE. Otherwise, the name is preserved, and it returns FALSE. */ static BOOL res__trySuffix(char *name) { char *suff; char *end; if (wimpt_getVersion()>=300) { end=name+strlen(name); wimpt_noerr(wimp_readsysinfo(2,(int *)&suff)); strcat(name,suff); if (res_fileExists(name)) return (TRUE); *end=0; } if (res_fileExists(name)) return (TRUE); return (FALSE); } /* * char *res__findCountryName(void) * * Use * Returns the name of the current country. If the current country is * unknown, try the UK. */ static char *res__findCountryName(void) { char *buffer=buffer_find(); int country=_swi(OS_Byte,_inr(0,2)+_return(1),240,0,255); int len; if (_swi(OS_ServiceCall, _inr(1,4)+_out(5)+_return(1), 0x43,2,country,buffer, &len)==1) return ("UK"); else { buffer[len]=0; return (buffer); } } /* * int res_findname(const char *resname,char *buf) * * Use * Returns a full pathname for the resource file as given in the buffer. * This is for compatibility reasons. If I was writing this fresh, I * would return a pointer to an internal static object, but Acorn know * best... * * Some new functionality has been added. It will look for files first in * the directory set up using res_init or res_setPrefix, and then in the * 'Resources' subdirectory. If neither is present, then the name returned * is in the main application directory. * * Also, under RISC OS 3, it will search for files with mode prefixes, so * you can use multiple sprite files for different resolutions. Isn't this * fun! * * Parameters * const char *resname == the leafname of the resource file. * char *buf == where to put the result. * * Returns * TRUE for compatibility with the Acorn version. What good it does, I * don't know. This is in all a very odd routine indeed. */ int res_findname(const char *resname,char *buf) { sprintf(buf,"%s.Resources.%s",res__prefix,resname); if (res__trySuffix(buf)) return (TRUE); sprintf(buf, "%s.Resources.%s.%s", res__prefix, res__findCountryName(), resname); if (res__trySuffix(buf)) return (TRUE); sprintf(buf, "%s.Resources.UK.%s", res__prefix, resname); if (res__trySuffix(buf)) return (TRUE); sprintf(buf,"%s.%s",res__prefix,resname); if (res__trySuffix(buf)) return (TRUE); sprintf(buf,"%s.%s",res__prefix,resname); return (TRUE); } /* * char *res_name(const char *resname) * * Use * Translates the name given as for res_findname and returns a pointer to * the translated string */ char *res_name(const char *resname) { char *buffer=buffer_find(); res_findname(resname,buffer); return (buffer); } /* * FILE *res_openfile(const char *resname,const char *mode) * * Use * Opens a resource file in a given ANSI mode. It does this without the * odd adding on of numbers to the end that the Acorn one does (I hope!) * * Parameters * const char *resname == leafname of file to open * const char *mode == pointer to ANSI mode string * * Returns * A standard ANSI-type FILE pointer. */ FILE *res_openfile(const char *resname,const char *mode) { return (fopen(res_name(resname),mode)); }