Advertisement
ibi

HTML File-Indexer

ibi
May 13th, 2014
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.16 KB | None | 0 0
  1. //-----
  2. //
  3. // HFI - Commandlineversion - WATCOM C/C++ 11.0
  4. // 04.1998 by bingen@gmx.de - use m.bat to compile
  5. //    upd 1.05 - 06.1998 - sorting mainindex, if no subs no mainindexlink
  6. //
  7. //-----
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <io.h>
  12. #include <string.h>
  13. #include <dos.h>
  14. #include <direct.h>
  15.  
  16. #define TRUE   1
  17. #define FALSE  0
  18. #define MAXWERT      2000   //soviele files sollten wohl reichen, oder?
  19. #define MAX_PATH     500    //laengste rekursion
  20. #define MAX_FNLEN    255    //laengster filename
  21. #define MAX_DIRS     500    
  22. #define PROGVERSION  "1.05" //na was wohl?
  23. #define COPYR        "(c)1998 by -BiNGEN- (bingen@gmx.de)"
  24. #define COMMENTSTR   "<!--generated by HFI "PROGVERSION"/html-file-indexer\n    "COPYR" -->\n"
  25. #define METASTR      "<meta name=\"generator\" content=\"HFI "PROGVERSION"/"OS_VERSION"\">\n"
  26. #define VERSIONSTR   "HFI "PROGVERSION"/"OS_VERSION
  27.  
  28. #ifdef WINNT
  29. #define OS_VERSION   "winnt32"
  30. #endif
  31. #ifdef DOS4G
  32. #define OS_VERSION   "dos4gw"
  33. #endif
  34.  
  35. char  outframe[20];  
  36. char  mainframe[20];  
  37. char  colorstr[100];  
  38.  
  39. char fnames[MAXWERT][MAX_FNLEN];
  40. int  anz_fnames;
  41. char spec[15][15];
  42. int  anz_spec;
  43. char dirnames[MAX_DIRS][MAX_PATH];
  44. int  anz_dirs;
  45. int  home_len;
  46. int  subs;
  47.  
  48.  
  49. /*****************************************************************************/
  50. void makeframe(char *path ,char *filename)
  51. {
  52.    FILE  *stream;
  53.    char  akt_file[MAX_PATH];
  54.  
  55.    sprintf(akt_file,"%s\\%s", path, mainframe);
  56.  
  57.    if((stream=fopen(akt_file,"w"))!=NULL)
  58.       {
  59.       fputs("<html><head>\n", stream);
  60.       fputs(METASTR, stream);
  61.       fputs(COMMENTSTR, stream);
  62.       fputs("<title>Index of ", stream);
  63.       fputs(&path[home_len], stream);
  64.       fputs("</title></head>\n", stream);
  65.       fputs("<frameset cols=\"20%,*\" border=\"3\">\n", stream);
  66.       fputs("<frame src=\"", stream);
  67.       fputs(outframe, stream);
  68.       fputs("\" name=\"left\">\n", stream);
  69.       fputs("<frame src=\"", stream);
  70.       fputs(filename, stream);
  71.       fputs("\" name=\"right\">\n</frameset>\n</html>", stream);
  72.       fclose(stream);
  73.       }
  74.    else
  75.       printf("..can't open %s\n", akt_file);
  76.  
  77. }
  78.  
  79. /*****************************************************************************/
  80. long buildarray(char *path)
  81. {
  82.    struct _finddata_t   fileinfo;
  83.    long                 handle, bytes;
  84.    int                  rc;
  85.    char                 tausch[MAX_FNLEN];
  86.    char                 akt_file[MAX_PATH];
  87.    register int         i, j;
  88.  
  89.    anz_fnames=0;
  90.    bytes=0L;
  91.  
  92.    for(i=0;i<anz_spec;i++)
  93.       {
  94.       sprintf(akt_file,"%s\\%s", path, spec[i]);
  95.  
  96.       handle=_findfirst(akt_file, &fileinfo);
  97.       rc=handle;
  98.       while(rc!=-1)
  99.          {
  100.          if(anz_fnames>MAXWERT-1)
  101.             {
  102.             printf("*.file-overflow! (more than %d files)\n", MAXWERT);
  103.             break;
  104.             }
  105.          else
  106.             {
  107.             if (strcmp(fileinfo.name,"..")!=0
  108.             && strcmp(fileinfo.name,".")!=0
  109.             && !(fileinfo.attrib&_A_SUBDIR))
  110.                {
  111.                strcpy(fnames[anz_fnames++], fileinfo.name);
  112.                bytes+=fileinfo.size;
  113.                }
  114.             }
  115.    
  116.          rc=_findnext(handle, &fileinfo);
  117.          }
  118.        _findclose(handle);
  119.       }
  120.  
  121.    // fuer die paar strings reicht ein bubblesort
  122.    for(i=0;i<anz_fnames-1;i++)
  123.       {
  124.        for(j=i+1;j<anz_fnames;j++)
  125.          {
  126.          if (strcmp(fnames[i],fnames[j])>0)
  127.             {
  128.             strcpy(tausch, fnames[i]);
  129.             strcpy(fnames[i], fnames[j]);
  130.             strcpy(fnames[j], tausch);
  131.             }
  132.          }
  133.       }
  134.  
  135.    return(bytes);
  136.  
  137. }
  138. /*****************************************************************************/
  139. void usage(void)
  140. {
  141.    printf(".compiled with watcom c/c++ 11.0. edited with zeus32 2.60.\n");
  142.    printf(" (the dos4gw-version needs dos4gw.exe, the winnt-version windows95/nt)\n\n");
  143.    printf(".status: freeware. use as often as you want.\n");
  144.    printf(".update: if any... check out http://www.lastresort.net.\n\n");
  145.    printf(".description:\nthis cute, little program builds a html-index of the files in a directory.\n");
  146.    printf("it uses frames. on the left are the filenames, right the file itself.\n\n");
  147.    printf(".main purpose:\ncreating indexes for cd-rom's with jpg's or other media.\n\n");
  148.    //printf("[more]\n\n"); getch();
  149.    printf(".arguments:\n -?, /? - this help.\n");
  150.    printf(" /s     - include all subdirs.\n");
  151.    printf(" /t[x]  - filetypes (i=jpg,gif (default) / t=txt,doc / s=wav,mid,mp3).\n");
  152.    printf("                    (h=htm,html / m=avi,mpg,mov / *=all filetypes).\n\n");
  153.    printf(".example: hfi /th /tt (make index with all htm/txt/doc files).\n");
  154.    exit(0);
  155. }
  156.  
  157. /*****************************************************************************/
  158. char *makedots(char *in)
  159. {
  160.    register int   i;
  161.    char out[MAX_FNLEN]="";
  162.  
  163.    for(i=0;i<strlen(in)+1;i++)
  164.       {
  165.       if (in[i]=='\\')
  166.          strcat(out, "../");
  167.       }
  168.    
  169.    strcpy(in, out);
  170.    return(in);
  171. }
  172.  
  173. /*****************************************************************************/
  174. void makepage(char *path)
  175. {
  176.    FILE  *stream;
  177.    char  outstr[MAX_PATH];
  178.    char  akt_file[MAX_PATH];
  179.    char  dotstr[MAX_FNLEN];
  180.    int   i;
  181.    long  bytes;
  182.  
  183.    if(bytes=buildarray(path))
  184.       {
  185.  
  186.       sprintf(akt_file,"%s\\%s", path, outframe);
  187.  
  188.       if((stream=fopen(akt_file,"w"))!=NULL)
  189.          {
  190.          
  191.          if(anz_dirs>MAX_DIRS-1)
  192.             printf("*.dir-overflow! (more than %d dirs)\n", MAX_DIRS);
  193.          else
  194.             strcpy(dirnames[anz_dirs++], path);
  195.  
  196.          // geht zwar, aber nicht unbedingt sinnvoll
  197.          //strlen(&path[home_len])==0?strcpy(outstr,"\\"): strcpy(outstr,&path[home_len]);
  198.  
  199.          makeframe(path, fnames[0]);
  200.          printf("..processing %s (%i bytes)\n", path, bytes);
  201.          fputs("<html><head>\n", stream);
  202.          fputs(METASTR, stream);
  203.          fputs("<base target=\"right\">\n", stream);
  204.          fputs("<title>Index of ", stream);
  205.          fputs(&path[home_len], stream);
  206.          fputs("</title></head>\n<body ", stream);
  207.          fputs(colorstr, stream);
  208.          fputs(">\n<font face=\"Arial, Helvetica\" size=-1><b>Index&nbsp;of ", stream);
  209.          fputs(&path[home_len], stream);
  210.          fputs("</b><hr>\n", stream);
  211.  
  212.          // nur wenn mit subs
  213.          if(subs)
  214.             {
  215.             strcpy(dotstr, &path[home_len]);
  216.             sprintf(outstr, "<a href=\"%sindex.htm\" target=_parent><b>MainIndex</b></a><hr>\n", makedots(dotstr));
  217.             fputs(outstr, stream);
  218.             }
  219.  
  220.          for(i=0;i<anz_fnames;i++)
  221.             {
  222.             sprintf(outstr, "<a href=\"%s\">%s</a><br>\n", fnames[i], fnames[i]);
  223.             fputs(outstr, stream);
  224.             }
  225.  
  226.          fputs("<hr><a href=\"#top\" target=_self><b>Top of Page</b></a>\n", stream);
  227.          fputs("</font></body></html>\n", stream);
  228.          fclose(stream);
  229.          }
  230.       else
  231.          printf("..can't open %s\n", akt_file);
  232.  
  233.       }
  234.    else
  235.       printf("..no matching files in %s\n", path);
  236.  
  237. }
  238. /*****************************************************************************/
  239. void scandirs(char *path)
  240. {
  241.    struct find_t  fileinfo;
  242.    char           akt_pfad[MAX_PATH];
  243.  
  244.    strcpy(akt_pfad, path);
  245.    strcat(akt_pfad, "\\*.*");
  246.  
  247.    if (_dos_findfirst(akt_pfad, _A_SUBDIR ,&fileinfo)==0)
  248.    {
  249.       do
  250.       {
  251.          if (strcmp(fileinfo.name,"..")!=0)
  252.          {
  253.             if (strlen(path)+strlen(fileinfo.name)>=MAX_PATH)
  254.             {
  255.               printf("..path: %s sub: %s\n..recursion too deep!\n", akt_pfad, fileinfo.name);
  256.             }
  257.             else
  258.               {
  259.               if(strcmp(fileinfo.name,".")==0)
  260.                {
  261.                // darum gehts eigentlich =;-)
  262.                makepage(path);
  263.                }
  264.               else
  265.                {
  266.                // kleine rekursion :-)
  267.                strcpy(akt_pfad, path);
  268.                strcat(akt_pfad, "\\");
  269.                strcat(akt_pfad, fileinfo.name);
  270.                scandirs(akt_pfad);
  271.                }
  272.             }
  273.          }
  274.       }
  275.       while ((_dos_findnext(&fileinfo)==0));
  276.    }
  277.  
  278. }
  279. /*****************************************************************************/
  280. char *tweakdirname(char *in, char *path)
  281. {
  282.    register int   i, k=strlen(in)-strlen(path);
  283.  
  284.    for(i=strlen(path)+1;i<strlen(in)+1;i++)
  285.       {
  286.       if (in[i]=='\\')
  287.          in[i-(strlen(path)+1)]='/';
  288.       else
  289.          in[i-(strlen(path)+1)]=in[i];
  290.       }
  291.    in[k]='\0';
  292.    if(strlen(in)==0) strcpy(in, ".");
  293.  
  294.    return(in);
  295. }
  296.  
  297. /*****************************************************************************/
  298. void buildidx(char *path)
  299. {
  300.    register int   i,j;
  301.    char  outstr[MAX_PATH];
  302.    char  outfile[MAX_PATH];
  303.    char  tausch[MAX_FNLEN];
  304.    FILE  *stream;
  305.  
  306.    if(anz_dirs)
  307.       {
  308.      
  309.       // noch'n bubblesort
  310.       for(i=0;i<anz_dirs-1;i++)
  311.          {
  312.           for(j=i+1;j<anz_dirs;j++)
  313.             {
  314.             if (strcmp(dirnames[i],dirnames[j])>0)
  315.                {
  316.                strcpy(tausch, dirnames[i]);
  317.                strcpy(dirnames[i], dirnames[j]);
  318.                strcpy(dirnames[j], tausch);
  319.                }
  320.             }
  321.          }
  322.      
  323.       strcpy(outfile, path);
  324.       strcat(outfile, "\\index.htm");
  325.       if((stream=fopen(outfile,"w"))!=NULL)
  326.          {
  327.          printf("..building mainindex %s\n", outfile);
  328.          fputs("<html><head>\n", stream);
  329.          fputs(METASTR, stream);
  330.          fputs(COMMENTSTR, stream);
  331.          fputs("<title>Index created by ", stream);
  332.          fputs(VERSIONSTR, stream);
  333.          fputs("</title></head>\n<body ", stream);
  334.          fputs(colorstr, stream);
  335.          //fputs(" onload=\"self.status='&#169; 1998 by bingen@gmx.de'\">", stream);
  336.          fputs(">\n<font face=\"Arial, Helvetica\" size=-1><h2><b>Index created by ", stream);
  337.          fputs(VERSIONSTR, stream);
  338.          fputs("</h2></b><hr>\n", stream);
  339.  
  340.          for(i=0;i<anz_dirs;i++)
  341.             {
  342.             tweakdirname(dirnames[i], path);
  343.             sprintf(outstr, "<a href=\"%s/%s\">%s</a><br>\n", dirnames[i], mainframe, dirnames[i]);
  344.             fputs(outstr, stream);
  345.             }
  346.  
  347.          fputs("<hr></font></body></html>\n", stream);
  348.          fclose(stream);
  349.          }
  350.       else
  351.          printf("..can't open %s\n", outfile);
  352.       }
  353.  
  354. }
  355.  
  356. /*****************************************************************************/
  357. void main(int argc, char *argv[])
  358. {
  359.    char           homedir[MAX_FNLEN];
  360.    register int   i;
  361.  
  362.    anz_spec=anz_dirs=0;
  363.    subs=FALSE;
  364.  
  365.    printf("\nHFI "PROGVERSION" - html-file-indexer ("OS_VERSION")\n");
  366.     printf(COPYR" * use /? for help.\n\n");
  367.  
  368.    // gibts argumente dafuer? ;-)
  369.    for (i=1;i<argc;i++)
  370.      {
  371.      if (argv[i][0]=='/' || argv[i][0]=='-')
  372.         {
  373.         if(argv[i][1]=='?') usage();
  374.        
  375.         if(argv[i][1]=='s' || argv[i][1]=='S') subs=TRUE;
  376.  
  377.         if(argv[i][1]=='t' || argv[i][1]=='T')
  378.          {
  379.          if(argv[i][2]=='i' || argv[i][2]=='I')
  380.             {
  381.             strcpy(spec[anz_spec++], "*.jpg");
  382.             strcpy(spec[anz_spec++], "*.gif");
  383.             }
  384.          if(argv[i][2]=='h' || argv[i][2]=='H')
  385.             {
  386.             strcpy(spec[anz_spec++], "*.htm");
  387.             strcpy(spec[anz_spec++], "*.html");
  388.             }
  389.          if(argv[i][2]=='t' || argv[i][2]=='T')
  390.             {
  391.             strcpy(spec[anz_spec++], "*.doc");
  392.             strcpy(spec[anz_spec++], "*.txt");
  393.             }
  394.          if(argv[i][2]=='m' || argv[i][2]=='M')
  395.             {
  396.             strcpy(spec[anz_spec++], "*.avi");
  397.             strcpy(spec[anz_spec++], "*.mpg");
  398.             strcpy(spec[anz_spec++], "*.mov");
  399.             }
  400.          if(argv[i][2]=='s' || argv[i][2]=='S')
  401.             {
  402.             strcpy(spec[anz_spec++], "*.wav");
  403.             strcpy(spec[anz_spec++], "*.mid");
  404.             strcpy(spec[anz_spec++], "*.mp3");
  405.             }
  406.          if(argv[i][2]=='*')
  407.             {
  408.             strcpy(spec[0], "*.*");
  409.             anz_spec=1;
  410.             }
  411.          }
  412.         }
  413.      else
  414.         {
  415.         printf("Unknown option \"%s\". try /? for help.\n", argv[i]);
  416.         exit(-1);
  417.         }
  418.      }
  419.  
  420.    // die filenamen per argument? hab ich grad kein bock...
  421.    strcpy(outframe,"_idx.htm");
  422.    strcpy(mainframe,"idx.htm");
  423.    strcpy(colorstr,"bgcolor=\"#cccccc\" text=\"#000033\" link=\"#000033\" vlink=\"#666666\" alink=\"#ff0000\"");
  424.  
  425.    strcpy(homedir,getcwd( NULL ,0));
  426.    if(strlen(homedir)==3) homedir[strlen(homedir)-1]='\0';
  427.    home_len=strlen(homedir);
  428.  
  429.    //defaultwerte falls nichts gesetzt
  430.    if(anz_spec<1)
  431.       {
  432.       strcpy(spec[anz_spec++], "*.jpg");
  433.       strcpy(spec[anz_spec++], "*.gif");
  434.       }
  435.    printf("..indexing: ");
  436.    for(i=0;i<anz_spec;i++)
  437.       printf("%s ",spec[i]);
  438.  
  439.    if(subs)
  440.       {
  441.       printf("\n..starting at %s incl. subdirs\n", homedir);
  442.       scandirs(homedir);
  443.       buildidx(homedir);
  444.       }
  445.    else
  446.       {
  447.       printf("\n");
  448.       makepage(homedir);
  449.       }
  450.  
  451.    printf("..done.\n\nthnx for using HFI "PROGVERSION", bye.\n");
  452.  
  453. }
  454. /*****************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement