original 1.0.1 release
[xwelltris.git] / src / commonfuncs.cxx
blob3cfd66bf1527f424d773d0d55b9c67f155051d74
1 // docm_prefix(///)
2 /****************************************************************************
3 * Copyright (C) 2002 by Leo Khramov
4 * email: leo@xnc.dubna.su
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 ****************************************************************************/
16 // $Id: commonfuncs.cxx,v 1.3 2003/02/27 08:27:51 leo Exp $
18 #include "commonfuncs.h"
21 //===========================================================================
22 /// global find_full_path_for_file(char* fname, char* ret_full_pathname, FMode mode)
23 /// find file in global and local dirs and return full pathname
24 /// tags common_funcs
25 bool find_full_path_for_file(char* name, char* ret_full_pathname, FMode mode)
27 #ifndef WIN32
28 int fd;
29 char *home=getenv("HOME");
30 mode_t fmode=O_RDONLY;
32 switch(mode)
34 case ReadOnly:
35 fmode=O_RDONLY;
36 break;
37 case ReadWrite:
38 fmode=O_RDWR;
39 break;
42 do
44 sprintf(ret_full_pathname,"%s/%s",GLOBAL_SEARCH,name); //First check in GLOBAL_SEARCH dir
45 fd=open(ret_full_pathname,fmode);
46 if(fd>0)
47 break;
49 sprintf(ret_full_pathname,"%s/%s/%s",home,LOCAL_SEARCH,name); //Then in LOCAL one
50 fd=open(ret_full_pathname,fmode);
51 if(fd>0)
52 break;
54 sprintf(ret_full_pathname,"./%s",name); //Then in current dir
55 fd=open(ret_full_pathname,fmode);
56 if(fd>0)
57 break;
59 sprintf(ret_full_pathname,"./data/%s",name); //And finally in ./data
60 fd=open(ret_full_pathname,fmode);
61 if(fd>0)
62 break;
64 fprintf(stderr,"ERROR: Can't find file %s in:\n\t./\n\t./data\n\t%s/%s\n\t%s\n",
65 name,home,LOCAL_SEARCH,GLOBAL_SEARCH);
66 return false;
67 } while(0);
69 close(fd);
70 return true;
71 #else
72 FILE *fp;
73 sprintf(ret_full_pathname,"data\\%s",name); //And finally in ./data
74 fp=fopen(ret_full_pathname, "r");
75 if(fp)
77 fclose(fp);
78 return true;
80 return false;
81 #endif