Imported Upstream version 0.19.5
[platform/upstream/fribidi.git] / gen.tab / gen-unicode-version.c
1 /* FriBidi
2  * gen-unicode-version.c - generate fribidi-unicode-version.h
3  *
4  * $Id: gen-unicode-version.c,v 1.12 2006-01-31 03:23:12 behdad Exp $
5  * $Author: behdad $
6  * $Date: 2006-01-31 03:23:12 $
7  * $Revision: 1.12 $
8  * $Source: /home/behdad/src/fdo/fribidi/togit/git/../fribidi/fribidi2/gen.tab/gen-unicode-version.c,v $
9  *
10  * Author:
11  *   Behdad Esfahbod, 2001, 2002, 2004
12  *
13  * Copyright (C) 2004 Sharif FarsiWeb, Inc
14  * Copyright (C) 2004 Behdad Esfahbod
15  * 
16  * This library is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2.1 of the License, or (at your option) any later version.
20  * 
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * Lesser General Public License for more details.
25  * 
26  * You should have received a copy of the GNU Lesser General Public License
27  * along with this library, in a file named COPYING; if not, write to the
28  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29  * Boston, MA 02110-1301, USA
30  * 
31  * For licensing issues, contact <license@farsiweb.info>.
32  */
33
34 #include <common.h>
35
36 #include <stdio.h>
37 #if STDC_HEADERS+0
38 # include <stdlib.h>
39 # include <stddef.h>
40 #else
41 # if HAVE_STDLIB_H
42 #  include <stdlib.h>
43 # endif
44 #endif
45 #if HAVE_STRING_H+0
46 # if !STDC_HEADERS && HAVE_MEMORY_H
47 #  include <memory.h>
48 # endif
49 # include <string.h>
50 #endif
51 #if HAVE_STRINGS_H+0
52 # include <strings.h>
53 #endif
54
55 #include "packtab.h"
56
57 #define appname "gen-unicode-version"
58 #define outputname "fribidi-unicode-version.h"
59
60 static void
61 die (
62   const char *msg
63 )
64 {
65   fprintf (stderr, appname ": %s\n", msg);
66   exit (1);
67 }
68
69 static void
70 die2 (
71   const char *fmt,
72   const char *p
73 )
74 {
75   fprintf (stderr, appname ": ");
76   fprintf (stderr, fmt, p);
77   fprintf (stderr, "\n");
78   exit (1);
79 }
80
81 int version_major, version_minor, version_micro;
82 char unicode_version[100];
83 char buf[4000];
84
85 static void
86 init (
87   void
88 )
89 {
90   version_major = version_minor = version_micro = 0;
91   strcpy (unicode_version, "(unknown)");
92 }
93
94 #define READ_VERSION(prefix) ((where = strstr(buf, prefix)) && \
95                               (3 == sscanf (where, \
96                                             prefix"%d.%d.%d", &version_major, &version_minor, &version_micro)))
97
98 static int
99 read_file (
100   FILE *f
101 )
102 {
103   int found = 0;
104
105   version_micro = 0;
106
107   while (fgets (buf, sizeof buf, f))
108     {
109       const char *where;
110
111       if (READ_VERSION ("Version ") || READ_VERSION ("Unicode "))
112         {
113           found = 1;
114           break;
115         }
116     }
117
118   if (!found)
119     return 0;
120
121   sprintf (unicode_version, "%d.%d.%d", version_major, version_minor,
122            version_micro);
123   return 1;
124 }
125
126 static int
127 read_data (
128   const char *data_file_type,
129   const char *data_file_name
130 )
131 {
132   FILE *f;
133   int status;
134
135   fprintf (stderr, "Reading `%s'\n", data_file_name);
136   if (!(f = fopen (data_file_name, "rt")))
137     die2 ("error: cannot open `%s' for reading", data_file_name);
138
139   status = read_file (f);
140
141   fclose (f);
142
143   return status;
144 }
145
146 static void
147 gen_unicode_version (
148   const char *data_file_type
149 )
150 {
151   fprintf (stderr, "Generating `" outputname "'\n");
152   printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
153           FRIBIDI_VERSION ")\n" " * from the file %s */\n\n", data_file_type);
154
155   printf ("#define FRIBIDI_UNICODE_VERSION \"%s\"\n"
156           "#define FRIBIDI_UNICODE_MAJOR_VERSION %d\n"
157           "#define FRIBIDI_UNICODE_MINOR_VERSION %d\n"
158           "#define FRIBIDI_UNICODE_MICRO_VERSION %d\n"
159           "#define FRIBIDI_UNICODE_NAMESPACE(SYMBOL) \\\n"
160           "     FRIBIDI_NAMESPACE(SYMBOL##_unicode_%d_%d_%d)\n"
161           "#define FRIBIDI_UNICODE_PRIVATESPACE(SYMBOL) \\\n"
162           "     FRIBIDI_PRIVATESPACE(SYMBOL##_unicode_%d_%d_%d)\n"
163           "\n",
164           unicode_version,
165           version_major, version_minor, version_micro,
166           version_major, version_minor, version_micro,
167           version_major, version_minor, version_micro);
168
169   printf ("/* End of generated " outputname " */\n");
170 }
171
172 int
173 main (
174   int argc,
175   const char **argv
176 )
177 {
178   const char *data_file_type;
179   int i;
180   int found = 0;
181
182   if (argc < 2)
183     die ("usage:\n  " appname
184          " /path/to/a/UCD/file [/path/to/more/UCD/files ...]");
185
186   {
187     const char *data_file_name;
188
189     init ();
190     for (i = 1; i < argc && !found; i++)
191       {
192         data_file_name = argv[i];
193         data_file_type = strrchr (data_file_name, '/');
194         if (data_file_type)
195           data_file_type++;
196         else
197           data_file_type = data_file_name;
198         found = read_data (data_file_type, data_file_name);
199       }
200     if (!found)
201       die ("error: version not found");
202     gen_unicode_version (data_file_type);
203   }
204
205   return 0;
206 }