GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
find_file.c
Go to the documentation of this file.
1 
16 #include <string.h>
17 #include <unistd.h>
18 #include <grass/gis.h>
19 #include <grass/glocale.h>
20 
21 static char *find_file(int misc,
22  const char *dir,
23  const char *element,
24  const char *name, const char *mapset)
25 {
26  char path[GPATH_MAX];
27  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
28  const char *pname, *pmapset;
29  int n;
30 
31  if (*name == 0)
32  return NULL;
33  *path = 0;
34 
35  /*
36  * if name is in the fully qualified format, split it into
37  * name, mapset (overrides what was in mapset)
38  */
39  if (G__name_is_fully_qualified(name, xname, xmapset)) {
40  pname = xname;
41  pmapset = xmapset;
42  }
43  else {
44  pname = name;
45  pmapset = mapset;
46  }
47 
48  /*
49  * reject illegal names and mapsets
50  */
51  if (G_legal_filename(pname) == -1)
52  return NULL;
53 
54  if (pmapset && *pmapset && G_legal_filename(pmapset) == -1)
55  return NULL;
56 
57  /*
58  * if no specific mapset is to be searched
59  * then search all mapsets in the mapset search list
60  */
61  if (pmapset == NULL || *pmapset == 0) {
62  int cnt = 0;
63  const char *pselmapset = NULL;
64 
65  for (n = 0; (pmapset = G__mapset_name(n)); n++) {
66  if (misc)
67  G__file_name_misc(path, dir, element, pname, pmapset);
68  else
69  G__file_name(path, element, pname, pmapset);
70  if (access(path, 0) == 0) {
71  if (!pselmapset)
72  pselmapset = pmapset;
73  else
74  G_warning(_("'%s/%s' was found in more mapsets (also found in <%s>)"),
75  element, pname, pmapset);
76  cnt++;
77  }
78  }
79  if (cnt > 0) {
80  /* If the same name exists in more mapsets and print a warning */
81  if (cnt > 1)
82  G_warning(_("Using <%s@%s>"),
83  pname, pselmapset);
84 
85  return (char *)pselmapset;
86  }
87  }
88  /*
89  * otherwise just look for the file in the specified mapset.
90  * since the name may have been qualified, mapset may point
91  * to the xmapset, so we must should it to
92  * permanent storage via G_store().
93  */
94  else {
95  if (misc)
96  G__file_name_misc(path, dir, element, pname, pmapset);
97  else
98  G__file_name(path, element, pname, pmapset);
99 
100  if (access(path, 0) == 0)
101  return G_store(pmapset);
102  }
103 
104  return NULL;
105 }
106 
107 
108 
109 static char *find_file1(int misc,
110  const char *dir,
111  const char *element, char *name, const char *mapset)
112 {
113  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
114  const char *pname, *pmapset;
115  char *mp;
116 
117  if (G__name_is_fully_qualified(name, xname, xmapset)) {
118  pname = xname;
119  pmapset = xmapset;
120  }
121  else {
122  pname = name;
123  pmapset = mapset;
124  }
125 
126  mp = find_file(misc, dir, element, pname, pmapset);
127 
128  if (mp && name != pname)
129  strcpy(name, pname);
130 
131  return mp;
132 }
133 
159 char *G_find_file(const char *element, char *name, const char *mapset)
160 {
161  return find_file1(0, NULL, element, name, mapset);
162 }
163 
164 char *G_find_file_misc(const char *dir,
165  const char *element, char *name, const char *mapset)
166 {
167  return find_file1(1, dir, element, name, mapset);
168 }
169 
170 
191 char *G_find_file2(const char *element, const char *name, const char *mapset)
192 {
193  return find_file(0, NULL, element, name, mapset);
194 }
195 
196 char *G_find_file2_misc(const char *dir,
197  const char *element,
198  const char *name, const char *mapset)
199 {
200  return find_file(1, dir, element, name, mapset);
201 }
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
char xmapset[512]
Definition: g3dcats.c:89
char * G__file_name_misc(char *path, const char *dir, const char *element, const char *name, const char *mapset)
Definition: file_name.c:70
string name
Definition: render.py:1314
char * G_find_file2_misc(const char *dir, const char *element, const char *name, const char *mapset)
Definition: find_file.c:196
char * G__file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:33
char * G__mapset_name(int n)
Get name of the n&#39;th mapset from the mapset_name[] list.
Definition: mapset_nme.c:36
return NULL
Definition: dbfopen.c:1394
char * G_find_file2(const char *element, const char *name, const char *mapset)
searches for a file from the mapset search list or in a specified mapset. (look but don&#39;t touch) retu...
Definition: find_file.c:191
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
char * G_find_file_misc(const char *dir, const char *element, char *name, const char *mapset)
Definition: find_file.c:164
char * G_find_file(const char *element, char *name, const char *mapset)
searches for a file from the mapset search list or in a specified mapset. returns the mapset name whe...
Definition: find_file.c:159
char xname[512]
Definition: g3dcats.c:89
int n
Definition: dataquad.c:291
int G__name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:57