root/tags/5.4.pre05/wxis_src/easyfc.c

Revision 1, 14.2 kB (checked in by heitor.barbieri, 4 years ago)

Criação do svn para Cisis.

Line 
1/* ---------------------------------------------------------------- EASYFC.C */
2
3/* /////////////////////////////////////////////////////////////////////////
4
5   [ Version 1.0 ]
6   16.Oct.1998 - Functions to make things easier.
7
8   ///////////////////////////////////////////////////////////////////////// */
9
10/* ---------------------------------------------------------- C HEADER FILES */
11#include <stdlib.h>
12#include <stdio.h>
13#include <string.h>
14
15#ifdef WIN32
16#include <io.h>
17#include <sys\stat.h>
18#else /* WIN32 */
19#include <sys/io.h>
20#include <sys/stat.h>
21#endif /* WIN32 */
22/* ------------------------------------------------------------ HEADER FILES */
23/* include "cisis.h" */
24#include "easyfc2.h"
25
26/* =========================================== file content buffer variables */
27#ifdef XIS_SERVER
28const int maxPos = 10;
29int first = 1;
30int olderPos = 0;
31EFC_BUFFER fileContents[10];
32#endif
33
34/* =============================================================== efc_error */
35int efc_error(EFC_ERROR *err,   /* error structure */
36              int error_code,   /* error code */
37              char *error_info) {   /* error information */
38/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
39    1. Set error values
40   2. Return error code
41
42   1.0 - 16.Oct.1998
43>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
44
45   /* 1 */
46    err->code = error_code;
47   *(err->info) = '\0';
48   if (error_info) strncpy(err->info,error_info,EFC_ERROR_INFO_MAX);
49   /* 2 */
50   return error_code;
51} /* efc_error */
52/* ================================================================= efc_new */
53void *efc_new(long mem_size) { /* buffer size, in bytes */
54/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
55    1. Return allocated memory pointer, NULL indicates error
56
57   1.0 - 16.Oct.1998
58      a) the caller must free returned space
59>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
60
61   /* 1 */
62   return (void *)malloc((unsigned long)mem_size);
63} /* efc_new */
64/* ================================================================ efc_free */
65void *efc_free(void *buff) { /* original string buffer */
66/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
67    1. Free memory space
68   2. Return NULL
69
70   1.0 - 16.Oct.1998
71>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
72
73    /* 1 */
74    if (buff) free(buff);
75
76    /* 2 */
77   return NULL;
78} /* efc_free */
79/* ============================================================= efc_strrepl */
80char *efc_strrepl(char *old,    /* current string buffer content */
81                  char *from) { /* new string buffer content */
82/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
83    1. Free old memory space
84    2. Return allocated memory copied, NULL indicates error
85
86   1.0 - 16.Oct.1998
87      a) the caller must free returned space
88>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
89
90    /* 1 */
91   efc_free(old);
92
93    /* 2 */
94   return strdup(from);
95} /* efc_strrepl */
96/* ========================================================== efc_trim_right */
97char *efc_trim_right(char *buff) { /* string buffer to be trimed */
98/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
99    1. Eliminate trailing spaces
100   2. Return trailed buffer
101
102   1.0 - 16.Oct.1998
103>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
104   char *p;  /* auxiliary loop string pointer */
105   long len; /* original string lenght */
106
107    /* 1 */
108   if (buff)
109    if (*buff)
110           for (len = strlen(buff),p = buff+(len-1); len && *p == ' '; len--,p--)
111           if (*p == ' ') *p = '\0';
112
113    /* 2 */
114   return buff;
115} /* efc_trim_right */
116/* ============================================================ efc_filesize */
117long efc_filesize(int handle) { /* file handle */
118/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
119    1. Get file status information
120   2. Return file size on success, or -1 on error
121
122   1.0 - 16.Oct.1998
123    a) fstat isn't an ANSI function
124>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
125   struct stat statbuf; /* stat structure */
126   int err; /* stat structure */
127
128    /* 1 */
129    err = fstat(handle,&statbuf);
130
131    /* 2 */
132   return (long)(err ? err : statbuf.st_size);
133} /* efc_filesize */
134/* ================================================= efc_filecontents_return */
135char *efc_filecontents_return(char *buff,        /* return buffer */
136                              long *qttbytes, /* file size in bytes on success, negative code on error */
137                              int error_code) { /* error code */
138/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
139    1. If error, set qttbytes with the negative error code
140   2. Return buffer
141
142   1.0 - 16.Oct.1998
143>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
144
145    /* 1 */
146    if (error_code) *qttbytes = (long)error_code;
147
148    /* 1 */
149    return buff;
150} /* efc_filecontents_return */
151/* ========================================================= efc_filecontent */
152char *efc_filecontent(char *read_file,  /* file name */
153                      long *qttbytes) {   /* size of the file in bytes */
154/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
155    1. Open the file, on error: qttbytes = -2
156   2. Get file size in bytes, on error: qttbytes = -1
157   3. Allocate file content buffer, on error: qttbytes = -3
158   4. Read file content, set end of string buffer, on error: qttbytes = -4
159   5. Close file
160   6. Return allocated buffer pointer
161
162   1.0 - 16.Oct.1998
163    a) on error -2 or -4, "errno" contains the system error code number
164      b) the caller must free returned space
165>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
166   int handle; /* file handle */
167   char *buff; /* buffer pointer */
168
169   /* 1 */
170   handle = open(read_file,O_TEXT|O_RDONLY|O_DENYNONE);
171   if (handle < 0) return efc_filecontents_return(NULL,qttbytes,-FILE_ERROR_OPEN);
172
173   /* 2 */
174   *qttbytes = efc_filesize(handle);
175   if (*qttbytes <= 0) {
176       close(handle);
177       return NULL; /* -FILE_ERROR_SIZE */
178   }
179
180   /* 3 */
181   buff = (char *)efc_new(*qttbytes + 1);
182   if (!buff) {
183       close(handle);
184       return efc_filecontents_return(NULL,qttbytes,-FILE_ERROR_ALLOC);
185   }
186
187   /* 4 */
188    *qttbytes = read(handle,buff,(unsigned int)*qttbytes);
189    if (*qttbytes <= 0) {
190        close(handle);
191        return efc_filecontents_return(NULL,qttbytes,-FILE_ERROR_READ);
192    }
193    *(buff+(*qttbytes)) = '\0';
194
195   /* 5 */
196   close(handle);
197
198   /* 6 */
199   return buff;
200} /* efc_filecontent */
201/* ===================================================== efc_std_filecontent */
202char *efc_std_filecontent(EFC_ERROR *err,    /* error structure */
203                          char *file_name) {   /* file name */
204/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
205    1. Get file content
206    2. Check error
207    3. Return allocated buffer
208
209   1.0 - 16.Oct.1998
210        a) the caller must free returned space
211>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
212    char *buff;     /* file content buffer pointer */
213    long qttbytes; /* file size, in bytes */
214
215   /* 1 */
216
217#ifndef XIS_SERVER
218    buff = efc_filecontent(file_name,&qttbytes);
219#else
220   buff = efc_load_buffer(file_name, &qttbytes);
221#endif
222
223   /* 2 */
224   if (!buff) efc_error(err,qttbytes,file_name);
225
226   /* 3 */
227   return buff;
228}
229/* ==================================================== efc_split_lines_char */
230long efc_split_lines_char(EFC_SPLIT_LINES *split_lines,  /* split lines structure */
231                          char *text,                    /* text to be splited */
232                          char char_sep) {                 /* separator character */
233/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
234    1. Count number of lines
235   2. Allocate line pointers
236   3. Set line begin pointers
237   4. Return number of lines
238
239   1.0 - 24.Nov.1998
240>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
241    char *p;                /* auxiliary string buffer pointer */
242   char char_pair = '\0';
243   BOOLEAN newline; /* new line flag */
244   long i;              /* line index */
245
246   /* 1 */
247    for (split_lines->qtt = 0L,newline = TRUE,p = text; *p; p++) {
248      if (newline) {
249        split_lines->qtt++;
250         newline = FALSE;
251      }
252        if (*p == char_sep) {
253        newline = TRUE;
254         continue;
255      }
256   } /* for */
257
258   /* 2 */
259   split_lines->list = NULL;    /* 16.Apr.1999 for empty buffer */
260   if (split_lines->qtt) {
261       split_lines->list = (EFC_LINE *)efc_new((size_t)sizeof(EFC_LINE)*split_lines->qtt);
262      if (!split_lines->list) return -1L;
263   }
264
265   /* 3 */
266    if (char_sep == '\r') char_pair  = '\n';
267    if (char_sep == '\n') char_pair  = '\r';
268    for (newline = TRUE,i = 0L,p = text; *p; p++) {
269      if (newline) {
270            if (*p == char_pair) p++;
271        split_lines->list[i++].text = p;
272         newline = FALSE;
273      }
274        if (*p == char_pair && *(p+1) == char_sep) *p = '\0';
275        if (*p == char_sep) {
276         *p = '\0';
277        newline = TRUE;
278      }
279   } /* for */
280
281   /* 4 */
282   return split_lines->qtt;
283}
284/* ========================================================= efc_split_lines */
285long efc_split_lines(EFC_SPLIT_LINES *split_lines,  /* split lines structure */
286                     char *text) {                    /* text to be splited */
287/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
288    1. Split lines
289
290   1.0 - 24.Nov.1998
291>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
292
293   /* 1 */
294    return efc_split_lines_char(split_lines,text,'\n');
295}
296/* ==================================================== efc_split_lines_free */
297void efc_split_lines_free(EFC_SPLIT_LINES *split_lines) { /* split lines structure */
298/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
299    1. No lines, garbage collector
300
301   1.0 - 24.Nov.1998
302>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
303
304    /* 1 */
305    split_lines->qtt = 0L;
306   split_lines->list = (EFC_LINE *)efc_free(split_lines->list);
307}
308
309/* ========================================================== efc_numberList */
310long efc_numberList(EFC_ERROR *err,                 /* error structure */
311                    EFC_NUMBER_LIST *numberList,    /* store number list */
312                    char *text) {                   /* text to be splited */
313   char *textDup;                                       /* text string copy */
314   EFC_SPLIT_LINES splitLines;                  /* split lines structure */
315    int line;                                 /* line index */
316
317    /* initialization */
318   splitLines.qtt = 0L;
319   splitLines.list = NULL;
320
321    /* split text string into lines */
322   textDup = strdup(text);
323    if (!textDup)
324   {
325    return -efc_error(err,NUMBERLIST_ERROR_ALLOC,text);
326   }
327   if (efc_split_lines(&splitLines,textDup) < 0L)
328    {
329    return -efc_error(err,NUMBERLIST_ERROR_ALLOC,textDup);
330    }
331
332    /* store numbers from each text line */
333    numberList->qtt = splitLines.qtt;
334    numberList->list = (long *)efc_new( (size_t)sizeof(long *) * numberList->qtt );
335    for (line = 0L; line < numberList->qtt; line++)
336    {
337        numberList->list[line] = atol(splitLines.list[line].text);
338    }
339
340    /* garbage collector */
341    efc_free(textDup);
342    efc_split_lines_free(&splitLines);
343
344    /* return quantity of numbers */
345    return numberList->qtt;
346
347} /* efc_numberList */
348
349/* ====================================================== efc_freeNumberList */
350void efc_numberListFree(EFC_NUMBER_LIST *numberList) {      /* number list */
351    numberList->qtt = 0L;
352   numberList->list = (long *)efc_free(numberList->list);
353} /* efc_numberListFree */
354
355/* ------------------------------------------------------------ efc_copyFile */
356COPYFILE_ERROR efc_copyFile(unsigned char *fileNameIn,
357                            unsigned char *fileNameOut) {
358   FILE *copyIn;
359   FILE *copyOut;
360   int theChar;
361
362   copyIn = fopen((const char *)fileNameIn,"rb");
363   if (!copyIn)
364   {
365    return COPYFILE_ERROR_IN_OPEN;
366   }
367
368   copyOut = fopen((const char *)fileNameOut,"wb");
369   if (!copyOut)
370   {
371    fclose(copyIn);
372       return COPYFILE_ERROR_OUT_OPEN;
373   }
374
375   theChar = fgetc(copyIn);
376   while (theChar != EOF)
377   {
378        fputc(theChar,copyOut);
379    theChar = fgetc(copyIn);
380   }
381
382   fclose(copyIn);
383   fclose(copyOut);
384
385   return COPYFILE_ERROR_OK;
386
387} /* efc_copyFile */
388
389#ifdef XIS_SERVER
390/* ========================================================= efc_load_buffer */
391char * efc_load_buffer(char *fileName,
392                       long *qttbytes) {
393    char * ret = NULL;
394    long size = 0;
395    int newPos = 0;
396    int pos = 0;
397
398    if (first == 1) {
399        for (pos = 0; pos < maxPos; pos++) {
400            fileContents[pos].fname[0] = '\0';
401        }
402        //memset(fileContents, 0x00, sizeof(fileContents));
403        first = 0;
404    }
405
406    for (pos = 0; pos < maxPos; pos++) {
407        if (fileContents[pos].fname[0] == '\0') {
408            fileContents[pos].buffer = efc_filecontent(fileName, &size);
409            if (size > 0) {
410                strcpy(fileContents[pos].fname, fileName);
411                ret = fileContents[pos].buffer;
412            }
413            break;
414        } else if (strcmp(fileName, fileContents[pos].fname) == 0) {
415            ret = fileContents[pos].buffer;
416            size = ((ret != NULL) ? strlen(ret) : 0);
417            break;
418        }
419    }
420
421    if (ret == NULL) {
422
423        newPos = olderPos;
424        olderPos = ((olderPos < (maxPos - 1)) ? (++olderPos) : 0);
425        ret = efc_filecontent(fileName, &size);
426        if (size > 0) {
427            efc_free(fileContents[newPos].buffer);
428            fileContents[newPos].buffer = ret;
429            strcpy(fileContents[newPos].fname, fileName);
430        } else {
431            olderPos = newPos;
432        }
433    }
434
435    *qttbytes = size;
436
437    return ret;
438}
439
440/* ======================================================== efc_delete_buffer */
441void efc_delete_buffer() {
442    int pos = 0;
443
444    for (pos = 0; pos < maxPos; pos++) {
445        efc_free(fileContents[pos].buffer);
446        fileContents[pos].buffer = NULL;
447        fileContents[pos].fname[0] = '\0';
448    }
449}
450#endif /* XIS_SERVER */
Note: See TracBrowser for help on using the browser.