// listsub.cpp -----------------------------------------------------------
// Usage:      listsub InputFileName OutputFileName
// called by listsubs.bat
// compile with: bcc32 listsub.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define L 2000 // max line length

int n = 1;     // length of filename without the extension
FILE* fi,*fo;  // input and output files
char* q;
char* p;       // pointer to file extension (starting with the dot)
char in[L];    // input buffer
char r[L];     // root file name (not including extension)
char t[L];     // concatination of .srt and .srh strings
char s[L]      // file extension followed by names of embedded subtitles
     = "List of video files in current folder and all subfolders showing supplied subtitles\n"
       "===================================================================================\n";

void pt()      // output line to file fo
{
  // 70 dashes indicate that there are no embedded or external subtitles
  if (strlen(s)+strlen(t) < 5) strcat(t,"----------------------------------------------------------------------");
  //fprintf(fo,"DebugPT: s = [%s], t = [%s], r = [%s]\n",s,t,r);
  strcat(strcat(r,s),t);                             // concatinate s and t onto the end of r
  while (r[strlen(r)-1] == ' ') r[strlen(r)-1] = 0;  // remove trailing blanks
  fputs(strcat(r,"\n"),fo);                          // output to file with trailing newline character
}

void main(int argc, char* argv[])
{ if (argc != 3)
  { printf("Usage:\n%s InputFileName OutputFileName\n", argv[0]);  // 2 input parameters required
    return;
  }
  fi = fopen(argv[1],"r");  if (!fi) printf("Can't open input file %s\n", argv[1]);
  fo = fopen(argv[2],"w");  if (!fo) printf("Can't open output file %s\n",argv[2]);

  while (fgets(in,L,fi))                                        // get next line from input file
  { in[strlen(in)-1] = 0;                                       // remove newline character
    if (!strncmp(in,r,n))  // here if the filename (without extension) is the same as the previous line
    { p = in+n;  q = strstr(p,":");  if(q) *q = '.';
      if (strstr(p,".sr") || strstr(p,".SR"))  { strcat(t," "); strcat(t,p); }
      else                                     strcpy(s,p);
      //fprintf(fo,"DebugCont: s = [%s], t = [%s]\n",s,t);
    }
    else                                    // here if it's a new filename
    { pt();  strcpy(r,in);                  // write previous line to output file & save new line into "r"
      //fprintf(fo,"DebugNew: in = [%s]\n",r);
      p = strrchr(r,'.');  strcpy(s,p);     // save extension in "s"
      q = strstr(s,":");  if (q) *q = '.';
      *t = 0;  *p = 0;  n = strlen(r);      // clear t buffer & remove the extension from the r buffer
      if (strstr(s,".sr") || strstr(s,".SR"))
        { strcpy(t+1,s); *t = ' '; *s = 0; }  // move extension to t buffer for .srt/.srh files
      //fprintf(fo,"DebugNEW: s = [%s], t = [%s]\n",s,t);
    }
  }
  pt();                    // write the last line  to the output file
}
