📄 fileutil.c
字号:
/****************** Start of $RCSfile: fileutil.c,v $ ****************** $Source: /home/alb/afbackup/afbackup-3.3.8.1/RCS/fileutil.c,v $* $Id: fileutil.c,v 1.7 2005/01/15 08:56:22 alb Exp alb $* $Date: 2005/01/15 08:56:22 $* $Author: alb $********* description *********************************************************************************************************************/#include <conf.h>#include <version.h> static char * fileversion = "$RCSfile: fileutil.c,v $ $Source: /home/alb/afbackup/afbackup-3.3.8.1/RCS/fileutil.c,v $ $Id: fileutil.c,v 1.7 2005/01/15 08:56:22 alb Exp alb $ " PACKAGE " " VERSION_STRING;#include <stdio.h>#include <string.h>#include <fcntl.h>#include <ctype.h>#include <dirent.h>#include <unistd.h>#include <stdarg.h>#include <sys/stat.h>#include <sys/types.h>#ifdef HAVE_SYS_PARAM_H#include <sys/param.h>#endif#include <genutils.h>#include <mvals.h>#include <fileutil.h>#include <sysutils.h>#include <x_regex.h>#define CLEANUP { goto cleanup; }#define CLEANUPR(ret) { r = ret ; goto cleanup; }#define GETOUT { goto getout; }/* Function for inserting the output of a typical "save"-function * into a possibly existing file into the block marked by two * delimiters determined by <blockname> and END_<blockname>. * * save_block_func is the users save-function, that wants to insert * into the file named by <filename>. <block> is the data structure, * the user's function wants to save. */Int32 save_insert( UChar *filename, UChar *blockname, Int32 (*save_block_func)(FILE *,void *), void *block){ FILE *fpw; UChar end_mark[100]; Int32 not_in_file; Int32 i, j; Int32 own_line, line_ended; UChar *cptr = NULL; UChar **file; Int32 num_lines; /* if(errno) return(errno); *//* no longer critical */ num_lines = 0;/* read the existing file into a string array */ file = read_asc_file(filename, &num_lines);/* if the file doesn't exist, this is no error */ if(file == NULL) errno = NO_ERROR;/* open the (possibly same) file for writing */ fpw = fopen(filename, "w"); if(fpw == NULL){ free_asc_file(file, num_lines); return(errno = NO_ACCESS_TO_FILE); } i = -1; not_in_file = 1; line_ended = 0; if(file){ forever{ /* search for the beginning block delimiter <blockname> */ i++; if(i >= num_lines) break; cptr = strstr(file[i], blockname); if(cptr != NULL){ /* block marker is found */ UChar tmp_buf[BUFSIZ]; /* handle it, if the marker is not at the beginning of the line */ if(cptr != file[i]){ strncpy(tmp_buf, file[i], (Int32) (cptr - file[i])); fprintf(fpw, "%s", tmp_buf); } not_in_file = 0; break; } own_line = 0; /* if there is some information left in the existing line, * put it into a new own line */ for(j = 0; file[i][j] != '\0'; j++) if(! isspace(file[i][j])) own_line = 1; if(!line_ended && !own_line){ fprintf(fpw, "\n"); line_ended = 1; } if(own_line){ fprintf(fpw, "%s\n", file[i]); line_ended = 0; } } }/* call the users save-function with his data as parameter, * handle user's errors (his function has to return 0, if no * error occured) */ if((errno = save_block_func(fpw, block)) != NO_ERROR){ fclose(fpw); free_asc_file(file, num_lines); return(errno); }/* append the rest of the previously existing file to the * newly generated one */ fprintf(fpw, "\n"); if(file){ if(! not_in_file){ sprintf(end_mark, "END_%s", blockname); /* handle the case, that the end-mark is in the same line * as the begin-mark */ if(strstr(file[i], end_mark) > (char *) cptr && cptr != NULL){ cptr = strstr(file[i], end_mark) + strlen(end_mark); fprintf(fpw, "%s\n", cptr); } /* search for the end-mark */ forever{ i++; if(i >= num_lines){ /* There is no end-mark -> error */ fclose(fpw); free_asc_file(file, num_lines); return(errno = FILE_FORMAT_ERROR); } if(strstr(file[i], end_mark) != NULL){ /* end-mark found. If it's not the last expression in the * line, copy the rest */ cptr = strstr(file[i], end_mark) + strlen(end_mark); fprintf(fpw, "%s\n", cptr); break; } } } /* copy the rest of the lines into the new file, skip empty lines */ j = 0; for(i++; i < num_lines; i++){ if(! empty_string(file[i])){ fprintf(fpw, "%s\n", file[i]); j = 0; } else{ if(! j) fprintf(fpw, "\n"); j = 1; } } } /* end of if(file) */ fclose(fpw); return(errno);}/**** Function to read parameters out of a file ****/#define re_start_buffer (re_buffers + 0)#define re_end_buffer (re_buffers + 1)static RE_cmp_buffer *re_buffers = NULL;static Int32 num_re_buffers = 0;static Int32 allocate_re_buffers(Int32);static UChar *read_a_line(FILE *);static Int32 get_entry(ParamFileEntry *, UChar *);Int32read_param_file( UChar *filename, ParamFileEntry *entries, Int32 num_entries, UChar *start_pat, UChar *end_pat){ FILE *fp = NULL; Int32 errc, i, j, ret = NO_ERROR; UChar *line; UChar *flags; line = NULL; if(num_entries < 1) return(num_entries < 0 ? (errno = ILLEGAL_VALUE) : NO_ERROR); fp = fopen(filename, "r"); if(!fp) return(errno = NO_SUCH_FILE); if( (errc = allocate_re_buffers(num_entries + 2)) ) return(errc); flags = NEWP(UChar, num_entries); if(!flags) return(errno); memset(flags, 0, num_entries * sizeof(UChar)); for(i = 0; i < num_entries; i++){ if(re_compile_pattern(entries[i].pattern, strlen(entries[i].pattern), re_buffers + i + 2)) GETOUT; if(entries[i].num_entries) *(entries[i].num_entries) = 0; } if(start_pat) if(re_compile_pattern(start_pat, strlen(start_pat), re_start_buffer)) GETOUT; if(end_pat) if(re_compile_pattern(end_pat, strlen(end_pat), re_end_buffer)) GETOUT; if(start_pat) forever{ if(line){ free(line); line = NULL; } if(!(line = read_a_line(fp))) CLEANUP; if(re_find_match(re_start_buffer, line, NULL, NULL) >= 0) break; } if(line) free(line); while( (line = read_a_line(fp)) ){ if(end_pat) if(re_find_match(re_end_buffer, line, NULL, NULL) >= 0) CLEANUP; for(i = 0; i < num_entries; i++){ if(flags[i]) continue; if(re_find_match(re_buffers + i + 2, line, NULL, &j) >= 0){ get_entry(entries + i, line + j); flags[i] = 1; } } free(line); } cleanup: if(line) free(line); if(flags) free(flags); if(fp) fclose(fp); return(ret); getout: ret = errno; CLEANUP;}static UChar *yesstrings[] = { "1", "yes", "true", "ok", NULL, };static Int32get_entry( ParamFileEntry *entry, UChar *line){ UChar *cptr, **cpptr; Uns32 num_values; void *values = NULL; double indouble; float infloat; long int inlongint; unsigned long inlonguns; short int inshortint; unsigned short inshortuns; unsigned char inchar; int j; num_values = 0; cptr = line; if(!entry->entry_ptr) return(NO_ERROR); if(entry->num_entries) *entry->num_entries = 0; switch(entry->type){ case TypeReal64: for(; *cptr;){ if(sscanf(cptr, "%lf", &indouble) < 1) cptr++; else{ if(entry->num_entries) *entry->num_entries = 1; *((Real64 *) entry->entry_ptr) = (Real64) indouble; break; } } break; case TypeReal32: for(; *cptr;){ if(sscanf(cptr, "%f", &infloat) < 1) cptr++; else{ if(entry->num_entries) *entry->num_entries = 1; *((Real32 *) entry->entry_ptr) = (Real32) infloat; break; } } break; case TypeInt32: for(; *cptr;){ if(sscanf(cptr, "%ld", &inlongint) < 1) cptr++; else{ if(entry->num_entries) *entry->num_entries = 1; *((Int32 *) entry->entry_ptr) = (Int32) inlongint; break; } } break; case TypeUns32: for(; *cptr;){ if(sscanf(cptr, "%lu", &inlonguns) < 1) cptr++; else{ if(entry->num_entries) *entry->num_entries = 1; *((Uns32 *) entry->entry_ptr) = (Uns32) inlonguns; break; } } break; case TypeInt16: for(; *cptr;){ if(sscanf(cptr, "%hd", &inshortint) < 1) cptr++; else{ if(entry->num_entries) *entry->num_entries = 1; *((Int16 *) entry->entry_ptr) = (Int16) inshortint; break; } } break; case TypeUns16: for(; *cptr;){ if(sscanf(cptr, "%hu", &inshortuns) < 1) cptr++; else{ if(entry->num_entries) *entry->num_entries = 1; *((Uns16 *) entry->entry_ptr) = (Uns16) inshortuns; break; } } break; case TypeUChar: case TypeSChar: for(; *cptr;){ if(sscanf(cptr, "%c", &inchar) < 1) cptr++; else{ if(entry->num_entries) *entry->num_entries = 1; *((UChar *) entry->entry_ptr) = (UChar) inchar; break; } } break; case TypeFlag: *((Flag *) entry->entry_ptr) = NO; if(entry->num_entries) *entry->num_entries = 1; cptr = first_nospace(cptr); j = first_space(cptr) - cptr; for(cpptr = yesstrings; *cpptr; cpptr++){ if(!strncasecmp(*cpptr, cptr, j)){ *((Flag *) entry->entry_ptr) = YES; break; } } break; case TypeReal64PTR: values = seg_malloc(sizeof(Real64)); if(!values) GETOUT; for(; *cptr;){ if(sscanf(cptr, "%lf", &indouble) < 1){ cptr++; continue; } values = seg_realloc(values, (num_values + 1) * sizeof(Real64), num_values * sizeof(Real64)); if(!values) GETOUT; *((Real64 *) values + num_values) = indouble; num_values++; sscanf(cptr, "%*f%n", &j); cptr += j; } if(entry->num_entries) *entry->num_entries = num_values; *((Real64 **) entry->entry_ptr) = values; break; case TypeReal32PTR: values = seg_malloc(sizeof(Real32)); if(!values) GETOUT; for(; *cptr;){ if(sscanf(cptr, "%f", &infloat) < 1){ cptr++; continue; } values = seg_realloc(values, (num_values + 1) * sizeof(Real32), num_values * sizeof(Real32)); if(!values) GETOUT; *((Real32 *) values + num_values) = infloat; num_values++; sscanf(cptr, "%*f%n", &j); cptr += j; } if(entry->num_entries) *entry->num_entries = num_values; *((Real32 **) entry->entry_ptr) = values; break; case TypeInt32PTR: values = seg_malloc(sizeof(Int32)); if(!values) GETOUT; for(; *cptr;){ if(sscanf(cptr, "%ld", &inlongint) < 1){ cptr++; continue; } values = seg_realloc(values, (num_values + 1) * sizeof(Int32), num_values * sizeof(Int32)); if(!values) GETOUT; *((Int32 *) values + num_values) = inlongint; num_values++; sscanf(cptr, "%*d%n", &j); cptr += j; } if(entry->num_entries) *entry->num_entries = num_values; *((Int32 **) entry->entry_ptr) = values; break; case TypeUns32PTR: values = seg_malloc(sizeof(Uns32)); if(!values) GETOUT; for(; *cptr;){ if(sscanf(cptr, "%lu", &inlonguns) < 1){ cptr++; continue; } values = seg_realloc(values, (num_values + 1) * sizeof(Uns32), num_values * sizeof(Uns32)); if(!values) GETOUT; *((Uns32 *) values + num_values) = inlonguns; num_values++; sscanf(cptr, "%*u%n", &j); cptr += j; } if(entry->num_entries) *entry->num_entries = num_values; *((Uns32 **) entry->entry_ptr) = values; break; case TypeInt16PTR: values = seg_malloc(sizeof(Int16)); if(!values) GETOUT; for(; *cptr;){ if(sscanf(cptr, "%hd", &inshortint) < 1){ cptr++; continue; } values = seg_realloc(values, (num_values + 1) * sizeof(Int16), num_values * sizeof(Int16)); if(!values) GETOUT; *((Int16 *) values + num_values) = inshortint; num_values++; sscanf(cptr, "%*d%n", &j); cptr += j; } if(entry->num_entries) *entry->num_entries = num_values; *((Int16 **) entry->entry_ptr) = values; break; case TypeUns16PTR: values = seg_malloc(sizeof(Uns16)); if(!values) GETOUT; for(; *cptr;){ if(sscanf(cptr, "%hu", &inshortuns) < 1){ cptr++; continue; } values = seg_realloc(values, (num_values + 1) * sizeof(Uns16), num_values * sizeof(Uns16)); if(!values) GETOUT; *((Uns16 *) values + num_values) = inshortuns; num_values++; sscanf(cptr, "%*u%n", &j); cptr += j; } if(entry->num_entries) *entry->num_entries = num_values; *((Uns16 **) entry->entry_ptr) = values; break; case TypeUCharPTR: case TypeSCharPTR: *((UChar **) entry->entry_ptr) = strdup(cptr); if(! (*((UChar **) entry->entry_ptr))) GETOUT; if(entry->num_entries) *entry->num_entries = strlen(cptr); break; } return(NO_ERROR); getout: if(values) free(values); return(errno);}static UChar *read_a_line(FILE * fp)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -