summaryrefslogtreecommitdiffstats
path: root/utils/skinupdater/skinupdater.c
blob: a7f7cfd96a765d824a53bb8e0ed32caacbf6a5a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "tag_table.h"

#define PUTCH(out, c) fprintf(out, "%c", c)
extern struct tag_info legal_tags[];
/* dump "count" args to output replacing '|' with ',' except after the last count.
 * return the amount of chars read. (start+return will be after the last | )
 */
int dump_arg(FILE* out, const char* start, int count, bool close)
{
    int l = 0;
    while (count)
    {
        if (start[l] == '|')
        {
            if (count > 1)
            {
                PUTCH(out, ',');
            } else if (close) {
                PUTCH(out, ')');
            }
            count--;
        } else {
            PUTCH(out, start[l]);
        }
        l++;
    }
    return l;
}
#define MATCH(s) (!strcmp(tag->name, s))
int parse_tag(FILE* out, const char* start)
{
    struct tag_info *tag;
    int len = 0;
    for(tag = legal_tags; 
        tag->name[0] && strncmp(start, tag->name, strlen(tag->name)) != 0;
        tag++) ;
    if (!tag->name[0])
        return -1;
    if (tag->params[0] == '\0')
    {
        fprintf(out, "%s", tag->name);
        return strlen(tag->name);
    }
    fprintf(out, "%s", tag->name);
    len += strlen(tag->name);
    start += len;
    /* handle individual tags which accept params */
    if (MATCH("bl") || MATCH("pb") || MATCH("pv"))
    {
        start++; len++;
        if (*start == '|')
        {
            PUTCH(out, '(');
            /* TODO: need to verify that we are actually using the long form... */
            len += dump_arg(out, start, 5, true);
        }
    }
    else if (MATCH("d") || MATCH("D") || MATCH("mv") || MATCH("pS") || MATCH("pE") || MATCH("t") || MATCH("Tl"))
    {
        char temp[8] = {'\0'};
        int i = 0;
        /* tags which maybe have a number after them */
        while ((*start >= '0' && *start <= '9') || *start == '.')
        {
            temp[i++] = *start++;
        }
        if (i!= 0)
        {
            fprintf(out, "(%s)", temp);
            len += i;
        }
    }
    else if (MATCH("xl"))
    {
        PUTCH(out, '(');
        len += 1+dump_arg(out, start+1, 5, true);
    }
    else if (MATCH("xd"))
    {
        /* NOTE: almost certainly needs work */
        PUTCH(out, '(');
        PUTCH(out, *start++); len++;
        if ((*start >= 'a' && *start <= 'z') ||
            (*start >= 'A' && *start <= 'Z'))
        PUTCH(out, *start); len++;
        PUTCH(out, ')');
    }
    else if (MATCH("x"))
    {
        PUTCH(out, '(');
        len += 1+dump_arg(out, start+1, 4, true);
    }
    else if (MATCH("Fl"))
    {
        PUTCH(out, '(');
        len += 1+dump_arg(out, start+1, 2, true);
    }
    else if (MATCH("Cl"))
    {
        PUTCH(out, '(');
        len += 1+dump_arg(out, start+1, 4, true);
    }
    else if (MATCH("Vd") || MATCH("VI"))
    {
        PUTCH(out, '(');
        PUTCH(out, *start); len++;
        PUTCH(out, ')');
    }
    else if (MATCH("Vp"))
    {
        /* NOTE: almost certainly needs work */
        PUTCH(out, '(');
        len += 1+dump_arg(out, start+1, 3, true);
    }
    else if (MATCH("Vl") || MATCH("Vi"))
    {
        PUTCH(out, '(');
        len += 1+dump_arg(out, start+1, 8, true);
    }
    else if (MATCH("V"))
    {
        PUTCH(out, '(');
        len += 1+dump_arg(out, start+1, 7, true);
    }
    else if (MATCH("X"))
    {
        if (*start+1 == 'd')
        {
            fprintf(out, "(d)");
            len ++;
        }
        else
        {
            PUTCH(out, '(');
            len += 1+dump_arg(out, start+1, 1, true);
        }
    }
    else if (MATCH("St") || MATCH("Sx"))
    {
        PUTCH(out, '(');
        len += 1+dump_arg(out, start+1, 1, true);
    }
    
    else if (MATCH("T"))
    {
        PUTCH(out, '(');
        len += 1+dump_arg(out, start+1, 5, true);
    }
    return len;
}
        
void parse_text(const char* in, FILE* out)
{
    const char* end = in+strlen(in);
top:
    while (in < end && *in)
    {
        if (*in == '%')
        {
            PUTCH(out, *in++);
            switch(*in)
            {

                case '%':
                case '<':
                case '|':
                case '>':
                case ';':
                case '#':
                    PUTCH(out, *in++);
                    goto top;
                    break;
                case '?':
                    PUTCH(out, *in++);
                    break;
            }
            in += parse_tag(out, in);
        }
        else if (*in == '#')
        {
            while (*in != '\n')
            {
                PUTCH(out, *in++);
            }
        }
        else 
        {
            PUTCH(out, *in++);
        }            
    }
}

int main(int argc, char* argv[])
{
    char buffer[10*1024], temp[512];
    FILE *in, *out = stdout;
    if( (argc < 2) ||
        strcmp(argv[1],"-h") == 0 ||
        strcmp(argv[1],"--help") == 0 )
    {
        printf("Usage: %s infile [outfile]\n", argv[0]);
        return 0;
    }
    in = fopen(argv[1], "r");
    if (!in)
        return 1;
    while (fgets(temp, 512, in))
        strcat(buffer, temp);
    
    if (argc == 3)
    {
        out = fopen(argv[2], "w");
        if (!out)
            return 1;
    }        
    
    parse_text(buffer, out);
    fclose(in);
    if (out != stdout)
        fclose(out);
    return 0;
}