source: trunk/lib/lang/lang.c @ 1236

Revision 1236, 2.8 KB checked in by reyalp, 2 years ago (diff)

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

  • Property svn:eol-style set to native
Line 
1#include "stdlib.h"
2#include "lang.h"
3
4//-------------------------------------------------------------------
5
6static char **strings = NULL;
7static int count = 0;
8
9//-------------------------------------------------------------------
10void lang_init(int num) {
11    int i;
12
13    if (strings) {
14       for (i=0; i<count; ++i)
15           if (strings[i]) ufree(strings[i]);
16       ufree(strings);
17       count = 0;
18    }
19
20    ++num;
21    strings = umalloc(num*sizeof(char*));
22    if (strings) {
23        memset(strings, 0, num*sizeof(char*));
24        count = num;
25    }
26
27}
28
29//-------------------------------------------------------------------
30static void lang_add_string(int num, const char *str) {
31    int f=0;
32    char *p;
33
34    if (strings && num<count) {
35       if (strings[num]) ufree(strings[num]);
36       p = strings[num] = umalloc(strlen(str)+1);
37       if (p) {
38           for (; *str; ++str) {
39                if (f) {
40                    if (*str == '"' || *str == '\\') *(p-1)=*str;
41                    else if (*str == 'n') *(p-1)='\n';
42                    else *p++=*str;
43                    f = 0;
44                } else {
45                    *p++=*str;
46                    if (*str == '\\') {
47                        f = 1;
48                    }
49                }
50           }
51           *p=0;
52       }
53    }
54}
55
56//-------------------------------------------------------------------
57void lang_load_from_mem(char *buf) {
58    char *p, *s, *e;
59    int i;
60   
61    e = buf-1;
62    while(e) {
63        p = e+1;
64        while (*p && (*p=='\r' || *p=='\n')) ++p;
65        i = strtol(p, &e, 0);
66        if (e!=p) {
67            p = e;
68            e = strpbrk(p, "\r\n");
69            if (e) *e=0;
70            while (*p && *p!='\"') ++p;
71            if (*p) ++p;
72            s = p;
73            while (*p && (*p!='\"' || *(p-1)=='\\')) ++p;
74            *p=0;
75            lang_add_string(i, s);
76        } else { //skip invalid line
77            e = strpbrk(p, "\r\n");
78            if (e) *e=0;
79        }
80    }
81}
82
83//-------------------------------------------------------------------
84void lang_load_from_file(const char *filename) {
85    int f, size;
86    static struct stat st;
87    char *buf;
88
89    f = open(filename, O_RDONLY, 0777);
90    if (f>=0) {
91        size = (stat((char*)filename, &st)==0)?st.st_size:0;
92        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);
99            }
100        }
101        close(f);
102    }
103}
104
105//-------------------------------------------------------------------
106char* lang_str(int str) {
107    if (str && str<0x1000) {
108        return (strings && str<count && strings[str])?strings[str]:"";
109    } else { // not ID, just char*
110        return (char*)str;
111    }
112}
Note: See TracBrowser for help on using the repository browser.