Back to home page

MITgcm

 
 

    


File indexing completed on 2018-03-02 18:44:45 UTC

view on githubraw file Latest commit add29e06 on 2018-01-31 20:35:05 UTC
915b5ad83a Ed H*0001 /*************************************************   -*- mode: C -*-
                0002 */
                0003 
                0004 /*  Encode one or more files for later writing */
                0005 
                0006 #include <stdio.h>
                0007 #include <stdlib.h>
                0008 #include <unistd.h>
                0009 #include <sys/stat.h>
                0010 #include <sys/types.h>
                0011 
                0012 int main(int argc, char** argv)
                0013 {
                0014     FILE * fout;
                0015     FILE * fin;
                0016     int ii;
                0017     int nbytes;
                0018     struct stat astat;
                0019     unsigned char c;
                0020     unsigned char buf[1024];
                0021 
                0022     /*
                0023     fout = fopen("t8", "w");
                0024     for (ii=0; ii<256; ii++) {
                0025     c = (unsigned char)ii;
                0026     fprintf(fout,"%c",c);
                0027     }
                0028     fclose(fout);
                0029     */
                0030 
                0031     if (argc < 3) {
                0032     printf("ERROR:  usage is: "
                0033            "\"%s OUTPUT_FILE INPUT_FILE [...INPUT_FILES...]\"\n",
                0034            argv[0]);
                0035     return 1;
                0036     }
                0037 
                0038     fout = fopen(argv[1], "w");
                0039     if (! fout) {
                0040     printf("ERROR: cannot open \"%s\" for output\n", argv[1]);
                0041     return 1;
                0042     }
                0043     fprintf(fout,"struct embeded_file {\n    char * name;\n"
                0044             "    int ndat;\n    char * dat;\n};\n");
                0045     fprintf(fout,"const int n_flist = %d;\n", argc-2);
                0046     fprintf(fout,"struct embeded_file flist[] = {\n");
                0047     
                0048     for (ii=2; ii<argc; ii++) {
                0049     printf("  encoding:  \"%s\"\n",argv[ii]);
                0050     if (! stat(argv[ii], &astat)) {
                0051         nbytes = (int)(astat.st_size);
                0052     }
                0053     else {
                0054         printf("    ERROR:  cannot stat() \"%s\"\n",argv[ii]);
                0055         return 3;
                0056     }
                0057 
                0058     fin = fopen(argv[ii], "r");
                0059     if (!fin) {
                0060         printf("    ERROR:  cannot fopen() \"%s\"\n",argv[ii]);
                0061         return 4;
                0062     }
                0063 
                0064     fprintf(fout,"  %s{ \"%s\", %d,", ((ii==2) ? " " : ","),
                0065         argv[ii], nbytes);
                0066     while(fin) {
                0067         size_t ir;
                0068         size_t nread;
                0069 
                0070         /* fgets(buf, 255, fin); */
                0071         nread = fread(buf, sizeof(char), 200, fin);
                0072         if (nread < 1)
                0073         break;
                0074         fprintf(fout,"\n\"");
                0075         for (ir=0; ir<nread; ir++) {
                0076         fprintf(fout,"\\x%02x",buf[ir]);
                0077         /*
                0078         if (isalnum(buf[ir]))
                0079             fprintf(fout,"%c",buf[ir]);
                0080         else
                0081             fprintf(fout,"\\x%02x",buf[ir]);
                0082         */
                0083         }
                0084         fprintf(fout,"\"");
                0085     }
                0086     fclose(fin);
                0087     fprintf(fout,"  }\n");
                0088     }
                0089     fprintf(fout,"  };\n");
                0090     fclose(fout);
                0091 
                0092     return 0;
                0093 }
                0094