Initial release of Maemo 5 port of gnuplot
[gnuplot] / src / win / geticon.c
1 #ifndef lint
2 static char *RCSid() { return RCSid("$Id: geticon.c,v 1.3 2004/07/01 17:10:10 broeker Exp $"); }
3 #endif
4
5 /* geticon.c */
6 /* extract Borland ascii format icons from resource script */
7 /* and write as Microsoft binary format .ICO files */
8 /* Russell Lang 1992-12-20 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14
15 /* HBB 980809: naming a variable 'inline' is a bad idea, these days. Too
16  * many compilers use it as a keyword... Changed to 'inputline' */
17 #define MAXLINE 255
18 FILE *rcfile;
19 char inputline[MAXLINE+1];
20 char *tok1, *tok2, *tok3;
21 char *p;
22 char iconname[MAXLINE+1];
23 FILE *iconfile;
24 int line;
25
26 int htoi(char ch)
27 {
28     ch = toupper(ch);
29     if (ch < '0')
30         return(0);
31     else if (ch <= '9')
32         return((int)(ch - '0'));
33     else if (ch < 'A')
34         return(0);
35     else if (ch <= 'F')
36         return((int)(ch - 'A' + 10));
37     return(0);
38 }
39
40 void
41 geticon()
42 {
43     char ch;
44
45     fgets(inputline,MAXLINE,rcfile);
46     line++;
47     if (strncmp(inputline,"BEGIN",5)) {
48         fprintf(stderr,"Expecting BEGIN at line %d\n",line);
49         exit(3);
50     }
51     if ( (iconfile = fopen(iconname,"wb")) == (FILE *)NULL) {
52         fprintf(stderr,"Can't open ICON file %s\n",iconname);
53         exit(4);
54     }
55     fgets(inputline,MAXLINE,rcfile);
56     line++;
57     while (strncmp(inputline,"END",3) && !feof(rcfile)) {
58         for (p = inputline; *p && (*p==' ' || *p == '\t' || *p=='\''); p++);
59         while (isxdigit(*p)) {
60             ch = htoi(*p++)<<4;
61             ch += htoi(*p++);
62             fputc(ch, iconfile);
63             p++;
64         }
65         fgets(inputline,MAXLINE,rcfile);
66         line++;
67     }
68     fclose(iconfile);
69 }
70
71 int
72 main(int argc, char *argv[])
73 {
74     if ((argc < 2) || (argc > 3)) {
75         fprintf(stderr,"Usage:  geticon  resource_file [icon_directory]\n");
76         return(1);
77     }
78     if ( (rcfile = fopen(argv[1],"r")) == (FILE *)NULL) {
79         fprintf(stderr,"Can't open RC file\n");
80         return(2);
81     }
82     line = 0;
83     while (fgets(inputline,MAXLINE,rcfile)) {
84         line++;
85         tok1 = strtok(inputline," \t\r\n");
86         tok2 = strtok(NULL," \t\r\n");
87         tok3 = strtok(NULL," \t\r\n");
88         if (tok2 && !strcmp(tok2,"ICON") && (tok3 == (char *)NULL)) {
89             iconname[0] = '\0';
90             if (argc == 3) {
91                 strcpy(iconname,argv[2]);
92                 strcat(iconname,"\\");
93             }
94             strcat(iconname,tok1);
95             strcat(iconname,".ico");
96             fprintf(stdout,"%s\n",iconname);
97             geticon();
98         }
99     }
100     return 0;
101 }