File: bldRes.c

package info (click to toggle)
mtink 1.0.16-10
  • links: PTS
  • area: main
  • in suites: buster
  • size: 3,808 kB
  • sloc: ansic: 19,264; sh: 1,008; python: 626; xml: 444; makefile: 76
file content (204 lines) | stat: -rw-r--r-- 4,595 bytes parent folder | download | duplicates (9)
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
/* file bldRes.c
 * build c-resource file
 *
 * read printer description file and fill the
 * printer knowledge data base
 *
 *  Copyrights: Jean-Jacques Sarton  j.sarton@t-online.de
 */
/*
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

void printHead()
{
   printf("#include \"version.h\"\nchar *fallbackResources[] = {\n");
}

void printTrailer()
{
   printf("\n   (char *)0\n};\n");
}

void searchVersionPart(char *s, char **b, char **e)
{
   *b = *e = NULL;
   while ( *s )
   {
      if ( s[0] == 'V' && s[1] == ' ' && isdigit(s[2]) )
      {
         *b = s;
         s += 2;
         while ( isdigit(*s) || *s == '.' )
            s++;
         *e = s;
         return;
      }
      s++;
   }
}

void translate(FILE *fp, int utf)
{
   int   inComment = 0;
   int   inText   = 0;
   char  buf[2048];
   char *s;
   char *b, *e;

   /* read the resource file line by line and build the c file */
   while ( fgets(buf, sizeof(buf), fp) )
   {
      s = buf;
      searchVersionPart(s, &b, &e);
      s = buf;
      if ( ! inText )
         while ( *s && (*s == ' ' || *s == '\t') ) s++;
      if ( !inComment && ! inText && *s == '!' )
      {
         inComment = 1;
         printf("#if 0\n/*\n   %s", s);
      }
      else if ( inComment && *s == '\n' )
      {
         printf("\n");
      }
      else if ( inComment && *s == '!' )
      {
         printf("   %s", s);
      }
      else if ( inComment )
      {
         printf("*/\n#endif\n");
         inComment = 0;
      }
      
      if ( ! inComment )
      {
         if ( *s == '\n' && ! inText )
         {
            printf("\n");
            continue;
         }

         inText = 1;
         printf("   \"");
         while ( *s )
         {
            if ( s == b )
            {
               printf("\"VERSION");
               s += e-b;
               if ( *s == '\n' )
               {
                  printf(",\n");
                  break;
               }
               else
               {
                  printf("\"");
               }
            }
            if ( s == buf && s[0] == '.' && s[3] == '.' && utf )
            {
               printf("%c",*s); // .
               s++;
               printf("%c",*s); // D
               s++;
               printf("%c",*s); // e
               s++;
               printf("8");     // add 8
               printf("%c",*s); // .
               s++;
            }
            
            switch (*s)
            {
               case '"':
                  printf("\\\"");
               break;
               case '\\':
                  if ( s[1] == '\n' )
                  {
                     s++; s++;
                     printf("\"\n");
                     continue;
                  }
                  else if ( s[1] == '"' )
                  {
                     printf("\\\"");
                     s++;
                  }
                  else
                  {
                     printf("\\\\");
                  } 
               break;
               case '\n':
                  printf("\",\n");
                  inText = 0;
               break;
               default:
                  printf("%c",*s);
            }
            s++;
         }
      }
   }
}

int main(int argc, char **argv)
{
   FILE *fp;
   int   utf;
   
   argc--;
   argv++;
   
   if ( argc )
   {
      printHead();
      while ( argc > 0 )
      {
         if ( (fp = fopen(argv[0], "r")) != NULL )
         {
            if ( strstr(argv[0],"utf8") )
               utf = 1;
            else
               utf = 0;
            translate(fp, utf);
            fclose(fp);
         }
         argc--;
         argv++;
      }
      printTrailer();
   }
   else
   {
      printHead();
      translate(stdin,0);
      printTrailer();
   }
   exit(0);
   return 0;
}