Changeset 1236 for trunk/lib/lang/lang.c


Ignore:
Timestamp:
07/11/11 04:39:08 (2 years ago)
Author:
reyalp
Message:

roll back lib/lang/lang.c from r1234 by philmoz request in http://chdk.setepontos.com/index.php?topic=650.msg70128#msg70128

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/lang/lang.c

    r1234 r1236  
    44//------------------------------------------------------------------- 
    55 
    6 // Array of offsets to each string stored in the 'sbuf' array 
    7 static unsigned short *strings = NULL; 
     6static char **strings = NULL; 
    87static int count = 0; 
    9  
    10 // Strings stored in one large umalloc'ed array rather than individually (reduces memory overhead) 
    11 // Previously when loading a language file a block of memory was allocated to read the file 
    12 // then another block was allocated for each string in the data read from the file, then the original 
    13 // block was freed. This version uses the same buffer to read the file and store the strings. 
    14 static char *sbuf = NULL;    
    15 static int sbuflen = 0; 
    168 
    179//------------------------------------------------------------------- 
     
    1911    int i; 
    2012 
    21     // Free old buffer (should not happen since this is only called once at startup) 
    22     if (strings) 
    23     { 
     13    if (strings) { 
     14       for (i=0; i<count; ++i) 
     15           if (strings[i]) ufree(strings[i]); 
    2416       ufree(strings); 
    25        strings = 0; 
    2617       count = 0; 
    2718    } 
    2819 
    29     // Free old buffer (should not happen since this is only called once at startup) 
    30     if (sbuf) 
    31     { 
    32         ufree(sbuf); 
    33         sbuf = 0; 
    34         sbuflen = 0; 
     20    ++num; 
     21    strings = umalloc(num*sizeof(char*)); 
     22    if (strings) { 
     23        memset(strings, 0, num*sizeof(char*)); 
     24        count = num; 
    3525    } 
    3626 
    37     // Allocate offset buffer 
    38     ++num; 
    39     strings = umalloc(num*sizeof(unsigned short)); 
    40     if (strings) { 
    41         memset(strings, 0, num*sizeof(unsigned short)); 
    42         count = num; 
    43     } 
    4427} 
    4528 
    4629//------------------------------------------------------------------- 
    47 // Add a string to the buffer. String is cleaned up to convert special 
    48 // characters and the offset of the string in 'sbuf' is stored in the 
    49 // strings array. 
    50 static void lang_add_string(int num, char *str) { 
     30static void lang_add_string(int num, const char *str) { 
    5131    int f=0; 
    5232    char *p; 
    5333 
    54     if (num<count) { 
    55        p = str; 
    56        strings[num] = (unsigned short)(str - sbuf); 
     34    if (strings && num<count) { 
     35       if (strings[num]) ufree(strings[num]); 
     36       p = strings[num] = umalloc(strlen(str)+1); 
    5737       if (p) { 
    5838           for (; *str; ++str) { 
     
    7555 
    7656//------------------------------------------------------------------- 
    77 // Parse the 'sbuf' memory and build the strings offset array 
    78 void lang_load_from_sbuf() 
    79 { 
     57void lang_load_from_mem(char *buf) { 
    8058    char *p, *s, *e; 
    8159    int i; 
    8260     
    83     e = sbuf-1; 
     61    e = buf-1; 
    8462    while(e) { 
    8563        p = e+1; 
     
    10482 
    10583//------------------------------------------------------------------- 
    106 // Allocate a new 'sbuf' array if needed. 
    107 // If the existing one is large enough use it instead of getting a new block of memory. 
    108 int alloc_sbuf(int len) 
    109 { 
    110     if (len > sbuflen) 
    111     { 
    112         if (sbuf) ufree(sbuf); 
    113         sbuf = umalloc(len); 
    114         if (sbuf) 
    115         { 
    116             sbuflen = len; 
    117         } 
    118     } 
    119     return (sbuf != 0); 
    120 } 
    121 //------------------------------------------------------------------- 
    122 // Load the default language data from memory. 
    123 void lang_load_from_mem(char *buf) { 
    124     if (alloc_sbuf(strlen(buf)+1)) 
    125     { 
    126         memcpy(sbuf,buf,sbuflen); 
    127         lang_load_from_sbuf(); 
    128     } 
    129 } 
    130  
    131 //------------------------------------------------------------------- 
    132 // Load language data from a file. 
    13384void lang_load_from_file(const char *filename) { 
    13485    int f, size; 
     
    14091        size = (stat((char*)filename, &st)==0)?st.st_size:0; 
    14192        if (size) { 
    142             if (alloc_sbuf(size+1)) 
    143             { 
    144                 size = read(f, sbuf, size); 
    145                 sbuf[size]=0; 
    146                 lang_load_from_sbuf(); 
     93            buf = umalloc(size+1); 
     94            if (buf) { 
     95                size = read(f, buf, size); 
     96                buf[size]=0; 
     97                lang_load_from_mem(buf); 
     98                ufree(buf); 
    14799            } 
    148100        } 
     
    152104 
    153105//------------------------------------------------------------------- 
    154 // Return the string corresponding to the 'str' index. 
    155106char* lang_str(int str) { 
    156107    if (str && str<0x1000) { 
    157         return (strings && str<count && strings[str])?sbuf+strings[str]:""; 
     108        return (strings && str<count && strings[str])?strings[str]:""; 
    158109    } else { // not ID, just char* 
    159110        return (char*)str; 
Note: See TracChangeset for help on using the changeset viewer.