Changeset 8774
- Timestamp:
- Sep 7, 2006, 4:13:20 PM (20 years ago)
- Location:
- trunk/psLib
- Files:
-
- 1 added
- 4 edited
-
src/types/psMetadataConfig.c (modified) (37 diffs)
-
src/types/psMetadataConfig.h (modified) (2 diffs)
-
test/types/Makefile.am (modified) (1 diff)
-
test/types/tap_psMetadataConfigParse.c (modified) (1 diff)
-
test/types/tap_psMetadataConfigParse_time.c (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/types/psMetadataConfig.c
r8771 r8774 10 10 * @author Eric Van Alst, MHPCC 11 11 * 12 * @version $Revision: 1. 79$ $Name: not supported by cvs2svn $13 * @date $Date: 2006-09-0 7 22:02:47$12 * @version $Revision: 1.80 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2006-09-08 02:13:20 $ 14 14 * 15 15 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 22 22 #include <inttypes.h> 23 23 #include <strings.h> 24 #include <sys/mman.h> 25 #include <sys/types.h> 26 #include <sys/stat.h> 27 #include <fcntl.h> 28 #include <unistd.h> 24 29 25 30 #include "psAbort.h" … … 69 74 ; 70 75 static void parseLevelInfoFree(p_psParseLevelInfo* info); 71 static psBool parseLine(psS32* level, psArray* levelArray, char* linePtr, psS32 lineCount, 72 char* fileName, psBool overwrite); 73 static psBool parseMetadataItem(char* keyName, psS32* level, psArray* levelArray, char* linePtr, 74 psS32 lineCount, char* fileName, psMetadataFlags flags); 76 static psBool parseLine(psS32* level, psArray* levelArray, char* linePtr, psS32 lineCount, psBool overwrite); 77 static psBool parseMetadataItem(char* keyName, psS32* level, psArray* levelArray, char* linePtr, psS32 lineCount, psMetadataFlags flags); 75 78 static psString formatMetadataItem(psMetadataItem *item); 76 79 static psArray *p_psMetadataKeyArray(psMetadata *md); … … 301 304 } 302 305 303 psTime *out = psTimeFromISO(inString, tt);304 306 // like psTimeFromISO() but the ending Z is required 307 psTime *out = psTimeStrptime(inString, "%Y-%m-%dT%H:%M:%SZ"); 305 308 if (!out) { 306 *status = 0;309 *status = 1; 307 310 return NULL; 308 311 } 309 312 310 *status = 1; 313 // psTimeStrptime() leaves the type at the default 314 out->type = tt; 315 316 *status = 0; 311 317 312 318 return out; … … 662 668 char* linePtr, 663 669 psS32 lineCount, 664 char* fileName,665 670 psMetadataFlags flags) 666 671 { … … 694 699 // Check for no type 695 700 if(strType==NULL) { 696 psError(PS_ERR_IO, true, _("Failed to read a metadata %s on line %u of %s."), "type",lineCount, 697 fileName); 701 psError(PS_ERR_IO, true, _("Failed to read a metadata %s on line %u."), "type",lineCount); 698 702 returnValue = false; 699 703 } else { … … 726 730 vectorType = PS_TYPE_F64; 727 731 } else { 728 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u of %s."), "", keyName,729 strType, lineCount , fileName);732 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u."), "", keyName, 733 strType, lineCount); 730 734 psFree(strType); 731 735 return false; … … 755 759 } else if(!strncmp(strType, "METADATA", 8)) { 756 760 mdType = PS_DATA_METADATA; 757 } else if( !strncmp(strType, " PS_TIME_UTC", 12) || !strncmp(strType, "PS_TIME_TAI", 12)758 || !strncmp(strType, " PS_TIME_UT1", 12) || !strncmp(strType, "PS_TIME_TT", 12)) {761 } else if( !strncmp(strType, "UTC", 3) || !strncmp(strType, "TAI", 3) 762 || !strncmp(strType, "UT1", 3) || !strncmp(strType, "TT", 3)) { 759 763 mdType = PS_DATA_TIME; 760 764 } else { … … 771 775 // Check for add failure 772 776 if (! addStatus) { 773 psError(PS_ERR_IO, true, _("Duplicate Metadata item, %s, found on line %u of %s. Overwrite not allowed."),774 keyName, lineCount , fileName);777 psError(PS_ERR_IO, true, _("Duplicate Metadata item, %s, found on line %u. Overwrite not allowed."), 778 keyName, lineCount); 775 779 psFree(strType); 776 780 psFree(tempMeta); … … 780 784 } else { 781 785 // Metadata type read error 782 psError(PS_ERR_IO,true,_("Failed to read a metadata %s on line %u of %s."),783 keyName, lineCount,fileName);786 psError(PS_ERR_IO,true,_("Failed to read a metadata %s on line %u."), 787 keyName, lineCount); 784 788 psFree(strType); 785 789 return false; … … 790 794 } 791 795 if(!typeFound) { 792 psError(PS_ERR_IO, true, _("Metadata type '%s', found on line %u of %s, is invalid."),793 strType, lineCount ,fileName);796 psError(PS_ERR_IO, true, _("Metadata type '%s', found on line %u, is invalid."), 797 strType, lineCount); 794 798 psFree(strType); 795 799 return false; … … 807 811 strValue = getToken(&linePtr, "#", &status,true); 808 812 if(status) { 809 psError(PS_ERR_IO, true, _("Failed to read a metadata %s on line %u of %s."), "value", lineCount, 810 fileName); 813 psError(PS_ERR_IO, true, _("Failed to read a metadata %s on line %u."), "value", lineCount); 811 814 psFree(strType); 812 815 psFree(strValue); … … 814 817 } 815 818 if(strValue==NULL) { 816 psError(PS_ERR_IO, true, _("Failed to read a metadata %s on line %u of %s."), "value", lineCount, 817 fileName); 819 psError(PS_ERR_IO, true, _("Failed to read a metadata %s on line %u."), "value", lineCount); 818 820 psFree(strType); 819 821 psFree(strValue); … … 824 826 strComment = getToken(&linePtr,"~", &status,true); 825 827 if(status) { 826 psError(PS_ERR_IO, true, _("Failed to read a metadata %s on line %u of %s."), "comment", lineCount, 827 fileName); 828 psError(PS_ERR_IO, true, _("Failed to read a metadata %s on line %u."), "comment", lineCount); 828 829 psFree(strType); 829 830 psFree(strValue); … … 849 850 strComment, tempBool); 850 851 } else { 851 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u of %s."), strValue, keyName,852 strType, lineCount , fileName);852 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u."), strValue, keyName, 853 strType, lineCount); 853 854 returnValue = false; 854 855 } … … 863 864 } else { 864 865 psError(PS_ERR_IO, true, 865 _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u of %s."), strValue, keyName, strType, lineCount, 866 fileName); 866 _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u."), strValue, keyName, strType, lineCount); 867 867 returnValue = false; 868 868 } … … 873 873 addStatus = psMetadataAdd(md, PS_LIST_TAIL, keyName, mdType | flags, strComment, tempInt); 874 874 } else { 875 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u of %s."), strValue, keyName,876 strType, lineCount , fileName);875 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u."), strValue, keyName, 876 strType, lineCount); 877 877 returnValue = false; 878 878 } … … 883 883 addStatus = psMetadataAdd(md, PS_LIST_TAIL, keyName, mdType | flags, strComment, tempInt); 884 884 } else { 885 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u of %s."), strValue, keyName,886 strType, lineCount , fileName);885 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u."), strValue, keyName, 886 strType, lineCount); 887 887 returnValue = false; 888 888 } … … 893 893 addStatus = psMetadataAdd(md, PS_LIST_TAIL, keyName, mdType | flags, strComment, tempInt); 894 894 } else { 895 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u of %s."), strValue, keyName,896 strType, lineCount , fileName);895 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u."), strValue, keyName, 896 strType, lineCount); 897 897 returnValue = false; 898 898 } … … 903 903 addStatus = psMetadataAdd(md, PS_LIST_TAIL, keyName, mdType | flags, strComment, tempUint); 904 904 } else { 905 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u of %s."), strValue, keyName,906 strType, lineCount , fileName);905 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u."), strValue, keyName, 906 strType, lineCount); 907 907 returnValue = false; 908 908 } … … 913 913 addStatus = psMetadataAdd(md, PS_LIST_TAIL, keyName, mdType | flags, strComment, tempUint); 914 914 } else { 915 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u of %s."), strValue, keyName,916 strType, lineCount , fileName);915 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u."), strValue, keyName, 916 strType, lineCount); 917 917 returnValue = false; 918 918 } … … 923 923 addStatus = psMetadataAdd(md, PS_LIST_TAIL, keyName, mdType | flags, strComment, tempUint); 924 924 } else { 925 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u of %s."), strValue, keyName,926 strType, lineCount , fileName);925 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u."), strValue, keyName, 926 strType, lineCount); 927 927 returnValue = false; 928 928 } 929 929 break; 930 930 case PS_DATA_TIME: 931 if( !strncmp(strType, "PS_TIME_UTC", 12) )931 if(!strncmp(strType, "UTC", 3)) { 932 932 timeType = PS_TIME_UTC; 933 else if( !strncmp(strType, "PS_TIME_TAI", 12) )933 } else if(!strncmp(strType, "TAI", 3)) { 934 934 timeType = PS_TIME_TAI; 935 else if( !strncmp(strType, "PS_TIME_UT1", 12) )935 } else if(!strncmp(strType, "UT1", 3)) { 936 936 timeType = PS_TIME_UT1; 937 else if( !strncmp(strType, "PS_TIME_TT", 12) )937 } else if(!strncmp(strType, "TT", 3)) { 938 938 timeType = PS_TIME_TT; 939 } 940 939 941 mTime = parseTime(strValue, timeType, &status); 940 942 if(!status) { … … 943 945 strComment, mTime); 944 946 } else { 945 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u of %s."), strValue, keyName,946 strType, lineCount , fileName);947 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u."), strValue, keyName, 948 strType, lineCount); 947 949 returnValue = false; 948 950 } … … 956 958 strComment, tempVec); 957 959 } else { 958 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u of %s."), strValue, keyName,959 strType, lineCount , fileName);960 psError(PS_ERR_IO, true, _("Failed to parse the value '%s' of metadata item %s, type %s, on line %u."), strValue, keyName, 961 strType, lineCount); 960 962 returnValue = false; 961 963 } … … 968 970 for(psS32 k=0; k < nonUniqueKeys->n; k++) { 969 971 if(strcmp(keyName,(char*)nonUniqueKeys->data[k]) == 0) { 970 psError(PS_ERR_IO,true,_("Duplicate MULTI specifier on line %u of %s."),971 lineCount ,fileName);972 psError(PS_ERR_IO,true,_("Duplicate MULTI specifier on line %u."), 973 lineCount); 972 974 psFree(strType); 973 975 return false; … … 994 996 break; 995 997 default: 996 psError(PS_ERR_IO,true,_("Metadata of unknown type found on line %u of %s."),997 lineCount ,fileName);998 psError(PS_ERR_IO,true,_("Metadata of unknown type found on line %u."), 999 lineCount); 998 1000 break; 999 1001 } … … 1015 1017 char* linePtr, 1016 1018 psS32 lineCount, 1017 char* fileName,1018 1019 psBool overwrite) 1019 1020 { … … 1042 1043 if(repeatedChars(linePtr, '@') > 1) { 1043 1044 psError(PS_ERR_IO, true, 1044 _("More than one '%c' character not allowed. Found on line %u of %s."), '@', lineCount, fileName);1045 _("More than one '%c' character not allowed. Found on line %u."), '@', lineCount); 1045 1046 return false; 1046 1047 } … … 1050 1051 if(status) { 1051 1052 psError(PS_ERR_IO, true, 1052 _("Failed to read a metadata %s on line %u of %s."), "keyName", lineCount, fileName);1053 _("Failed to read a metadata %s on line %u."), "keyName", lineCount); 1053 1054 psFree(keyName); 1054 1055 return false; … … 1060 1061 strType = getToken(&linePtr," ",&status,true); 1061 1062 if(strType == NULL) { 1062 psError(PS_ERR_IO,true,_("Failed to read a metadata %s on line %u of %s."),"type",lineCount, 1063 fileName); 1063 psError(PS_ERR_IO,true,_("Failed to read a metadata %s on line %u."),"type",lineCount); 1064 1064 psFree(keyName); 1065 1065 return false; … … 1074 1074 // Compare type name with the list of current types 1075 1075 if(strcmp(strType,(char*)typeArray->data[k]) == 0) { 1076 psError(PS_ERR_IO,true,_("Specified type, %s, on line %u of %sis already defined."),1077 strType,lineCount ,fileName);1076 psError(PS_ERR_IO,true,_("Specified type, %s, on line %u is already defined."), 1077 strType,lineCount); 1078 1078 psFree(tempTemplate); 1079 1079 psFree(keyName); … … 1089 1089 psFree(tempTemplate); 1090 1090 } else { 1091 psError(PS_ERR_IO,true,_("Metadata type '%s', found on line %u of %s, is invalid."),1092 strType,lineCount ,fileName);1091 psError(PS_ERR_IO,true,_("Metadata type '%s', found on line %u, is invalid."), 1092 strType,lineCount); 1093 1093 psFree(keyName); 1094 1094 psFree(strType); … … 1139 1139 } 1140 1140 // Parse metadataItem 1141 if(!parseMetadataItem(keyName,level, levelArray, linePtr, lineCount, f ileName, flags)) {1141 if(!parseMetadataItem(keyName,level, levelArray, linePtr, lineCount, flags)) { 1142 1142 psFree(keyName); 1143 1143 return false; … … 1149 1149 } 1150 1150 1151 psMetadata* psMetadataConfigRead(psMetadata* md, 1152 unsigned int *nFail, 1153 const char *filename, 1154 bool overwrite) 1155 { 1156 // Check for NULL file name 1157 PS_ASSERT_PTR_NON_NULL(filename,NULL); 1158 1159 struct stat buf; 1160 1161 // Attempt to open specified file 1162 int fd = 0; 1163 if (!(fd = open(filename, O_RDONLY))) { 1164 // XXX really should return strerror() here 1165 psError(PS_ERR_IO, true, _("Failed to open file '%s'. Check if it exists and it has the proper permissions."), filename); 1166 return NULL; 1167 } 1168 1169 // psMetadataConfigParse() is going to read the entire file into memory so 1170 // we're trying to be nice by allowing the VM to flush the file out of 1171 // memory if need be. 1172 // XXX yes, mmap is evil 1173 void *file = mmap(0, (size_t)buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 1174 1175 md = psMetadataConfigParse(md, nFail, (char *)file, overwrite); 1176 1177 munmap(file, (size_t)buf.st_size); 1178 1179 if (close(fd) != 0) { 1180 // XXX really should return strerror() here 1181 psError(PS_ERR_IO, true, _("Failed to close file '%s'."), filename); 1182 return NULL; 1183 } 1184 1185 return md; 1186 } 1187 1151 1188 psMetadata* psMetadataConfigParse(psMetadata* md, 1152 1189 unsigned int *nFail, 1153 const char * filename,1190 const char *str, 1154 1191 bool overwrite) 1155 1192 { 1156 FILE* fp = NULL;1157 char* line = NULL;1158 char* linePtr = NULL;1159 1193 psArray* parseLevelInfoArray = NULL; 1160 psS32 lineCount = 0;1161 1194 psS32 nestingLevel = 0; 1162 1195 p_psParseLevelInfo* topLevelInfo = NULL; 1163 1164 // Check for NULL file name1165 PS_ASSERT_PTR_NON_NULL(filename,NULL);1166 1196 1167 1197 // Initialise nFail, if provided … … 1170 1200 } 1171 1201 1172 // Attempt to open specified file1173 if((fp=fopen(filename, "r")) == NULL) {1174 psError(PS_ERR_IO, true, _("Failed to open file '%s'. Check if it exists and it has the proper permissions."), filename);1175 return NULL;1176 }1177 1178 1202 // Allocate metadata if necessary 1179 1203 if (md == NULL) { … … 1187 1211 topLevelInfo = p_psParseLevelInfoAlloc(); 1188 1212 topLevelInfo->metadata = psMemIncrRefCounter(md); 1189 parseLevelInfoArray = psArrayAdd(parseLevelInfoArray, 1,topLevelInfo);1213 parseLevelInfoArray = psArrayAdd(parseLevelInfoArray, 1, topLevelInfo); 1190 1214 psFree(topLevelInfo); 1191 1192 // Create reusable line for continuous read1193 line = (char*)psAlloc(MAX_STRING_LENGTH*sizeof(char));1194 1215 1195 1216 // While loop to parse the file 1196 1217 psErrorClear(); // so we can call psError(..., false, ...) 1197 while(fgets(line, MAX_STRING_LENGTH, fp) != NULL) { 1198 1199 // Initialize variables for new line 1200 linePtr = line; 1201 lineCount++; 1202 1203 if(!parseLine(&nestingLevel,parseLevelInfoArray,linePtr,lineCount,(char*)filename,overwrite)) { 1218 1219 psList *doc = psStringSplit(str, "\n", false); 1220 if (!doc) { 1221 psFree(parseLevelInfoArray); 1222 psFree(md); 1223 return NULL; 1224 } 1225 1226 long lineCount = 0; 1227 char *line = NULL; 1228 psListIterator *iter = psListIteratorAlloc(doc, 0, false); 1229 while((line = psListGetAndIncrement(iter))) { 1230 lineCount++; // indexed from 1 1231 1232 if(!parseLine(&nestingLevel, parseLevelInfoArray, line, lineCount, 1233 overwrite)) { 1204 1234 if (nFail != NULL) { 1205 1235 (*nFail)++; 1206 1236 } else { 1207 psError(PS_ERR_BAD_PARAMETER_VALUE, false, "Error parsing line: %s", linePtr); 1208 } 1209 } 1210 } 1211 fclose(fp); 1237 psError(PS_ERR_BAD_PARAMETER_VALUE, false, "Error parsing line: %s", line); 1238 } 1239 } 1240 } 1241 1242 psFree(iter); 1243 psFree(doc); 1212 1244 1213 1245 // Free parse array and line buffer 1214 1246 psFree(parseLevelInfoArray); 1215 psFree(line);1216 1247 1217 1248 return md; 1218 1249 } 1219 1220 1250 1221 1251 psString psMetadataConfigFormat(psMetadata *md) … … 1358 1388 psTime *time = item->data.V; 1359 1389 if ( time->type == PS_TIME_UTC ) 1360 psStringAppend(&content, " PS_TIME_UTC ");1390 psStringAppend(&content, "UTC "); 1361 1391 else if ( time->type == PS_TIME_TAI ) 1362 psStringAppend(&content, " PS_TIME_TAI ");1392 psStringAppend(&content, "TAI "); 1363 1393 else if ( time->type == PS_TIME_UT1 ) 1364 psStringAppend(&content, " PS_TIME_UT1 ");1394 psStringAppend(&content, "UT1 "); 1365 1395 else if ( time->type == PS_TIME_TT ) 1366 psStringAppend(&content, " PS_TIME_TT ");1396 psStringAppend(&content, "TT "); 1367 1397 else { 1368 1398 psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Specified psDataType, %d, is not supported."), time->type); -
trunk/psLib/src/types/psMetadataConfig.h
r8312 r8774 10 10 * @author Robert DeSonia, MHPCC 11 11 * 12 * @version $Revision: 1.2 1$ $Name: not supported by cvs2svn $13 * @date $Date: 2006-0 8-14 20:05:05$12 * @version $Revision: 1.22 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2006-09-08 02:13:20 $ 14 14 * 15 15 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 65 65 * @return psMetadata* : Resulting metadata from read. 66 66 */ 67 psMetadata* psMetadataConfigRead( 68 psMetadata* md, ///< Resulting metadata from read. 69 unsigned int *nFail, ///< Number of failed lines. 70 const char *filename, ///< Name of file to read. 71 bool overwrite ///< Allow overwrite of duplicate specifications. 72 ); 73 74 /** Parse metadata configuration string. 75 * 76 * Loads pre-defined settings by parsing a string into a psMetadata structure. 77 * 78 * @return psMetadata* : Resulting metadata from parse. 79 */ 67 80 psMetadata* psMetadataConfigParse( 68 81 psMetadata* md, ///< Resulting metadata from read. 69 82 unsigned int *nFail, ///< Number of failed lines. 70 const char * filename, ///< Name of file to read.83 const char *str, ///< String to process. 71 84 bool overwrite ///< Allow overwrite of duplicate specifications. 72 85 ); -
trunk/psLib/test/types/Makefile.am
r8747 r8774 16 16 tap_psListIterator \ 17 17 tap_psMetadataConfigParse \ 18 tap_psMetadataConfigParse_time \ 18 19 tap_psMetadataItemCompare \ 19 20 tap_psMetadataItemParse \ -
trunk/psLib/test/types/tap_psMetadataConfigParse.c
r8731 r8774 148 148 plan_tests(52); 149 149 150 diag("psMetadata ParseConfigtests");150 diag("psMetadataConfigRead tests"); 151 151 152 152 generateMDConfig(); 153 153 154 154 unsigned int numBadLines = 0; // Number of bad lines 155 psMetadata *md = psMetadataConfig Parse(NULL, &numBadLines, FILENAME, false);155 psMetadata *md = psMetadataConfigRead(NULL, &numBadLines, FILENAME, false); 156 156 ok(md, "md = %x", md); 157 157 ok(numBadLines == 1, "number of bad lines = %d", numBadLines); // One bad line from boolean 158 158 skip_start(!md || numBadLines != 1, 0, 159 "Skipping 0 tests because psMetadataConfig Parsefailed.");159 "Skipping 0 tests because psMetadataConfigRead failed."); 160 160 ok(psListLength(md->list) == 6, "size = %d", psListLength(md->list)); 161 161
Note:
See TracChangeset
for help on using the changeset viewer.
