
%{

#include<stdio.h>

  static FILE *f;

%}

%%

\\ fputs("\\\\", f);
\"  fputs("\\\"", f);

\r fputs("\\r", f);
\n fputs("\\n", f);
\t fputs("\\t", f);

.  fputc(yytext[0], f);

%%

int yywrap(void) {
          return 1;
          }

int main(int na, char* argv[]) {
  int i, j;
  char flag, c;

  if(na <= 2) {
    printf("\nThis program converts text files to strings that can be used within C programs");
    printf("\n\nUsage: %s output_file input_file [input_files]\n", argv[0]);
    return 0;
  }

  f = fopen(argv[1], "w");
  if(!f) {
    printf("\nThe file %s cannot be written\n", argv[1]);
    return 1;
  }

  for(i = 2, flag = 0; i < na; i++) {
    yyin = fopen(argv [i], "r");
    if(!yyin) {
      flag = 1;
      printf("\nThe file %s could not be opened\n", argv[i]);
    }
    else
      fclose(yyin);
  }

  if(flag)
    return 1;

  //  fprintf(f, "/* This is %s generated with the command line:\n %s %s", argv[1], argv[0], argv[1]);
  //for(i = 2; i < na; i++) 
  //  fprintf(f, " %s", argv[i]);
  //  fputs("\n*/\n\n", f); 

  for(i = 2; i < na; i++) {
    yyin = fopen(argv[i], "r");
    if(!yyin) {
      printf("\nCould not reopen the file %s\n", argv[i]);
      fclose(f);
      return 1;
    }

    fputs("\nchar ", f);
    for(j = 0; argv[i][j]; j++) {
      c = argv[i][j];
      if(c == '.')
        fputc('_', f);
      else
        fputc(c, f);
    }
    fputs("[] = \"", f);

    if(i >= 3)
      yyrestart(yyin);
    yylex();
    fclose(yyin);

    fputs("\";\n", f);
  }

  fclose(f);
  return 0;
}

