#include <stdio.h>
#include <string.h>

struct glstringrec {
  int stringnum;
  int textlength;
  int numlines;
};
char text[20480];

int main(int argc, char *argv[]) {
  FILE *inputf;
  struct glstringrec rec;
  char *inputfilename, *tokptr, *tokptr2;
  int cnt;

  if (argc > 1) {
    if (strcmp(argv[1], "-h") == 0) {
      printf("Usage: %s [input_file]\n", argv[0]);
      return 0;
    } else
    inputfilename = argv[1];
  } else
    inputfilename = "glstrings.bin";

  if ((inputf = fopen(inputfilename, "rb")) == NULL) {
    printf("Unable to open input file %s\n", argv[1]);
    return 0;
  }

  while (fread(&rec, sizeof(rec), 1, inputf) == 1) {
    printf("%%%d\n", rec.stringnum);
    if (fread(text, rec.textlength, 1, inputf) != 1) {
      fprintf(stderr, "Record #%d corrupted\n", rec.stringnum);
      printf("corrupted\n");
    } else {
      tokptr = strtok(text, "\n");
      for (cnt=0; cnt < rec.numlines; cnt++) {
        if (tokptr == NULL) {
	  fprintf(stderr, "Missing text: corrupted record #%d\n", rec.stringnum);
          printf("corrupted\n");
	} else {
          while ((tokptr2 = strstr(tokptr, "\\r\\n"))) {
            tokptr2[0] = '\\';
            tokptr2[1] = '\n';
            strcpy(tokptr2+2, tokptr2+4);
          }
          printf("%s\n", tokptr);
        }
        tokptr = strtok(NULL, "\n");
      }
    }
  }
  fclose(inputf);
  return 0;
}

