Changeset 1234 for trunk/lib/lang/lang.c
- Timestamp:
- 07/11/11 01:57:16 (23 months ago)
- File:
-
- 1 edited
-
trunk/lib/lang/lang.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/lang/lang.c
r515 r1234 4 4 //------------------------------------------------------------------- 5 5 6 static char **strings = NULL; 6 // Array of offsets to each string stored in the 'sbuf' array 7 static unsigned short *strings = NULL; 7 8 static 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; 8 16 9 17 //------------------------------------------------------------------- … … 11 19 int i; 12 20 13 if (strings) {14 for (i=0; i<count; ++i)15 if (strings[i]) ufree(strings[i]);21 // Free old buffer (should not happen since this is only called once at startup) 22 if (strings) 23 { 16 24 ufree(strings); 25 strings = 0; 17 26 count = 0; 18 27 } 19 28 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; 35 } 36 37 // Allocate offset buffer 20 38 ++num; 21 strings = umalloc(num*sizeof( char*));39 strings = umalloc(num*sizeof(unsigned short)); 22 40 if (strings) { 23 memset(strings, 0, num*sizeof( char*));41 memset(strings, 0, num*sizeof(unsigned short)); 24 42 count = num; 25 43 } 26 27 44 } 28 45 29 46 //------------------------------------------------------------------- 30 static void lang_add_string(int num, const char *str) { 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) { 31 51 int f=0; 32 52 char *p; 33 53 34 if ( strings &&num<count) {35 if (strings[num]) ufree(strings[num]);36 p = strings[num] = umalloc(strlen(str)+1);54 if (num<count) { 55 p = str; 56 strings[num] = (unsigned short)(str - sbuf); 37 57 if (p) { 38 58 for (; *str; ++str) { … … 55 75 56 76 //------------------------------------------------------------------- 57 void lang_load_from_mem(char *buf) { 77 // Parse the 'sbuf' memory and build the strings offset array 78 void lang_load_from_sbuf() 79 { 58 80 char *p, *s, *e; 59 81 int i; 60 82 61 e = buf-1;83 e = sbuf-1; 62 84 while(e) { 63 85 p = e+1; … … 82 104 83 105 //------------------------------------------------------------------- 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. 84 133 void lang_load_from_file(const char *filename) { 85 134 int f, size; … … 91 140 size = (stat((char*)filename, &st)==0)?st.st_size:0; 92 141 if (size) { 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); 142 if (alloc_sbuf(size+1)) 143 { 144 size = read(f, sbuf, size); 145 sbuf[size]=0; 146 lang_load_from_sbuf(); 99 147 } 100 148 } … … 104 152 105 153 //------------------------------------------------------------------- 154 // Return the string corresponding to the 'str' index. 106 155 char* lang_str(int str) { 107 156 if (str && str<0x1000) { 108 return (strings && str<count && strings[str])?s trings[str]:"";157 return (strings && str<count && strings[str])?sbuf+strings[str]:""; 109 158 } else { // not ID, just char* 110 159 return (char*)str;
Note: See TracChangeset
for help on using the changeset viewer.