📄 dmc_request.c
字号:
/* dmc_request.c 02mar89 mwiederspahn utig * * modified 05jul89 mw open for a+, if this fails, open r * * This package permits random access to interim DMC request files. * It works only on the second portion of the mail which is * in a request-record format (seed DMC.h). * * a similar package exists for entry files. * * This package assumes only: * a) read/write disk file, b) fixed length records * c) format strings and structures in "DMC.h" apply. * * This package is BSD UNIX specific (due to include files). *//* * open_DMC_request( name, file, size ) open a holdings file * read_DMC_request( file, rec, buf ) read a record into buf * get_DMC_request( file, rec, request ) read a record into struct request * write_DMC_request( file, rec, buf ) write a record from buf * put_DMC_request( file, rec, request ) write a record from struct request * close_DMC_request( file ) close a holdings file */#include <stdio.h>#include <string.h>#include <sys/file.h>#include <sys/types.h>#include <sys/stat.h>#include <memory.h>#include "DMC.h"int split();char *get_fld();/* ---------------------------------------------------------------------- */int open_DMC_request( name, file, size )char *name; /* input file name, will be created if needed */FILE **file; /* output file number */int *size; /* size in records */{ struct stat statbuf; if((*file = fopen(name, "r")) == NULL ) return( -1 ); if( fstat( fileno(*file) , &statbuf ) == 0 ) { *size = (int)statbuf.st_size / DMC_request_reclen; return( 0 ); } else { return( -1 ); }}int get_DMC_request(file, request)FILE *file; /* open file */struct DMC_request *request; /* returned request */{ char buf[DMC_request_reclen+1]; int num_flds; char *p, **ch_ptr; memset(request, 0, sizeof(struct DMC_request)); if (fgets(buf, DMC_request_reclen, file) == NULL) { if (ferror(file)) perror("get_DMC_request"); return( -1 ); } /* eliminate the \n if there */ buf[strlen(buf) - 1] = 0; num_flds = split(buf, &ch_ptr, '\t'); if (num_flds != DMC_request_count) { fprintf(stderr, "dmc_request() : incorrect number of fields scanned from \"h\" file\n"); fprintf(stderr, "\texpected:%d: scanned:%d\n", DMC_request_count, num_flds); fuse(&ch_ptr, num_flds); return -1; } strncpy(request->entry.statn, ch_ptr[0], sizeof(request->entry.statn) - 1); strncpy(request->entry.network, ch_ptr[1], sizeof(request->entry.network) - 1); strncpy(request->entry.chn, ch_ptr[2], sizeof(request->entry.chn) - 1); strncpy(request->entry.location, ch_ptr[3], sizeof(request->entry.location) - 1); strncpy(request->entry.start, ch_ptr[4], sizeof(request->entry.start) - 1); strncpy(request->entry.end, ch_ptr[5], sizeof(request->entry.end) - 1); strncpy(request->entry.file, ch_ptr[6], sizeof(request->entry.file) - 1); strncpy(request->entry.superfile, ch_ptr[7], sizeof(request->entry.superfile) - 1); strncpy(request->reqstart, ch_ptr[8], sizeof(request->reqstart) - 1); strncpy(request->reqend, ch_ptr[9], sizeof(request->reqend) - 1); if( (p = strchr(request->entry.file, ' ')) != NULL) *p='\0'; if((p = strchr(request->entry.superfile, ' ')) != NULL) *p='\0'; /* make sure the station name and channel and network are * padded with spaces to field length */ if (strlen(request->entry.statn) < 5) { strncpy(&(request->entry.statn[strlen(request->entry.statn)]), " ", 5 - strlen(request->entry.statn)); } if (strlen(request->entry.chn) < 3) { strncpy(&(request->entry.chn[strlen(request->entry.chn)]), " ", 3 - strlen(request->entry.chn)); } if (strlen(request->entry.network) < 2) { strncpy(&(request->entry.network[strlen(request->entry.network)]), " ", 2 - strlen(request->entry.network)); } if (strlen(request->entry.location) < 2) { strncpy(&(request->entry.location[strlen(request->entry.location)]), " ", 2 - strlen(request->entry.location)); } fuse(&ch_ptr, num_flds); return(0);}int close_DMC_request( file )FILE *file;{ return( fclose(file) );}/* ------------------------------------------------------------------- *//* ------------------------------------------------------------------- *//* split() will parse a string into components, based on the fs parameter * The fs param is very specific, space is space, tab is tab, etc * Always call fuse() when done */int split(s, parts, fs)char *s; /* thing to split */char ***parts; /* put parts into */char fs; /* field separator */ { int n, i; char wrkstr[512]; char *ch_ptr; i = 0; /* check for in bounds */ if (strlen(s) > sizeof(wrkstr)) { fprintf(stderr, "split(): too big to split - string= %s\n", s); return 0; } /* determine how many "parts" will be split */ if (strlen(s) == 0) return 0; strcpy(wrkstr, s); n = 0; /* always at least one part unless null string */ n = count_flds(wrkstr, fs); *parts = (char **)malloc(n * sizeof(char *)); ch_ptr = get_fld(wrkstr, fs); do { (*parts)[i] = (char *)malloc(strlen(ch_ptr) + 1); strcpy((*parts)[i], ch_ptr); i++; if (i > n) { fprintf(stderr, "split(), got \"splitting\" headache, not tonight dear!\n"); break; } ch_ptr = get_fld(NULL, fs); } while (ch_ptr != NULL); /* make check for i < n, not sure why this would happen */ if (i < n) { *parts = (char **)realloc(*parts, i * sizeof(char *)); } return i;}int count_flds(s, fs)char *s, fs;{ int fld_flag = 1; /* always start with a field, * so set flag to count it */ int num_flds = 0; int i; for (i = 0; i < strlen(s); i++) { if (s[i] == fs) { /* special case where we have two field seps * and no alpha-numeric btw, count field */ if (fld_flag) /* very special case, not 1st char */ if (i != 0) num_flds++; fld_flag = 1; } else if (fld_flag) { num_flds++; /* set false until we encounter next field sep */ fld_flag = 0; } } return num_flds;}char *get_fld(s, fs)char *s, fs;{ static char *sav_fld_ptr; char *fld_ptr; int found_fld = 0; int n; if (s != NULL) { fld_ptr = s; } else fld_ptr = sav_fld_ptr; if (*fld_ptr == '\0') return NULL; /* special case, if string starts with fs, * move past */ if ((s != NULL) && (*fld_ptr == fs)) fld_ptr++; for (n = 0; n < strlen(fld_ptr); n++) { if (fld_ptr[n] == fs) { if (!found_fld) { /* special case of 2 fs and * no field */ sav_fld_ptr = fld_ptr + n + 1; return ""; } break; } else found_fld = 1; } /* for strlen */ if (n == strlen(fld_ptr)) sav_fld_ptr = "\0"; else /* move past fs */ sav_fld_ptr = fld_ptr + n + 1; fld_ptr[n] = 0; return fld_ptr;} int fuse(parts, count)char ***parts;int count;{ int i; for (i = 0; i < count; i++) free((*parts)[i]); if (count > 0) free(*parts); *parts = 0; return 0;}/* ------------------------------------------------------------------- *//* ------------------------------------------------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -