GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
file.c
Go to the documentation of this file.
1 
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <grass/gis.h>
26 #include <grass/Vect.h>
27 #include <grass/glocale.h>
28 
36 long dig_ftell(GVFILE *file)
37 {
38  if (file->loaded) /* using memory */
39  return (file->current - file->start);
40 
41  return (ftell(file->file));
42 }
43 
60 int dig_fseek(GVFILE * file, long offset, int whence)
61 {
62  if (file->loaded) { /* using memory */
63  switch (whence) {
64  case SEEK_SET:
65  file->current = file->start + offset;
66  break;
67  case SEEK_CUR:
68  file->current += offset;
69  break;
70  case SEEK_END:
71  file->current = file->start + file->size + offset;
72  break;
73  }
74  return 0;
75  }
76 
77  return (fseek(file->file, offset, whence));
78 }
79 
85 void dig_rewind(GVFILE * file)
86 {
87  if (file->loaded) { /* using memory */
88  file->current = file->start;
89  }
90  else {
91  rewind(file->file);
92  }
93 }
94 
102 int dig_fflush(GVFILE * file)
103 {
104  if (file->loaded) { /* using memory */
105  return 0;
106  }
107  else {
108  return (fflush(file->file));
109  }
110 }
111 
122 size_t dig_fread(void *ptr, size_t size, size_t nmemb, GVFILE *file)
123 {
124  long tot;
125  size_t cnt;
126 
127  if (file->loaded) { /* using memory */
128  if (file->current >= file->end) { /* EOF */
129  return 0;
130  }
131  tot = size * nmemb;
132  cnt = nmemb;
133  if (file->current + tot > file->end) {
134  tot = file->end - file->current;
135  cnt = (int)tot / size;
136  }
137  memcpy(ptr, file->current, tot);
138  file->current += tot;
139  return (cnt);
140  }
141  return (fread(ptr, size, nmemb, file->file));
142 }
143 
154 size_t dig_fwrite(void *ptr, size_t size, size_t nmemb, GVFILE *file)
155 {
156  if (file->loaded) { /* using memory */
157  G_fatal_error(_("Writing to file loaded to memory not supported"));
158  }
159 
160  return fwrite(ptr, size, nmemb, file->file);
161 }
162 
168 void dig_file_init(GVFILE *file)
169 {
170  file->file = NULL;
171  file->start = NULL;
172  file->current = NULL;
173  file->end = NULL;
174  file->size = 0;
175  file->alloc = 0;
176  file->loaded = 0;
177 }
178 
190 int dig_file_load(GVFILE * file)
191 {
192  int ret, mode, load;
193  char *cmode;
194  size_t size;
195  struct stat sbuf;
196 
197  G_debug(2, "dig_file_load ()");
198 
199  if (file->file == NULL) {
200  G_warning(_("Unable to load file to memory, file not open"));
201  return -1;
202  }
203 
204  /* Get mode */
205  mode = GV_MEMORY_NEVER;
206  cmode = G__getenv("GV_MEMORY");
207  if (cmode != NULL) {
208  if (G_strcasecmp(cmode, "ALWAYS") == 0)
209  mode = GV_MEMORY_ALWAYS;
210  else if (G_strcasecmp(cmode, "NEVER") == 0)
211  mode = GV_MEMORY_NEVER;
212  else if (G_strcasecmp(cmode, "AUTO") == 0)
213  mode = GV_MEMORY_AUTO;
214  else
215  G_warning(_("Vector memory mode not supported, using 'AUTO'"));
216  }
217  G_debug(2, " requested mode = %d", mode);
218 
219 
220  fstat(fileno(file->file), &sbuf);
221  size = sbuf.st_size;
222 
223  G_debug(2, " size = %u", size);
224 
225  /* Decide if the file should be loaded */
226  /* TODO: I don't know how to get size of free memory (portability) to decide if load or not for auto */
227  if (mode == GV_MEMORY_AUTO)
228  mode = GV_MEMORY_NEVER;
229  if (mode == GV_MEMORY_ALWAYS)
230  load = 1;
231  else
232  load = 0;
233 
234  if (load) {
235  file->start = G_malloc(size);
236  if (file->start == NULL)
237  return -1;
238 
239  fseek(file->file, 0L, 0);
240  ret = fread(file->start, size, 1, file->file); /* Better to read in smaller portions? */
241  fseek(file->file, 0L, 0); /* reset to the beginning */
242 
243  if (ret <= 0) {
244  G_free(file->start);
245  return -1;
246  }
247 
248  file->alloc = size;
249  file->size = size;
250  file->current = file->start;
251  file->end = file->start + size;
252 
253  file->loaded = 1;
254  G_debug(2, " file was loaded to the memory");
255  return 1;
256  }
257  else {
258  G_debug(2, " file was not loaded to the memory");
259  }
260 
261  return 0;
262 }
263 
269 void dig_file_free(GVFILE * file)
270 {
271  if (file->loaded) {
272  G_free(file->start);
273  file->loaded = 0;
274  file->alloc = 0;
275  }
276 }
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:192
size_t dig_fwrite(void *ptr, size_t size, size_t nmemb, GVFILE *file)
Write GVFILE.
Definition: file.c:154
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
char * G__getenv(const char *name)
Get environment variable.
Definition: env.c:312
void dig_file_free(GVFILE *file)
Free GVFILE.
Definition: file.c:269
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
int dig_fseek(GVFILE *file, long offset, int whence)
Set GVFILE position.
Definition: file.c:60
int stat
Definition: g3dcolor.c:369
int
Definition: g3dcolor.c:48
return NULL
Definition: dbfopen.c:1394
int dig_fflush(GVFILE *file)
Flush GVFILE.
Definition: file.c:102
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
void dig_file_init(GVFILE *file)
Initialize GVFILE.
Definition: file.c:168
void dig_rewind(GVFILE *file)
Rewind GVFILE position.
Definition: file.c:85
#define file
int dig_file_load(GVFILE *file)
Load opened GVFILE to memory.
Definition: file.c:190
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
size_t dig_fread(void *ptr, size_t size, size_t nmemb, GVFILE *file)
Read GVFILE.
Definition: file.c:122
long dig_ftell(GVFILE *file)
Get GVFILE position.
Definition: file.c:36
tuple mode
Definition: tools.py:1481