883 it8->lineno++;
884 break;
885
886 // Comment
887 case '#':
888 NextCh(it8);
889 while (it8->ch && it8->ch != '\n' && it8->ch != '\r')
890 NextCh(it8);
891
892 it8->sy = SCOMMENT;
893 break;
894
895 // String.
896 case '\'':
897 case '\"':
898 idptr = it8->str;
899 sng = it8->ch;
900 k = 0;
901 NextCh(it8);
902
903 while (k < MAXSTR && it8->ch != sng) {
904
905 if (it8->ch == '\n'|| it8->ch == '\r') k = MAXSTR+1;
906 else {
907 *idptr++ = (char) it8->ch;
908 NextCh(it8);
909 k++;
910 }
911 }
912
913 it8->sy = SSTRING;
914 *idptr = '\0';
915 NextCh(it8);
916 break;
917
918
919 default:
920 SynError(it8, "Unrecognized character: 0x%x", it8 ->ch);
921 return;
922 }
923
2036 break;
2037
2038
2039 case SEOLN: break;
2040
2041 default:
2042 return SynError(it8, "expected keyword or identifier");
2043 }
2044
2045 SkipEOLN(it8);
2046 }
2047
2048 return TRUE;
2049
2050 }
2051
2052
2053 static
2054 void ReadType(cmsIT8* it8, char* SheetTypePtr)
2055 {
2056 // First line is a very special case.
2057
2058 while (isseparator(it8->ch))
2059 NextCh(it8);
2060
2061 while (it8->ch != '\r' && it8 ->ch != '\n' && it8->ch != '\t' && it8 -> ch != -1) {
2062
2063 *SheetTypePtr++= (char) it8 ->ch;
2064 NextCh(it8);
2065 }
2066
2067 *SheetTypePtr = 0;
2068 }
2069
2070
2071 static
2072 cmsBool ParseIT8(cmsIT8* it8, cmsBool nosheet)
2073 {
2074 char* SheetTypePtr = it8 ->Tab[0].SheetType;
2075
2076 if (nosheet == 0) {
2077 ReadType(it8, SheetTypePtr);
2078 }
2079
2080 InSymbol(it8);
2081
2082 SkipEOLN(it8);
2083
2236 }
2237
2238
2239 }
2240
2241 }
2242
2243
2244 }
2245
2246 }
2247 }
2248
2249 it8 ->nTable = nOldTable;
2250 }
2251
2252 // Try to infere if the file is a CGATS/IT8 file at all. Read first line
2253 // that should be something like some printable characters plus a \n
2254 // returns 0 if this is not like a CGATS, or an integer otherwise. This integer is the number of words in first line?
2255 static
2256 int IsMyBlock(cmsUInt8Number* Buffer, int n)
2257 {
2258 int words = 1, space = 0, quot = 0;
2259 int i;
2260
2261 if (n < 10) return 0; // Too small
2262
2263 if (n > 132)
2264 n = 132;
2265
2266 for (i = 1; i < n; i++) {
2267
2268 switch(Buffer[i])
2269 {
2270 case '\n':
2271 case '\r':
2272 return ((quot == 1) || (words > 2)) ? 0 : words;
2273 case '\t':
2274 case ' ':
2275 if(!quot && !space)
2276 space = 1;
2300
2301 fp = fopen(FileName, "rt");
2302 if (!fp) {
2303 cmsSignalError(0, cmsERROR_FILE, "File '%s' not found", FileName);
2304 return FALSE;
2305 }
2306
2307 Size = (cmsUInt32Number) fread(Ptr, 1, 132, fp);
2308
2309 if (fclose(fp) != 0)
2310 return FALSE;
2311
2312 Ptr[Size] = '\0';
2313
2314 return IsMyBlock(Ptr, Size);
2315 }
2316
2317 // ---------------------------------------------------------- Exported routines
2318
2319
2320 cmsHANDLE CMSEXPORT cmsIT8LoadFromMem(cmsContext ContextID, void *Ptr, cmsUInt32Number len)
2321 {
2322 cmsHANDLE hIT8;
2323 cmsIT8* it8;
2324 int type;
2325
2326 _cmsAssert(Ptr != NULL);
2327 _cmsAssert(len != 0);
2328
2329 type = IsMyBlock((cmsUInt8Number*)Ptr, len);
2330 if (type == 0) return NULL;
2331
2332 hIT8 = cmsIT8Alloc(ContextID);
2333 if (!hIT8) return NULL;
2334
2335 it8 = (cmsIT8*) hIT8;
2336 it8 ->MemoryBlock = (char*) _cmsMalloc(ContextID, len + 1);
2337
2338 strncpy(it8 ->MemoryBlock, (const char*) Ptr, len);
2339 it8 ->MemoryBlock[len] = 0;
2340
2341 strncpy(it8->FileStack[0]->FileName, "", cmsMAX_PATH-1);
2342 it8-> Source = it8 -> MemoryBlock;
2343
2344 if (!ParseIT8(it8, type-1)) {
2345
2346 cmsIT8Free(hIT8);
2347 return FALSE;
2348 }
2349
|
883 it8->lineno++;
884 break;
885
886 // Comment
887 case '#':
888 NextCh(it8);
889 while (it8->ch && it8->ch != '\n' && it8->ch != '\r')
890 NextCh(it8);
891
892 it8->sy = SCOMMENT;
893 break;
894
895 // String.
896 case '\'':
897 case '\"':
898 idptr = it8->str;
899 sng = it8->ch;
900 k = 0;
901 NextCh(it8);
902
903 while (k < (MAXSTR-1) && it8->ch != sng) {
904
905 if (it8->ch == '\n'|| it8->ch == '\r') k = MAXSTR+1;
906 else {
907 *idptr++ = (char) it8->ch;
908 NextCh(it8);
909 k++;
910 }
911 }
912
913 it8->sy = SSTRING;
914 *idptr = '\0';
915 NextCh(it8);
916 break;
917
918
919 default:
920 SynError(it8, "Unrecognized character: 0x%x", it8 ->ch);
921 return;
922 }
923
2036 break;
2037
2038
2039 case SEOLN: break;
2040
2041 default:
2042 return SynError(it8, "expected keyword or identifier");
2043 }
2044
2045 SkipEOLN(it8);
2046 }
2047
2048 return TRUE;
2049
2050 }
2051
2052
2053 static
2054 void ReadType(cmsIT8* it8, char* SheetTypePtr)
2055 {
2056 cmsInt32Number cnt = 0;
2057
2058 // First line is a very special case.
2059
2060 while (isseparator(it8->ch))
2061 NextCh(it8);
2062
2063 while (it8->ch != '\r' && it8 ->ch != '\n' && it8->ch != '\t' && it8 -> ch != 0) {
2064
2065 *SheetTypePtr++= (char) it8 ->ch;
2066 if (cnt++ < MAXSTR)
2067 *SheetTypePtr++= (char) it8 ->ch;
2068 NextCh(it8);
2069 }
2070
2071 *SheetTypePtr = 0;
2072 }
2073
2074
2075 static
2076 cmsBool ParseIT8(cmsIT8* it8, cmsBool nosheet)
2077 {
2078 char* SheetTypePtr = it8 ->Tab[0].SheetType;
2079
2080 if (nosheet == 0) {
2081 ReadType(it8, SheetTypePtr);
2082 }
2083
2084 InSymbol(it8);
2085
2086 SkipEOLN(it8);
2087
2240 }
2241
2242
2243 }
2244
2245 }
2246
2247
2248 }
2249
2250 }
2251 }
2252
2253 it8 ->nTable = nOldTable;
2254 }
2255
2256 // Try to infere if the file is a CGATS/IT8 file at all. Read first line
2257 // that should be something like some printable characters plus a \n
2258 // returns 0 if this is not like a CGATS, or an integer otherwise. This integer is the number of words in first line?
2259 static
2260 int IsMyBlock(const cmsUInt8Number* Buffer, int n)
2261 {
2262 int words = 1, space = 0, quot = 0;
2263 int i;
2264
2265 if (n < 10) return 0; // Too small
2266
2267 if (n > 132)
2268 n = 132;
2269
2270 for (i = 1; i < n; i++) {
2271
2272 switch(Buffer[i])
2273 {
2274 case '\n':
2275 case '\r':
2276 return ((quot == 1) || (words > 2)) ? 0 : words;
2277 case '\t':
2278 case ' ':
2279 if(!quot && !space)
2280 space = 1;
2304
2305 fp = fopen(FileName, "rt");
2306 if (!fp) {
2307 cmsSignalError(0, cmsERROR_FILE, "File '%s' not found", FileName);
2308 return FALSE;
2309 }
2310
2311 Size = (cmsUInt32Number) fread(Ptr, 1, 132, fp);
2312
2313 if (fclose(fp) != 0)
2314 return FALSE;
2315
2316 Ptr[Size] = '\0';
2317
2318 return IsMyBlock(Ptr, Size);
2319 }
2320
2321 // ---------------------------------------------------------- Exported routines
2322
2323
2324 cmsHANDLE CMSEXPORT cmsIT8LoadFromMem(cmsContext ContextID, const void *Ptr, cmsUInt32Number len)
2325 {
2326 cmsHANDLE hIT8;
2327 cmsIT8* it8;
2328 int type;
2329
2330 _cmsAssert(Ptr != NULL);
2331 _cmsAssert(len != 0);
2332
2333 type = IsMyBlock((const cmsUInt8Number*)Ptr, len);
2334 if (type == 0) return NULL;
2335
2336 hIT8 = cmsIT8Alloc(ContextID);
2337 if (!hIT8) return NULL;
2338
2339 it8 = (cmsIT8*) hIT8;
2340 it8 ->MemoryBlock = (char*) _cmsMalloc(ContextID, len + 1);
2341
2342 strncpy(it8 ->MemoryBlock, (const char*) Ptr, len);
2343 it8 ->MemoryBlock[len] = 0;
2344
2345 strncpy(it8->FileStack[0]->FileName, "", cmsMAX_PATH-1);
2346 it8-> Source = it8 -> MemoryBlock;
2347
2348 if (!ParseIT8(it8, type-1)) {
2349
2350 cmsIT8Free(hIT8);
2351 return FALSE;
2352 }
2353
|