File: fileman.c

package info (click to toggle)
vflib3 3.6.14.dfsg-3+nmu3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 11,536 kB
  • ctags: 3,710
  • sloc: ansic: 35,811; sh: 10,357; asm: 3,290; makefile: 962; lisp: 123; perl: 109; awk: 43
file content (440 lines) | stat: -rw-r--r-- 9,332 bytes parent folder | download | duplicates (6)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * fileman.c - a module for file descripter management
 * by Hirotsugu Kakugawa
 *
 */
/*
 * Copyright (C) 1996-1998  Hirotsugu Kakugawa. 
 * All rights reserved.
 *
 * This file is part of the VFlib Library.  This library is free
 * software; you can redistribute it and/or modify it under the terms of
 * the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  This library is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include  "config.h"
#include  <stdio.h>
#include  <stdlib.h>
#include  "VFlib-3_6.h"
#include  "VFsys.h"
#include  "cache.h"
#include  "consts.h"


#define VF_MAX_FD_HASH  101
Private VF_CACHE  fp_cache     = NULL;
Private int       fp_case_size = VF_MAX_FILE_DESCRIPTERS;



#if 1 

/* NEW CODE */ 


struct s_ck {
  char             path[MAXPATHLEN+16];
  long             iarg1, iarg2;
  void            *arg1, *arg2;
  FM_OPEN_METHOD   open;
  FM_CLOSE_METHOD  close;
  /* debug msg */
  char dbgmsg[160];
};
typedef   struct s_ck   CK;

struct s_val {
  void            *val;
  long             iarg1, iarg2;
  void            *arg1, *arg2;
  FM_OPEN_METHOD   open;
  FM_CLOSE_METHOD  close;
};
typedef   struct s_val   VAL;

Private FILE  *get_file_stream(char *file_path, int bin_file);
Private void   close_file_stream(char *file_path, int bin_file);
Private VAL   *open_file(VF_CACHE,CK*,int);
Private void   close_file(VAL*);
Private void  *simple_open(char*,long,long,void*,void*);
Private void   simple_close(void*,long,long,void*,void*);

static int     debug_fileman = 0;


Public int
vf_fm_init(void)
{
  char  *s;

  if (fp_cache != NULL){
    /* XXX   TO DO: dispose fp_cache here */
  }

  debug_fileman = 0;
  if (getenv(VF_ENV_DEBUG_FILEMAN) != NULL)
    debug_fileman = 1;

  fp_case_size = VF_MAX_FILE_DESCRIPTERS;
  if ((s = getenv(VF_ENV_MAX_FILE_DESCRIPTERS)) != NULL)
    fp_case_size = atoi(s);
  if (fp_case_size < 1)
    fp_case_size = VF_MAX_FILE_DESCRIPTERS;
  if (debug_fileman != 0)
    printf("VFlib fileman: #fd = %d\n", fp_case_size);

  fp_cache = vf_cache_create(fp_case_size, VF_MAX_FD_HASH,
			     (void*(*)(VF_CACHE,void*,int))open_file,
			     (void(*)(void*))close_file);
  if (fp_cache == NULL){
    vf_error = VF_ERR_NO_MEMORY;
    return -1;
  }

  return 0;
}

Public FILE*
vf_fm_OpenTextFileStream(char *file_path)
{
  FILE  *fp;

  if ((fp = get_file_stream(file_path, 0)) != NULL)
    fseek(fp, 0L, SEEK_SET);
  return fp;
}

Public FILE*
vf_fm_OpenBinaryFileStream(char *file_path)
{
  FILE  *fp;

  if ((fp = get_file_stream(file_path, 1)) != NULL)
    fseek(fp, 0L, SEEK_SET);
  return fp;
}

Private FILE*
get_file_stream(char *file_path, int bin_file)
{
  struct s_ck    ck;
  struct s_val  *val;

  if (debug_fileman != 0)
    printf("VFlib fileman: Get file stream for %s\n", file_path);

  if (file_path == NULL)
    return NULL;

  memset(&ck, 0, sizeof(ck));
  strncpy(ck.path, file_path, sizeof(ck.path));
  ck.iarg1 = 0;
  ck.iarg2 = 0;
  ck.arg1  = (void*)bin_file;
  ck.arg2  = NULL;
  ck.open  = simple_open;
  ck.close = simple_close;
  strncpy(ck.dbgmsg, file_path, sizeof(ck.dbgmsg));

  if ((val = (fp_cache->get)(fp_cache, &ck, sizeof(struct s_ck))) == NULL){
    vf_error = VF_ERR_CANT_OPEN;
    return NULL;
  }

  return val->val;
}


Public void*
vf_fm_OpenFileStreamApp(char *arg, 
			long iarg1, long iarg2, void *arg1, void *arg2, 
			FM_OPEN_METHOD open_method,
			FM_CLOSE_METHOD  close_method,
			char *dbgmsg)
{
  struct s_ck    ck;
  struct s_val  *val;

  if ((debug_fileman != 0) && (dbgmsg != NULL))
    printf("VFlib fileman: Get file stream %s (%s)\n", arg, dbgmsg);

  memset(&ck, 0, sizeof(ck));
  if (arg != NULL)
    strncpy(ck.path, arg, sizeof(ck.path));
  ck.iarg1 = iarg1;
  ck.iarg2 = iarg2;
  ck.arg1  = arg1;
  ck.arg2  = arg2;
  ck.open  = open_method;
  ck.close = close_method;
  if (dbgmsg != NULL) 
    strncpy(ck.dbgmsg, dbgmsg, sizeof(ck.dbgmsg));
  
  if ((val = (fp_cache->get)(fp_cache, &ck, sizeof(ck))) == NULL){
    vf_error = VF_ERR_CANT_OPEN;
    return NULL;
  }

  return val->val;
}


Public void
vf_fm_CloseTextFileStream(char *file_path)
{
  close_file_stream(file_path, 0);
}

Public void
vf_fm_CloseBinaryFileStream(char *file_path)
{
  close_file_stream(file_path, 1);
}

Public void
close_file_stream(char *file_path, int bin_file)
{
  struct s_ck    ck;

  if (debug_fileman != 0)
    printf("VFlib fileman: Close file stream for %s\n", file_path);

  if (file_path == NULL)
    return;

  memset(&ck, 0, sizeof(ck));
  strncpy(ck.path, file_path, sizeof(ck.path));
  ck.iarg1 = 0;
  ck.iarg2 = 0;
  ck.arg1  = (void*)bin_file;
  ck.arg2  = NULL;
  ck.open  = simple_open;
  ck.close = simple_close;
  strncpy(ck.dbgmsg, file_path, sizeof(ck.dbgmsg));

  (fp_cache->del)(fp_cache, &ck, sizeof(struct s_ck));
}

Public void
vf_fm_CloseFileStreamApp(char *arg, 
			 long iarg1, long iarg2, void *arg1, void *arg2, 
			 FM_OPEN_METHOD open_method,
			 FM_CLOSE_METHOD  close_method,
			 char *dbgmsg)
{
  struct s_ck    ck;

  if ((debug_fileman != 0) && (dbgmsg != NULL))
    printf("VFlib fileman: Close App file stream %s (%s)\n", arg, dbgmsg);

  memset(&ck, 0, sizeof(ck));
  if (arg != NULL) 
    strncpy(ck.path, arg, sizeof(ck.path));
  ck.iarg1 = iarg1;
  ck.iarg2 = iarg2;
  ck.arg1  = arg1;
  ck.arg2  = arg2;
  ck.open  = open_method;
  ck.close = close_method;
  if (dbgmsg != NULL) 
    strncpy(ck.dbgmsg, dbgmsg, sizeof(ck.dbgmsg));

  (fp_cache->del)(fp_cache, &ck, sizeof(struct s_ck));
}




/* Cache Loader and Disposer */

Private VAL*
open_file(VF_CACHE c, CK *ck, int key_len)
{
  VAL  *val;

  ALLOC_IF_ERR(val, VAL){
    return NULL;
  }
  val->iarg1 = ck->iarg1;
  val->iarg2 = ck->iarg2;
  val->arg1  = ck->arg1;
  val->arg2  = ck->arg2;
  val->open  = ck->open;
  val->close = ck->close;

  if (debug_fileman != 0)
    printf("VFlib fileman: call open function for %s\n", ck->dbgmsg);
  val->val = ck->open(ck->path, ck->iarg1, ck->iarg2, ck->arg1, ck->arg2);

  return val;
}

Private void
close_file(VAL *val)
{
  if (debug_fileman != 0)
    printf("VFlib fileman: call close function\n");
  val->close(val->val, val->iarg1, val->iarg2, val->arg1, val->arg2);
  vf_free(val);
}



/* Simple File Opener/Closer */

Private void*
simple_open(char *path, long iarg1, long iarg2, void *arg1, void *arg2)
{
  FILE  *fp;
  char  *mode;

  if ((int)arg1 == 1)
    mode = "rb";
  else 
    mode = "rt";

  fp = fopen(path, mode);

  if (debug_fileman != 0){
    printf("VFlib fileman: fopen(\"%s\", \"%s\")\n", path, mode);
    printf(" ==> %p\n", (void*)fp);
  }

  return fp;
}

Private void
simple_close(void *v, long iarg1, long iarg2, void *arg1, void *arg2)
{
  FILE *fp = (FILE*)v;

  if (debug_fileman != 0)
    printf("VFlib fileman: fclose %p\n", (void*)fp);

  if (fp != NULL)
    fclose(fp);
}



#else /* old code */


static int     last_file_valid = 0;
static char   *last_file_path = NULL;  /* a kind of a cache */
static int     last_file_path_size = 0;
static FILE   *last_fp = NULL;  

Private FILE* open_file(VF_CACHE c, char *key, int key_len);
Private void  close_file(FILE* fp);


Public int
vf_fm_Init(void)
{
  if (fp_cache != NULL){
    /* XXX   TO DO: dispose fp_cache here */
  }

  fp_cache = vf_cache_create(VF_MAX_FILE_DESCRIPTERS, VF_MAX_FD_HASH,
			     (void*(*)(VF_CACHE,void*,int))open_file,
			     (void(*)(void*))close_file);
  if (fp_cache == NULL){
    vf_error = VF_ERR_NO_MEMORY;
    return -1;
  }

  return 0;
}

Public FILE*
vf_fm_OpenFileStream(char *file_path)
{
  FILE  *fp;

  if ((fp = vf_fm_GetFileStream(file_path)) != NULL)
    fseek(fp, 0L, SEEK_SET);

  return fp;
}

Public FILE*
vf_fm_GetFileStream(char *file_path)
{
  FILE  *fp;
  int   n;

#if 0
  printf("FileMan: Path %s\n", file_path);
#endif

  if (file_path == NULL)
    return NULL;

  if ((last_file_path != NULL) && (last_fp != NULL) && (last_file_valid == 1) 
      && (strcmp(last_file_path, file_path) == 0)){
    return last_fp;
  }

  if ((fp = (fp_cache->get)(fp_cache, file_path,
			    strlen(file_path)+1)) == NULL){
    vf_error = VF_ERR_CANT_OPEN;
    return NULL;
  }

  if (last_file_path_size < strlen(file_path)+1){
    n = strlen(file_path) + 64;
    vf_free(last_file_path);
    ALLOCN_IF_ERR(last_file_path, char, n){
      last_file_valid = 0;
      last_file_path_size = 0;
      last_file_path = NULL;
      return fp;
    }
    last_file_path_size = n;
    last_file_valid = 1;
    strcpy(last_file_path, file_path);
  }

  return fp;
}



/* cache loader */
Private FILE*
open_file(VF_CACHE c, char *key, int key_len)
{
#if 0
  printf("FileMan: Open %s\n", key);
#endif

  return fopen(key, FOPEN_RD_MODE_BIN);
}

/* cache disposer */
Private void
close_file(FILE* fp)
{
  /*printf("FileMan: Close \n");*/
  if (last_fp == fp)
    last_fp = NULL;
  if (fp != NULL)
    fclose(fp);
}


#endif




/*EOF*/