📄 t1parse.c
字号:
#include <ftdebug.h>#include <t1types.h>#include <t1parse.h>#include <stdio.h> /* for sscanf *//*************************************************************************//* *//* <Function> T1_New_Table *//* *//* <Description> *//* Initialise a T1_Table. *//* *//* <Input> *//* table :: address of target table *//* count :: table size = maximum number of elements *//* memory :: memory object to use for all subsequent reallocations *//* *//* <Return> *//* Error code. 0 means success *//* */ LOCAL_FUNC T1_Error T1_New_Table( T1_Table* table, T1_Int count, FT_Memory memory ) { T1_Error error; table->memory = memory; if ( ALLOC_ARRAY( table->elements, count, T1_Byte* ) ) return error; if ( ALLOC_ARRAY( table->lengths, count, T1_Byte* ) ) { FREE( table->elements ); return error; } table->max_elems = count; table->num_elems = 0; table->block = 0; table->capacity = 0; table->cursor = 0; return error; }/*************************************************************************//* *//* <Function> T1_Add_Table *//* *//* <Description> *//* Adds an object to a T1_Table, possibly growing its memory block *//* *//* <Input> *//* table :: target table *//* index :: index of object in table *//* object :: address of object to copy in memory *//* length :: length in bytes of source object *//* *//* <Return> *//* Error code. 0 means success. An error is returned when a *//* realloc failed.. *//* */ static T1_Error reallocate_t1_table( T1_Table* table, T1_Int new_size ) { FT_Memory memory = table->memory; T1_Byte* old_base = table->block; T1_Error error; /* realloc the base block */ if ( REALLOC( table->block, table->capacity, new_size ) ) return error; table->capacity = new_size; /* shift all offsets when needed */ if (old_base) { T1_Long delta = table->block - old_base; T1_Byte** offset = table->elements; T1_Byte** limit = offset + table->max_elems; if (delta) for ( ; offset < limit; offset ++ ) if (offset[0]) offset[0] += delta; } return T1_Err_Ok; } LOCAL_FUNC T1_Error T1_Add_Table( T1_Table* table, T1_Int index, void* object, T1_Int length ) { if (index < 0 || index > table->max_elems) { FT_ERROR(( "T1.Add_Table: invalid index\n" )); return T1_Err_Syntax_Error; } /* grow the base block if needed */ if ( table->cursor + length > table->capacity ) { T1_Error error; T1_Int new_size = table->capacity; while ( new_size < table->cursor+length ) new_size += 1024; error = reallocate_t1_table( table, new_size ); if (error) return error; } /* add the object to the base block and adjust offset */ table->elements[ index ] = table->block + table->cursor; table->lengths [ index ] = length; MEM_Copy( table->block + table->cursor, object, length ); table->cursor += length; return T1_Err_Ok; }/*************************************************************************//* *//* <Function> T1_Done_Table *//* *//* <Description> *//* Finalise a T1_Table. (realloc it to its current cursor). *//* *//* <Input> *//* table :: target table *//* *//* <Note> *//* This function does NOT release the heap's memory block. It is up *//* to the caller to clean it, or reference it in its own structures. *//* */ LOCAL_FUNC void T1_Done_Table( T1_Table* table ) { FT_Memory memory = table->memory; T1_Error error; T1_Byte* old_base; /* should never fail, as rec.cursor <= rec.size */ old_base = table->block; if (!old_base) return; (void)REALLOC( table->block, table->capacity, table->cursor ); table->capacity = table->cursor; if (old_base != table->block) { T1_Long delta = table->block - old_base; T1_Byte** element = table->elements; T1_Byte** limit = element + table->max_elems; for ( ; element < limit; element++ ) if (element[0]) element[0] += delta; } } LOCAL_FUNC T1_String* CopyString( T1_Parser* parser ) { T1_String* string = NULL; T1_Token* token = parser->args++; FT_Memory memory = parser->tokenizer->memory; T1_Error error; if ( token->kind == tok_string ) { int len = token->len-2; if ( ALLOC( string, len+1 ) ) { parser->error = error; return 0; } MEM_Copy( string, parser->tokenizer->base + token->start+1, len ); string[len] = '\0'; parser->error = T1_Err_Ok; } else { FT_ERROR(( "T1.CopyString : syntax error, string token expected !\n" )); parser->error = T1_Err_Syntax_Error; } return string; } static T1_Error parse_int( T1_Byte* base, T1_Byte* limit, T1_Long* result ) { T1_Bool sign = 0; T1_Long sum = 0; if (base >= limit) goto Fail; /* check sign */ if ( *base == '+' ) base++; else if ( *base == '-' ) { sign++; base++; } /* parse digits */ if ( base >= limit ) goto Fail; do { sum = ( 10*sum + (*base++ - '0') ); } while (base < limit); if (sign) sum = -sum; *result = sum; return T1_Err_Ok; Fail: FT_ERROR(( "T1.parse_integer : integer expected\n" )); *result = 0; return T1_Err_Syntax_Error; } static T1_Error parse_float( T1_Byte* base, T1_Byte* limit, T1_Int scale, T1_Long* result ) {#if 1 /* XXX : We're simply much too lazy to code this function */ /* properly for now.. We'll do that when the rest of */ /* the driver works properly.. */ char temp[32]; int len = limit-base; double value; if (len > 31) goto Fail; strncpy( temp, (char*)base, len ); temp[len] = '\0'; if ( sscanf( temp, "%lf", &value ) != 1 ) goto Fail; *result = (T1_Long)(scale*value); return 0;#else T1_Byte* cur; T1_Bool sign = 0; /* sign */ T1_Long number_int = 0; /* integer part */ T1_Long number_frac = 0; /* fractional part */ T1_Long exponent = 0; /* exponent value */ T1_Int num_frac = 0; /* number of fractional digits */ /* check sign */ if (*base == '+') base++; else if (*base == '-') { sign++; base++; } /* find integer part */ cur = base; while ( cur < limit ) { T1_Byte c = *cur; if ( c == '.' || c == 'e' || c == 'E' ) break; cur++; } if ( cur > base ) { error = parse_integer( base, cur, &number_int ); if (error) goto Fail; } /* read fractional part, if any */ if ( *cur == '.' ) { cur++; base = cur; while ( cur < limit ) { T1_Byte c = *cur; if ( c == 'e' || c == 'E' ) break; cur++; } num_frac = cur - base; if ( cur > base ) { error = parse_integer( base, cur, &number_frac ); if (error) goto Fail;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -