Changeset 1236 for trunk/lib/lang/lang.c
- Timestamp:
- 07/11/11 04:39:08 (2 years ago)
- File:
-
- 1 edited
-
trunk/lib/lang/lang.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/lang/lang.c
r1234 r1236 4 4 //------------------------------------------------------------------- 5 5 6 // Array of offsets to each string stored in the 'sbuf' array 7 static unsigned short *strings = NULL; 6 static char **strings = NULL; 8 7 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 file12 // then another block was allocated for each string in the data read from the file, then the original13 // 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;16 8 17 9 //------------------------------------------------------------------- … … 19 11 int i; 20 12 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]); 24 16 ufree(strings); 25 strings = 0;26 17 count = 0; 27 18 } 28 19 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; 35 25 } 36 26 37 // Allocate offset buffer38 ++num;39 strings = umalloc(num*sizeof(unsigned short));40 if (strings) {41 memset(strings, 0, num*sizeof(unsigned short));42 count = num;43 }44 27 } 45 28 46 29 //------------------------------------------------------------------- 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) { 30 static void lang_add_string(int num, const char *str) { 51 31 int f=0; 52 32 char *p; 53 33 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); 57 37 if (p) { 58 38 for (; *str; ++str) { … … 75 55 76 56 //------------------------------------------------------------------- 77 // Parse the 'sbuf' memory and build the strings offset array 78 void lang_load_from_sbuf() 79 { 57 void lang_load_from_mem(char *buf) { 80 58 char *p, *s, *e; 81 59 int i; 82 60 83 e = sbuf-1;61 e = buf-1; 84 62 while(e) { 85 63 p = e+1; … … 104 82 105 83 //------------------------------------------------------------------- 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.133 84 void lang_load_from_file(const char *filename) { 134 85 int f, size; … … 140 91 size = (stat((char*)filename, &st)==0)?st.st_size:0; 141 92 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); 147 99 } 148 100 } … … 152 104 153 105 //------------------------------------------------------------------- 154 // Return the string corresponding to the 'str' index.155 106 char* lang_str(int str) { 156 107 if (str && str<0x1000) { 157 return (strings && str<count && strings[str])?s buf+strings[str]:"";108 return (strings && str<count && strings[str])?strings[str]:""; 158 109 } else { // not ID, just char* 159 110 return (char*)str;
Note: See TracChangeset
for help on using the changeset viewer.