~vcs-imports/lcms/main

« back to all changes in this revision

Viewing changes to src/cmscgats.c

  • Committer: mm2
  • Date: 2006-11-23 11:17:33 UTC
  • Revision ID: vcs-imports@canonical.com-20061123111733-8eaddbb2f5cd8374
no message

Show diffs side-by-side

added added

removed removed

Lines of Context:
108
108
 
109
109
#ifndef NON_WINDOWS
110
110
#include <io.h>
 
111
#define DIR_CHAR        '\\'
 
112
#else
 
113
#define DIR_CHAR        '/'
111
114
#endif
112
115
 
113
116
// Symbols
191
194
 
192
195
    } TABLE, *LPTABLE;
193
196
 
 
197
// File stream being parsed
194
198
 
 
199
typedef struct _FileContext {
 
200
        char           FileName[MAX_PATH];    // File name if being readed from file
 
201
        FILE*          Stream;                // File stream or NULL if holded in memory
 
202
        } FILECTX, *LPFILECTX;
195
203
 
196
204
// This struct hold all information about an openened
197
205
// IT8 handler. Only one dataset is allowed.
228
236
        char*          Source;                // Points to loc. being parsed
229
237
        int            lineno;                // line counter for error reporting
230
238
       
231
 
        char           FileName[MAX_PATH];    // File name if being readed from file
232
 
        FILE*          Stream[MAXINCLUDE];    // File stream or NULL if holded in memory
 
239
                LPFILECTX      FileStack[MAXINCLUDE]; // Stack of files being parsed
233
240
        int            IncludeSP;             // Include Stack Pointer
 
241
 
234
242
        char*          MemoryBlock;           // The stream if holded in memory
235
243
 
236
244
        char           DoubleFormatter[MAXID];   // Printf-like 'double' formatter
368
376
 
369
377
#define NUMPREDEFINEDSAMPLEID (sizeof(PredefinedSampleID)/sizeof(char *))
370
378
 
 
379
//Forward declaration of some internal functions                
 
380
static void* AllocChunk(LPIT8 it8, size_t size);
 
381
 
371
382
// Checks if c is a separator
372
383
static
373
384
BOOL isseparator(int c)
376
387
}
377
388
 
378
389
// Checks whatever if c is a valid identifier char
379
 
 
380
390
static 
381
391
BOOL ismiddle(int c)
382
392
{
397
407
     return !isdigit(c) && ismiddle(c);
398
408
}
399
409
 
400
 
 
 
410
// checks whether the supplied path looks like an absolute path
 
411
// NOTE: this function doesn't checks if the path exists or even if it's legal
 
412
static
 
413
BOOL isabsolutepath(const char *path)
 
414
{
 
415
        if(path == NULL)
 
416
                return FALSE;
 
417
        
 
418
    if(path[0] == DIR_CHAR)
 
419
                return TRUE;
 
420
 
 
421
#ifndef NON_WINDOWS
 
422
        if(isalpha(path[0]) && path[1] == ':')
 
423
                return TRUE;
 
424
#endif
 
425
        return FALSE;
 
426
}
 
427
 
 
428
// Makes a file path based on a given reference path
 
429
// NOTE: buffer is assumed to point to at least MAX_PATH bytes
 
430
// NOTE: both relPath and basePath are assumed to be no more than MAX_PATH characters long (including the null terminator!)
 
431
// NOTE: this function doesn't check if the path exists or even if it's legal
 
432
static 
 
433
BOOL makepath(const char *relPath, const char *basePath, char *buffer)
 
434
{
 
435
        if(!isabsolutepath(relPath))
 
436
        {
 
437
                char *tail;
 
438
                strcpy(buffer, basePath);
 
439
                tail = strrchr(buffer, DIR_CHAR);
 
440
                if (tail != NULL)
 
441
                {
 
442
                        size_t len = tail - buffer;
 
443
                        strncpy(tail + 1, relPath, MAX_PATH - len -1);
 
444
                        //      TODO: if combined path is longer than MAX_PATH, this should return FALSE!
 
445
                        return TRUE;
 
446
                }
 
447
        }
 
448
        strcpy(buffer, relPath);
 
449
        return TRUE;
 
450
}
 
451
 
 
452
// Syntax error
401
453
static
402
454
BOOL SynError(LPIT8 it8, const char *Txt, ...)
403
455
{
408
460
        vsprintf(Buffer, Txt, args);
409
461
        va_end(args);
410
462
 
411
 
        sprintf(ErrMsg, "%s: Line %d, %s", it8->FileName, it8->lineno, Buffer);
 
463
        sprintf(ErrMsg, "%s: Line %d, %s", it8->FileStack[it8 ->IncludeSP]->FileName, it8->lineno, Buffer);
412
464
        it8->sy = SSYNERROR;
413
465
        cmsSignalError(LCMS_ERRC_ABORTED, ErrMsg);
414
466
        return FALSE;
415
467
}
416
468
 
 
469
// Check if current symbol is same as specified. issue an error else.
417
470
static
418
471
BOOL Check(LPIT8 it8, SYMBOL sy, const char* Err)
419
472
{
428
481
static
429
482
void NextCh(LPIT8 it8)
430
483
{
431
 
    if (it8 -> Stream[it8 ->IncludeSP]) {
432
 
 
433
 
        it8 ->ch = fgetc(it8 ->Stream[it8 ->IncludeSP]);
434
 
 
435
 
        if (feof(it8 -> Stream[it8 ->IncludeSP]))  {
 
484
    if (it8 -> FileStack[it8 ->IncludeSP]->Stream) {
 
485
 
 
486
        it8 ->ch = fgetc(it8 ->FileStack[it8 ->IncludeSP]->Stream);
 
487
 
 
488
        if (feof(it8 -> FileStack[it8 ->IncludeSP]->Stream))  {
436
489
 
437
490
            if (it8 ->IncludeSP > 0) {
438
491
 
439
 
                fclose(it8 ->Stream[it8->IncludeSP--]);
 
492
                fclose(it8 ->FileStack[it8->IncludeSP--]->Stream);
440
493
                it8 -> ch = ' ';                            // Whitespace to be ignored
441
494
 
442
495
            } else
447
500
 
448
501
    }
449
502
    else {
450
 
 
451
503
        it8->ch = *it8->Source;
452
504
        if (it8->ch) it8->Source++;
453
505
    }
770
822
 
771
823
    if (it8 -> sy == SINCLUDE) {
772
824
 
773
 
                FILE* IncludeFile;
 
825
                LPFILECTX FileNest;
 
826
 
 
827
                                if(it8 -> IncludeSP >= (MAXINCLUDE-1))
 
828
                                {
 
829
                                        SynError(it8, "Too many recursion levels");
 
830
                                        return;
 
831
                                }
774
832
 
775
833
                InSymbol(it8);
776
834
                if (!Check(it8, SSTRING, "Filename expected")) return;
777
 
                IncludeFile = fopen(it8 -> str, "rt");
778
 
                if (IncludeFile == NULL) {
779
 
 
780
 
                        SynError(it8, "File %s not found", it8 ->str);
 
835
 
 
836
                                FileNest = it8 -> FileStack[it8 -> IncludeSP + 1];
 
837
                                if(FileNest == NULL)
 
838
                                {
 
839
                                        FileNest = it8 ->FileStack[it8 -> IncludeSP + 1] = (LPFILECTX)AllocChunk(it8, sizeof(FILECTX));
 
840
                                        //if(FileNest == NULL)
 
841
                                                //      TODO: how to manage out-of-memory conditions?
 
842
                                }
 
843
 
 
844
                                if(makepath(it8->str, it8->FileStack[it8->IncludeSP]->FileName, FileNest->FileName) == FALSE)
 
845
                                {
 
846
                                        SynError(it8, "File path too long");
 
847
                                        return;
 
848
                                }
 
849
 
 
850
                FileNest->Stream = fopen(FileNest->FileName, "rt");
 
851
                if (FileNest->Stream == NULL) {
 
852
 
 
853
                        SynError(it8, "File %s not found", FileNest->FileName);
781
854
                        return;
782
855
                }
 
856
                                it8->IncludeSP++;
783
857
 
784
 
                it8 -> Stream[++it8 -> IncludeSP] = IncludeFile;
785
858
                it8 ->ch = ' ';
786
859
                InSymbol(it8);    
787
860
    }
1093
1166
    AllocTable(it8);
1094
1167
    
1095
1168
    it8->MemoryBlock = NULL;
1096
 
    it8->Stream[0]   = NULL;
1097
 
    it8->IncludeSP   = 0;
1098
1169
    it8->MemorySink  = NULL;
1099
1170
    
1100
1171
    it8 ->nTable = 0;
1112
1183
    it8 -> inum = 0;
1113
1184
    it8 -> dnum = 0.0;
1114
1185
 
 
1186
        it8->FileStack[0] = (LPFILECTX)AllocChunk(it8, sizeof(FILECTX));
 
1187
    it8->IncludeSP   = 0;
1115
1188
    it8 -> lineno = 1;
1116
1189
 
1117
1190
    strcpy(it8->DoubleFormatter, DEFAULT_DBL_FORMAT);
1932
2005
 
1933
2006
        if (Buffer[i] == '\n' || Buffer[i] == '\r' || Buffer[i] == '\t') return TRUE;
1934
2007
        if (Buffer[i] < 32) return FALSE;
1935
 
       
 
2008
        if (Buffer[i] > 127) return FALSE;
1936
2009
    }
1937
2010
 
1938
2011
    return FALSE;
1980
2053
    strncpy(it8 ->MemoryBlock, (const char*) Ptr, len);
1981
2054
    it8 ->MemoryBlock[len] = 0;
1982
2055
 
1983
 
    strncpy(it8->FileName, "", MAX_PATH-1);
 
2056
    strncpy(it8->FileStack[0]->FileName, "", MAX_PATH-1);
1984
2057
    it8-> Source = it8 -> MemoryBlock;
1985
2058
 
1986
2059
    if (!ParseIT8(it8)) { 
2014
2087
     if (!hIT8) return NULL;
2015
2088
 
2016
2089
 
2017
 
     it8 ->Stream[0] = fopen(cFileName, "rt");
 
2090
     it8 ->FileStack[0]->Stream = fopen(cFileName, "rt");
2018
2091
 
2019
 
     if (!it8 ->Stream[0]) {         
 
2092
     if (!it8 ->FileStack[0]->Stream) {         
2020
2093
         cmsIT8Free(hIT8);
2021
2094
         return NULL;
2022
2095
     }
2023
2096
     
2024
2097
 
2025
 
    strncpy(it8->FileName, cFileName, MAX_PATH-1);    
 
2098
    strncpy(it8->FileStack[0]->FileName, cFileName, MAX_PATH-1);    
2026
2099
 
2027
2100
    if (!ParseIT8(it8)) { 
2028
2101
    
2029
 
            fclose(it8 ->Stream[0]);
 
2102
            fclose(it8 ->FileStack[0]->Stream);
2030
2103
            cmsIT8Free(hIT8); 
2031
2104
            return NULL; 
2032
2105
    }
2034
2107
    CookPointers(it8);
2035
2108
    it8 ->nTable = 0;
2036
2109
 
2037
 
    fclose(it8 ->Stream[0]);    
 
2110
    fclose(it8 ->FileStack[0]->Stream);    
2038
2111
    return hIT8;
2039
2112
 
2040
2113
}