File: file.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (276 lines) | stat: -rw-r--r-- 5,633 bytes parent folder | download | duplicates (3)
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
/*!
   \file diglib/file.c

   \brief Vector library (diglib) - file management

   Lower level functions for reading/writing/manipulating vectors.

   Note: seems that the time is almost the same for both cases: 
    - reading from file 
    - load whole file to memory and read from memory
   
   (C) 2001-2009 by the GRASS Development Team

   This program is free software under the GNU General Public License
   (>=v2).  Read the file COPYING that comes with GRASS for details.

   \author Dave Gerdes, Radim Blazek
 */

#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <grass/gis.h>
#include <grass/Vect.h>
#include <grass/glocale.h>

/*!
  \brief Get GVFILE position.

  \param file pointer to GVFILE structure

  \return current file position
*/
long dig_ftell(GVFILE *file)
{
    if (file->loaded) /* using memory */
	return (file->current - file->start);

    return (ftell(file->file));
}

/*!
  \brief Set GVFILE position.
 
  Start positions:
  
   - SEEK_SET (start)
   - SEEK_CUR (current position)
   - SEEK_END (end)
  
  \param file pointer to GVFILE structure
  \param offset offset position
  \param whence start position

  \return 0 OK
  \return -1 error
*/
int dig_fseek(GVFILE * file, long offset, int whence)
{
    if (file->loaded) {	 /* using memory */
	switch (whence) {
	case SEEK_SET:
	    file->current = file->start + offset;
	    break;
	case SEEK_CUR:
	    file->current += offset;
	    break;
	case SEEK_END:
	    file->current = file->start + file->size + offset;
	    break;
	}
	return 0;
    }

    return (fseek(file->file, offset, whence));
}

/*!
  \brief Rewind GVFILE position.
 
  \param file pointer to GVFILE structure
*/
void dig_rewind(GVFILE * file)
{
    if (file->loaded) {	/* using memory */
	file->current = file->start;
    }
    else {
	rewind(file->file);
    }
}

/*!
  \brief Flush GVFILE.
 
  \param file pointer to GVFILE structure

  \return 0
*/
int dig_fflush(GVFILE * file)
{
    if (file->loaded) {	/* using memory */
	return 0;
    }
    else {
	return (fflush(file->file));
    }
}

/*!
  \brief Read GVFILE.
 
  \param[out] ptr data buffer
  \param size buffer size
  \param nmemb number of members
  \param file pointer to GVFILE structure

  \return number of read members
 */
size_t dig_fread(void *ptr, size_t size, size_t nmemb, GVFILE *file)
{
    long tot;
    size_t cnt;

    if (file->loaded) {	/* using memory */
	if (file->current >= file->end) { /* EOF */
	    return 0;
	}
	tot = size * nmemb;
	cnt = nmemb;
	if (file->current + tot > file->end) {
	    tot = file->end - file->current;
	    cnt = (int)tot / size;
	}
	memcpy(ptr, file->current, tot);
	file->current += tot;
	return (cnt);
    }
    return (fread(ptr, size, nmemb, file->file));
}

/*!
  \brief Write GVFILE.

  \param ptr data buffer
  \param size buffer size
  \param nmemb number of members
  \param[out] file pointer to GVFILE structure

  \return number of items written
 */
size_t dig_fwrite(void *ptr, size_t size, size_t nmemb, GVFILE *file)
{
    if (file->loaded) {	/* using memory */
	G_fatal_error(_("Writing to file loaded to memory not supported"));
    }

    return fwrite(ptr, size, nmemb, file->file);
}

/*!
  \brief Initialize GVFILE.
  
  \param[out] file pointer to GVFILE structure
*/
void dig_file_init(GVFILE *file)
{
    file->file = NULL;
    file->start = NULL;
    file->current = NULL;
    file->end = NULL;
    file->size = 0;
    file->alloc = 0;
    file->loaded = 0;
}

/*!
  \brief Load opened GVFILE to memory.
 
  Warning: position in file is set to the beginning.
 
  \param file pointer to GVFILE structure

  \return 1 loaded
  \return 0 not loaded
  \return -1 error
*/
int dig_file_load(GVFILE * file)
{
    int ret, mode, load;
    char *cmode;
    size_t size;
    struct stat sbuf;

    G_debug(2, "dig_file_load ()");

    if (file->file == NULL) {
	G_warning(_("Unable to load file to memory, file not open"));
	return -1;
    }

    /* Get mode */
    mode = GV_MEMORY_NEVER;
    cmode = G__getenv("GV_MEMORY");
    if (cmode != NULL) {
	if (G_strcasecmp(cmode, "ALWAYS") == 0)
	    mode = GV_MEMORY_ALWAYS;
	else if (G_strcasecmp(cmode, "NEVER") == 0)
	    mode = GV_MEMORY_NEVER;
	else if (G_strcasecmp(cmode, "AUTO") == 0)
	    mode = GV_MEMORY_AUTO;
	else
	    G_warning(_("Vector memory mode not supported, using 'AUTO'"));
    }
    G_debug(2, "  requested mode = %d", mode);


    fstat(fileno(file->file), &sbuf);
    size = sbuf.st_size;
    
    G_debug(2, "  size = %u", size);
    
    /* Decide if the file should be loaded */
    /* TODO: I don't know how to get size of free memory (portability) to decide if load or not for auto */
    if (mode == GV_MEMORY_AUTO)
	mode = GV_MEMORY_NEVER;
    if (mode == GV_MEMORY_ALWAYS)
	load = 1;
    else
	load = 0;

    if (load) {
	file->start = G_malloc(size);
	if (file->start == NULL)
	    return -1;

	fseek(file->file, 0L, 0);
	ret = fread(file->start, size, 1, file->file);	/* Better to read in smaller portions? */
	fseek(file->file, 0L, 0);	/* reset to the beginning */

	if (ret <= 0) {
	    G_free(file->start);
	    return -1;
	}

	file->alloc = size;
	file->size = size;
	file->current = file->start;
	file->end = file->start + size;

	file->loaded = 1;
	G_debug(2, "  file was loaded to the memory");
	return 1;
    }
    else {
	G_debug(2, "  file was not loaded to the memory");
    }

    return 0;
}

/*!
  \brief Free GVFILE.

  \param file pointer to GVFILE structure
*/
void dig_file_free(GVFILE * file)
{
    if (file->loaded) {
	G_free(file->start);
	file->loaded = 0;
	file->alloc = 0;
    }
}