File: map.c

package info (click to toggle)
grass 6.0.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 38,764 kB
  • ctags: 31,167
  • sloc: ansic: 320,650; tcl: 25,669; cpp: 10,098; sh: 9,695; makefile: 4,714; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (324 lines) | stat: -rw-r--r-- 6,570 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include "list.h"
#include "mapcalc.h"
#include "map.h"

typedef MAP *(*m_func)(void);
typedef MAP *(*m_func_m)(void *m0);
typedef MAP *(*m_func_mm)(void *m0, void *m1);

typedef struct Mapfunc
{
  char *name;
  void *func;
  char *proto;
}MAPFUNC;

static MAP *testmap (MAP *m);
static MAP *test2map (MAP *m, MAP *n);
static MAP *map_op_func_plus (MAP *m, MAP *n);
static void find_maps (void);
void init_map (void);
void showmap (SYMBOL *map);
void setmap (SYMBOL *var, SYMBOL *map);
SYMBOL *mkmapvar (SYMBOL *var, SYMBOL *map);
SYMBOL *mapfunc (SYMBOL *func, SYMBOL *arglist);
SYMBOL *mapop (int op, SYMBOL *map1, SYMBOL *map2);

static MAPFUNC mf[] =
{
  { "testmap", testmap, "m" },
  { "test2map", test2map, "mm" },
  { "map_op_func_+", map_op_func_plus, "mm" },
  { NULL, NULL, NULL }
};

/*************************************************************************
 * This function represents a built-in map function.
 * Should probably go to another file
 */
static MAP *
testmap (MAP *m)
{
  char namebuf[128];
  /*
   * The map name always exists, as it represents data on disk, but
   * might be a temporary name, when it should not be displayed
   * (should it?).
   */
  printf ("Performing 1 arg map function on map %s\n", m->name);
  sprintf (namebuf, "t-%s", m->name);
  m = (MAP *)listitem (sizeof (MAP));
  m->name = strdup (namebuf);
  return m;
}

static MAP *
test2map (MAP *m, MAP *n)
{
  char namebuf[128];

  printf ("Performing 2 arg map function on maps %s and %s\n", m->name, n->name);
  sprintf (namebuf, "%s.%s", m->name, n->name);
  m = (MAP *)listitem (sizeof (MAP));
  m->name = strdup (namebuf);
  return m;
}

static MAP *
map_op_func_plus (MAP *m, MAP *n)
{
  char namebuf[128];

  printf ("Performing map %s + %s\n", m->name, n->name);
  sprintf (namebuf, "%s.%s", m->name, n->name);
  m = (MAP *)listitem (sizeof (MAP));
  m->name = strdup (namebuf);
  return m;
}

/*
 * end of move to other file
 ***********************************************************************/

static void
find_maps (void)
{
  char *gisdbase, *location, *mapset;
  char basepath[4096], subdirpath[4096], path[4096];
  DIR *dir, *subdir;
  struct dirent *ent, *subent;

  gisdbase = getenv ("GISDBASE");
  if (!gisdbase)
    gisdbase = ".";

  location = getenv ("LOCATION_NAME");
  if (!location)
    location = ".";

  mapset = getenv ("MAPSET");
  if (!mapset)
    mapset = ".";

  /*
   * Now, if I'm not in grass, I can simulate the existence of a vector
   * map creating a directory vector with one subdirectory for each `map'
   * having a file `head'
   */

  sprintf (basepath, "%s/%s/%s/vector", gisdbase, location, mapset);
  dir = opendir (basepath);
  if (!dir)
    return;

  while ((ent = readdir (dir)) != NULL)
  {
    struct stat buf;

    if (!strcmp (ent->d_name, ".") || !strcmp (ent->d_name, ".."))
      continue;

    strcpy (path, basepath);
    strcat (path, "/");
    strcat (path, ent->d_name);

    if (stat (path, &buf))
      continue;
    if (S_ISDIR (buf.st_mode))
    {
      strcpy (subdirpath, path);
      subdir = opendir (subdirpath);
      if (!subdir)
	continue;
      while ((subent = readdir (subdir)) != NULL)
      {
	if (!strcmp (subent->d_name, ".") || !strcmp (subent->d_name, ".."))
	  continue;
	if (!strcmp (subent->d_name, "head"))
	{
	  MAP *map;
	  SYMBOL *sym;

	  map = (MAP *)listitem (sizeof (MAP));
	  map->name = strdup (ent->d_name);
	  map->refcnt++;
	  sym = putsym (map->name);
	  sym->type = sym->itype = st_map;
	  sym->v.p = map;
	}
      }
      closedir (subdir);
    }
  }
  closedir (dir);
}

void
init_map (void)
{
  SYMBOL *sym;
  int i;

  for (i = 0; mf[i].name; i++)
  {
    sym = putsym (mf[i].name);
    sym->type = sym->itype = st_mfunc;
    sym->v.p = mf[i].func;
    sym->proto = mf[i].proto;
    sym->rettype = st_map;
  }

  find_maps ();
}

void
printmap (SYMBOL *sym)
{
  MAP *map;

  map = (MAP *)sym->v.p;
  if (map->name)
    printf ("\t%s\n", map->name);
}

void
showmap (SYMBOL *sym)
{
  MAP *map;

  map = (MAP *)sym->v.p;
  printmap (sym);
  if (--map->refcnt > 0)
    sym->v.p = NULL;
  freesym (sym);
}

MAP *
freemap (MAP *map)
{
  if (map)
  {
    if (map->refcnt > 0)
      return map;
    if (map->name)
      free (map->name);
    if (map->mapinfo)
      free (map->mapinfo);		/* call grass to handle map */
    free (map);
  }
  return NULL;
}

void
setmap (SYMBOL *var, SYMBOL *map)
{
  SYMBOL *sym;

  if (var->name)
  {
    sym = getsym (var->name);
    if (sym)
    {
      sym->v.p = freemap (sym->v.p);
      sym->v.p = map->v.p;
    }
  }

  freemap (var->v.p);
  var->v.p = NULL;
  freesym (var);

  printmap (map);
  map->v.p = NULL;
  freesym (map);
}

SYMBOL *
mkmapvar (SYMBOL *var, SYMBOL *map)
{
  var->type = var->itype = st_map;
  var->name = var->v.p;
  var->v.p = map->v.p;
  map->v.p = NULL;
  freesym (map);

  symtab = (SYMBOL *)listadd ((LIST *)symtab, (LIST *)var, cmpsymsym);

  printmap (var);

  return var;
}

SYMBOL *
mapfunc (SYMBOL *func, SYMBOL *arglist)
{
  SYMBOL *sym;
  MAP *res = NULL;
  int argc = -1;

  if (!func || !func->v.p || func->type != st_mfunc)
  {
    parseerror = 1;
    printf ("Can't call bad map-function\n");
  }
  else
    argc = listcnt ((LIST *)arglist);

  if (argc == 0 && (!func->proto || !*func->proto))
    res = (*(m_func)func->v.p)();
  else if (argc == 1 && !strcmp (func->proto, "m"))
    res = (*(m_func_m)func->v.p)(arglist->v.p);
  else if (argc == 2 && !strcmp (func->proto, "mm"))
    res = (*(m_func_mm)func->v.p)(arglist->v.p,
				  arglist->next->v.p);
  else
  {
    printf ("Bad arguments to mapfunc %s (argc = %d)\n", func->name, argc);
    parseerror = 1;
  }

  listdelall ((LIST *)func, freesym);
  listdelall ((LIST *)arglist, freesym);

  sym = (SYMBOL *)listitem (sizeof (SYMBOL));
  sym->type = st_map;
  sym->v.p = res;

  return sym;
}

SYMBOL *
mapop (int op, SYMBOL *map1, SYMBOL *map2)
{
  SYMBOL *func, *arglist, *res = NULL;
  char buf[32];

  sprintf (buf, "map_op_func_%c", op);

  func = getsym (buf);
  if (!func)
  {
    printf ("No function defined to perform map %c map\n", op);
    parseerror = 1;
  }
  else
  {
    res = (SYMBOL *)listitem (sizeof (SYMBOL));
    symcpy (res, func);
    res->next = NULL;
    func = res;
    arglist = (SYMBOL *)listapp (NULL, (LIST *)map1);
    arglist = (SYMBOL *)listapp ((LIST *)arglist, (LIST *)map2);

    res = mapfunc (func, arglist);
  }

  /* free map1/map2 ?    only if they have no name? */

  return res;
}