mkarray.c

Go to the documentation of this file.
00001 /*
00002  * mkarray - make a c array containing the input file.
00003  *
00004  * this file is part of the makeutil package:
00005  *   http://sourceforge.net/projects/makeutil/
00006  *   http://www.cybermesa.com/~aisa/makeutil/
00007  *
00008  * this file is hereby placed in the public domain.
00009  * aisa0@users.sourceforge.net, aisa@cybermesa.com
00010  */
00011 
00012 static char rcsid[]="$Id: mkarray.c,v 1.2 2004/12/11 18:21:51 aisa0 Exp $";
00013 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 
00018 static int
00019 mkarray(filename, file, ofile, varname)
00020   char *filename;
00021   FILE *file;
00022   FILE *ofile;
00023   char *varname;
00024 {
00025   char buf[BUFSIZ];
00026   size_t rsize, tsize=0;
00027   int done=0, line=8, status=EXIT_SUCCESS;
00028 
00029   fprintf(ofile, "/*\n");
00030   fprintf(ofile, " * this file is automatically generated\n");
00031   fprintf(ofile, " */\n");
00032   fprintf(ofile, "\n");
00033   fprintf(ofile, "#include <string.h>\n");
00034   fprintf(ofile, "\n");
00035   fprintf(ofile, "static char rcsid[]=\"%s\";\n", rcsid);
00036   fprintf(ofile, "\n");
00037   fprintf(ofile, "char %s[]=\n{\n", varname);
00038 
00039   while(!done) {
00040     int i, ch;
00041 
00042     rsize=fread(&buf[0], sizeof(char), sizeof buf/sizeof buf[0], file);
00043     tsize+=rsize;
00044 
00045     switch(rsize) {
00046     case 0:
00047       rsize=1;
00048       buf[0]='\0';
00049       done=1;
00050 
00051       /* down seems more likely */
00052 
00053     default:
00054       for(i=0;i<rsize;++i) {
00055         if(8==line) fputc('\t', ofile);
00056 
00057         line+=(ch=fprintf(ofile, "0x%02x,", buf[i]&0xff));
00058 
00059         if(line+ch>=78) {
00060           line=8;
00061           fputc('\n', ofile);
00062         }
00063       }
00064     }
00065   }
00066   if(ferror(file)) {
00067     perror("error: read");
00068     status|=EXIT_FAILURE;
00069   }
00070 
00071   if(8!=line) fputc('\n', ofile);
00072   fputs("};\n\n", ofile);
00073 
00074   fprintf(ofile, "size_t _%s_size=%d;\n", varname, (int)tsize);
00075   fprintf(ofile, "size_t *%s_size=&_%s_size;\n", varname, varname);
00076 
00077   if(ferror(ofile)) {
00078     perror("error: write: -: ");
00079     status|=EXIT_FAILURE;
00080   }
00081   if(EOF==fclose(ofile)) {
00082     perror("error: close: -: ");
00083     status|=EXIT_FAILURE;
00084   }
00085 
00086   return status;
00087 }
00088 
00089 int
00090 main(argc, argv)
00091   char *argv[];
00092 {
00093   char *filename, *ofilename, *varname;
00094   int status=EXIT_SUCCESS;
00095 
00096   --argc;
00097   ++argv;
00098 
00099   if(argc!=3) {
00100       fputs("usage: mkarray <input-file> <var-name> <output-file>\n", stderr);
00101       fprintf( stderr, "You gave %d args\n", argc);
00102       exit(status|EXIT_FAILURE);
00103   }
00104 
00105   filename=argv[0];
00106   ofilename=argv[2];
00107   varname=argv[1];
00108 
00109   if(0==strcmp("-", filename)) {
00110     status|=mkarray(filename, stdin, varname);
00111   } else {
00112     FILE *file;
00113     FILE *ofile;
00114     if( ( NULL!=(file=fopen(filename, "rb")) ) && ( NULL!=(ofile=fopen(ofilename, "w")) ) )
00115     {
00116       status|=mkarray(filename, file, ofile, varname);
00117       if(EOF==fclose(file)) {
00118         perror("error: close");
00119         status|=EXIT_FAILURE;
00120       }
00121     }
00122     else
00123     {
00124       perror("error: open");
00125       status|=EXIT_FAILURE;
00126     }
00127   }
00128 
00129   exit(status);
00130 }
00131 
00132 /*                                                              ..__
00133  *                                                              `' "
00134  */

Generated on Tue Sep 25 19:23:00 2007 for libzypp by  doxygen 1.5.3