U_CAPI void U_EXPORT2 u_parseDelimitedFile(const char *filename, char delimiter, char *fields[][2], int32_t fieldCount, UParseLineFn *lineFn, void *context, UErrorCode *pErrorCode) { FileStream *file; char line[300]; char *start, *limit; int32_t i, length; if(U_FAILURE(*pErrorCode)) { return; } if(fields==NULL || lineFn==NULL || fieldCount<=0) { *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return; } if(filename==NULL || *filename==0 || (*filename=='-' && filename[1]==0)) { filename=NULL; file=T_FileStream_stdin(); } else { file=T_FileStream_open(filename, "r"); } if(file==NULL) { *pErrorCode=U_FILE_ACCESS_ERROR; return; } while(T_FileStream_readLine(file, line, sizeof(line))!=NULL) { /* remove trailing newline characters */ length=(int32_t)(u_rtrim(line)-line); /* * detect a line with # @missing: * start parsing after that, or else from the beginning of the line * set the default warning for @missing lines */ start=(char *)getMissingLimit(line); if(start==line) { *pErrorCode=U_ZERO_ERROR; } else { *pErrorCode=U_USING_DEFAULT_WARNING; } /* skip this line if it is empty or a comment */ if(*start==0 || *start=='#') { continue; } /* remove in-line comments */ limit=uprv_strchr(start, '#'); if(limit!=NULL) { /* get white space before the pound sign */ while(limit>start && U_IS_INV_WHITESPACE(*(limit-1))) { --limit; } /* truncate the line */ *limit=0; } /* skip lines with only whitespace */ if(u_skipWhitespace(start)[0]==0) { continue; } /* for each field, call the corresponding field function */ for(i=0; i<fieldCount; ++i) { /* set the limit pointer of this field */ limit=start; while(*limit!=delimiter && *limit!=0) { ++limit; } /* set the field start and limit in the fields array */ fields[i][0]=start; fields[i][1]=limit; /* set start to the beginning of the next field, if any */ start=limit; if(*start!=0) { ++start; } else if(i+1<fieldCount) { *pErrorCode=U_PARSE_ERROR; limit=line+length; i=fieldCount; break; } } /* error in a field function? */ if(U_FAILURE(*pErrorCode)) { break; } /* call the field function */ lineFn(context, fields, fieldCount, pErrorCode); if(U_FAILURE(*pErrorCode)) { break; } } if(filename!=NULL) { T_FileStream_close(file); } }
U_CAPI void U_EXPORT2 u_parseDelimitedFile(const char *filename, char delimiter, char *fields[][2], int32_t fieldCount, UParseLineFn *lineFn, void *context, UErrorCode *pErrorCode) { FileStream *file; char line[300]; char *start, *limit; int32_t i, length; if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { return; } if(fields==NULL || lineFn==NULL || fieldCount<=0) { *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return; } if(filename==NULL || *filename==0 || (*filename=='-' && filename[1]==0)) { filename=NULL; file=T_FileStream_stdin(); } else { file=T_FileStream_open(filename, "r"); } if(file==NULL) { *pErrorCode=U_FILE_ACCESS_ERROR; return; } while(T_FileStream_readLine(file, line, sizeof(line))!=NULL) { length=(int32_t)uprv_strlen(line); /* remove trailing newline characters */ while(length>0 && (line[length-1]=='\r' || line[length-1]=='\n')) { line[--length]=0; } /* skip this line if it is empty or a comment */ if(line[0]==0 || line[0]=='#') { continue; } /* remove in-line comments */ limit=uprv_strchr(line, '#'); if(limit!=NULL) { /* get white space before the pound sign */ while(limit>line && (*(limit-1)==' ' || *(limit-1)=='\t')) { --limit; } /* truncate the line */ *limit=0; } /* skip lines with only whitespace */ if(u_skipWhitespace(line)[0]==0) { continue; } /* for each field, call the corresponding field function */ start=line; for(i=0; i<fieldCount; ++i) { /* set the limit pointer of this field */ limit=start; while(*limit!=delimiter && *limit!=0) { ++limit; } /* set the field start and limit in the fields array */ fields[i][0]=start; fields[i][1]=limit; /* set start to the beginning of the next field, if any */ start=limit; if(*start!=0) { ++start; } else if(i+1<fieldCount) { *pErrorCode=U_PARSE_ERROR; limit=line+length; i=fieldCount; break; } } /* error in a field function? */ if(U_FAILURE(*pErrorCode)) { break; } /* call the field function */ lineFn(context, fields, fieldCount, pErrorCode); if(U_FAILURE(*pErrorCode)) { break; } } if(filename!=NULL) { T_FileStream_close(file); } }