home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / gfx / misc / gallery.lha / Gallery / Gallery.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-03  |  8.8 KB  |  292 lines

  1. char *V = "$VER: Gallery 37.0 (02.02.97)";
  2.  
  3. #include <GUIC_Classes/all.hpp>
  4.  
  5. #define COPYRIGHT  "<SMALL> index file created with 'Gallery', (c) 1997 by <A HREF=\"http://www.student.uni-kl.de/~hillenbr\">Markus Hillenbrand</A> </SMALL>"
  6. #define FILEFILTER "#?.(jpeg|jpg|gif|iff|ilbm|iff24|png)"
  7.  
  8. GUIC_LibraryC IntuitionBase("intuition.library",37); // Just to check the version of the system
  9.  
  10. GUIC_ListC errorList;
  11. int lastDay=0,lastMonth=0,lastYear=0;
  12.  
  13. class GalleryEntryC : public GUIC_ObjectC
  14.     {
  15.     public:
  16.         GalleryEntryC(STRPTR f, LONG s, int d, int m, int y) { fileName=f; size=s; day=d; month=m; year=y; }
  17.         String fileName;
  18.         int day,month,year;
  19.         LONG size;
  20.     };
  21.  
  22. String     MakeThumbnail    (GUIC_FileExamineC &file)
  23. {
  24.     String fileName = file.GetName();
  25.  
  26.     int i = fileName.length();
  27.     while (--i) if (fileName[i] == '.') break;
  28.  
  29.     String thumbName = fileName.left(i);
  30.     thumbName+="_.jpg";
  31.  
  32.     try
  33.         {
  34.         GUIC_FileExamineC f(thumbName);
  35.         if (f.Newer(file)) return thumbName;
  36.         }
  37.     catch (GUIC_SystemX &e) { }
  38.  
  39.     String args = "<>NIL: " + fileName + " TO " + thumbName + " FORMAT JPEG QUALITY 90 BOXFIT 100 100";
  40.  
  41.     cout << " - creating thumbnail for file '" << fileName << "' ... " << flush;
  42.     BOOL result = GUIC_SystemC::RunProgram("GfxCon_68020", 100000, args);
  43.     if (result) cout << "done." << endl; else cout << "error occured." << endl;
  44.  
  45.     return thumbName;
  46. }
  47. void         CreateGallery        (GUIC_ListC &fileList, STRPTR pattern)
  48. {
  49.     GalleryEntryC *key = (GalleryEntryC *)fileList.ObjectAt(1);
  50.     GUIC_FileExamineC firstFile(key->fileName);
  51.     String currentDir = firstFile.GetPathPart();
  52.     GUIC_FileC indexFile(currentDir, "index.html", GUIC_Write);
  53.     GUIC_FileExamineC directoryName(currentDir);
  54.  
  55.     cout << "Creating gallery in directory '" << currentDir << "' " << endl;
  56.  
  57.     indexFile.WriteLn("<HTML>");
  58.     indexFile.WriteLn("<HEAD>");
  59.  
  60.     indexFile.Write("<TITLE> ");
  61.     indexFile.Write(directoryName.GetFilePart());
  62.     indexFile.WriteLn(" </TITLE>");
  63.  
  64.     indexFile.WriteLn("</HEAD>");
  65.     indexFile.Write("<BODY");
  66.  
  67.     if (pattern)
  68.         {
  69.         indexFile.Write(" Background=\"");
  70.         indexFile.Write(pattern);
  71.         indexFile.Write("\"");
  72.         }
  73.  
  74.     indexFile.WriteLn("><CENTER>");
  75.     indexFile.WriteLn("<HR>");
  76.     indexFile.Write("<H1>");
  77.     indexFile.Write(directoryName.GetFilePart());
  78.     indexFile.WriteLn("</H1>");
  79.  
  80.     indexFile.WriteLn("<TABLE Border=4 CellSpacing=4 CellPadding=4>");
  81.  
  82.     int count = 0;
  83.  
  84.     for (int i=1; i<=fileList.Length(); i++)
  85.         {
  86.         key = (GalleryEntryC *)fileList.ObjectAt(i);
  87.         try
  88.             {
  89.             GUIC_FileExamineC file(key->fileName);
  90.             String thumbName = MakeThumbnail(file);
  91.             GUIC_FileExamineC thumb (thumbName);
  92.  
  93.             // Check the date
  94.             GUIC_DateC oldDate(lastDay,lastMonth,lastYear),*fileDate = file.GetDate();;
  95.             if (fileDate->Greater(oldDate) )
  96.                 {
  97.                 lastDay   = fileDate->GetDay();
  98.                 lastMonth = fileDate->GetMonth();
  99.                 lastYear  = fileDate->GetYear();
  100.                 }
  101.  
  102.             // Check if there are 4 entries per line in the table
  103.             if (count++ % 4 == 0) indexFile.Write("<TR> ");
  104.  
  105.             indexFile.Write("<TD> <CENTER> <IMG SRC=\"");
  106.             indexFile.Write(thumb.GetFilePart());
  107.             indexFile.Write("\"> <BR> <A HREF=\"");
  108.             indexFile.Write(file.GetFilePart());
  109.             indexFile.Write("\"> ");
  110.             indexFile.Write(file.GetFilePart());
  111.             indexFile.Write(" </A> <BR> Size: ");
  112.             indexFile.Write(key->size);
  113.             indexFile.Write(" <BR> Date: ");
  114.             indexFile.Write(key->day);
  115.             indexFile.Write(".");
  116.             indexFile.Write(key->month);
  117.             indexFile.Write(".");
  118.             indexFile.Write(key->year);
  119.             indexFile.WriteLn("</TD>");
  120.             }
  121.         catch (GUIC_SystemX &e) { GalleryEntryC *s = new GalleryEntryC(key->fileName,0,0,0,0); errorList.AddTail(*s); }
  122.         }
  123.  
  124.         indexFile.WriteLn("</TABLE>");
  125.         indexFile.WriteLn("<HR>");
  126.         indexFile.WriteLn(COPYRIGHT);
  127.         indexFile.WriteLn("</BODY>");
  128.         indexFile.WriteLn("</HTML>");
  129. }
  130. void         CreateHTML        (GUIC_ListC &fileList, STRPTR pattern)
  131. {
  132.     GalleryEntryC *key = (GalleryEntryC *)fileList.ObjectAt(1);
  133.     GUIC_FileExamineC firstFile(key->fileName);
  134.  
  135.     STRPTR currentDir = firstFile.GetPathPart();
  136.     GUIC_FileC indexFile(currentDir, "index.html", GUIC_Write);
  137.     GUIC_FileExamineC directoryName(currentDir);
  138.  
  139.     String titleString = directoryName.GetFilePart();
  140.     if (titleString == String("") ) titleString = directoryName.GetName();
  141.  
  142.     cout << "Creating file '" << indexFile.GetName() << "'" << endl;
  143.  
  144.     indexFile.WriteLn("<HTML>");
  145.     indexFile.WriteLn("<HEAD>");
  146.  
  147.     indexFile.Write("<TITLE> ");
  148.     indexFile.Write(titleString);
  149.     indexFile.WriteLn(" </TITLE>");
  150.  
  151.     indexFile.WriteLn("</HEAD>");
  152.     indexFile.Write("<BODY");
  153.     if (pattern)
  154.         {
  155.         indexFile.Write(" Background=\"");
  156.         indexFile.Write(pattern);
  157.         indexFile.Write("\"");
  158.         }
  159.     indexFile.WriteLn("><CENTER>");
  160.  
  161.     indexFile.WriteLn("<HR>");
  162.     indexFile.Write("<H1>");
  163.     indexFile.Write(titleString);
  164.     indexFile.WriteLn("</H1><BR>");
  165.     indexFile.WriteLn("<TABLE Border=4 CellSpacing=4 CellPadding=4>");
  166.     indexFile.WriteLn("<TR> <TD> <CENTER> <H3> Gallery Name </H3> </TD> <TD> <CENTER> <H3> Entries </H3> </TD> <TD> <CENTER> <H3> Last Update </H3> </TD>");
  167.  
  168.     for (int i=1; i<=fileList.Length(); i++)
  169.         {
  170.         key = (GalleryEntryC *)fileList.ObjectAt(i);
  171.         GUIC_FileExamineC file (key->fileName);
  172.  
  173.         indexFile.Write("<TR> <TD> <CENTER> <A HREF=\"");
  174.         indexFile.Write(file.GetFilePart());
  175.         indexFile.Write("/index.html\"");
  176.         indexFile.Write("> ");
  177.         indexFile.Write(file.GetFilePart());
  178.         indexFile.Write(" </A> </TD> <TD> <CENTER> ");
  179.         indexFile.Write(key->size);
  180.         indexFile.Write(" </TD> <TD> <CENTER> ");
  181.  
  182.         if (key->day)
  183.             {
  184.             indexFile.Write(key->day);
  185.             indexFile.Write(".");
  186.             indexFile.Write(key->month);
  187.             indexFile.Write(".");
  188.             indexFile.Write(key->year);
  189.             }
  190.         else indexFile.Write("(Directory)");
  191.         indexFile.WriteLn(" </TD>");
  192.         }
  193.  
  194.     indexFile.WriteLn("</TABLE>");
  195.     indexFile.WriteLn("<HR>");
  196.     indexFile.WriteLn(COPYRIGHT);
  197.     indexFile.WriteLn("</BODY>");
  198.     indexFile.WriteLn("</HTML>");
  199. }
  200. int             ScanDir                (STRPTR startDir, STRPTR pattern)
  201. {
  202.     GUIC_ListC fileList,dirList;
  203.     GalleryEntryC *key = 0;
  204.     GUIC_FileExamineC *filex = 0;
  205.     int entries = 0;
  206.  
  207.     GUIC_FileExamineC *examine = new GUIC_FileExamineC(startDir);
  208.     if (!examine->IsDirectory()) throw GUIC_SystemX("Error in function ScanDir (directory name expected).");
  209.     String dirName = examine->GetName();
  210.     delete examine; examine=0;
  211.  
  212.     GUIC_DirectoryExamineC *direx= new GUIC_DirectoryExamineC(dirName);
  213.     BOOL hasSubDirs = direx->HasSubdirectory();
  214.     if (!hasSubDirs) direx->SetFilter(FILEFILTER);
  215.  
  216.     try
  217.         {
  218.         while ( (filex = direx->ExamineNext()) )
  219.             {
  220.             String fileName  = filex->GetName();
  221.             GUIC_DateC *date = filex->GetDate();
  222.  
  223.             if (filex->IsDirectory())
  224.                 {
  225.                 String newPattern = String("../") + String(pattern);
  226.                 int entriesInDir  = ScanDir(fileName, newPattern);
  227.                 if (entriesInDir)
  228.                     {
  229.                     key = new GalleryEntryC(fileName, entriesInDir, lastDay, lastMonth, lastYear);
  230.                     if (!key) throw GUIC_MemoryX("Can't allocate memory for key.");
  231.                     dirList.AddTail(*key);
  232.                     entries+=entriesInDir;
  233.                     }
  234.                 lastDay=0; lastMonth=0; lastYear=0;
  235.                 }
  236.             else if (! hasSubDirs && (fileName.right(5) != String("_.jpg") ) )
  237.                 {
  238.                 key = new GalleryEntryC(fileName, filex->GetSize(), date->GetDay(), date->GetMonth(), date->GetYear() );
  239.                 if (!key) throw GUIC_MemoryX("Can't allocate memory for key.");
  240.                 fileList.AddTail(*key);
  241.                 entries++;
  242.                 }
  243.             }
  244.         }
  245.     catch (GUIC_SystemX &e) { cerr << "Exception caught while processing directory '" << direx->GetName() << "': " << e.GetMessage() << " from file " << filex->GetName() << "." << endl; }
  246.     delete direx;direx=0; /* Damit der lock auf das Verzeichnis freigegeben wird ! */
  247.  
  248.     if (dirList.Length() ) CreateHTML(dirList,pattern);
  249.     if (fileList.Length()) CreateGallery(fileList,pattern);
  250.  
  251.     while (fileList.Length()) delete (GalleryEntryC *)fileList.Delete(1);
  252.     while ( dirList.Length()) delete (GalleryEntryC *) dirList.Delete(1);
  253.  
  254.     return entries;
  255. }
  256.  
  257. void         main                    (int argc, char **argv)
  258. {
  259.     try
  260.         {
  261.         LONG result[2] = { 0,0 };
  262.         GUIC_ProgramArgsC args;
  263.         if (! args.Fit("STARTDIR,BGPATTERN",result))
  264.             {
  265.             PrintFault(IoErr(),NULL);
  266.             GUIC_SystemC::exit(GUIC_ExitError);
  267.             }
  268.             
  269.         ScanDir((STRPTR)result[0], (STRPTR)result[1]); 
  270.  
  271.         if (errorList.Length())
  272.             {
  273.             cerr << "The following files could not be converted to a thumbnail:" << endl;
  274.             while (errorList.Length())
  275.                 {
  276.                 GalleryEntryC *s = (GalleryEntryC *)errorList.Delete(1);
  277.                 cerr << "- " << s->fileName << endl;
  278.                 delete s;
  279.                 }
  280.             cerr << "Please check them and run 'Gallery' again." << endl;
  281.             }
  282.         }
  283.     catch (GUIC_MemoryX  m) { cerr << "MemoryX() caught: "  << m.GetMessage() << endl; }
  284.     catch (GUIC_GadgetX  g) { cerr << "GadgetX() caught: "  << g.GetMessage() << endl; }
  285.     catch (GUIC_WindowX  w) { cerr << "WindowX() caught: "  << w.GetMessage() << endl; }
  286.     catch (GUIC_ScreenX  s) { cerr << "ScreenX() caught: "  << s.GetMessage() << endl; }
  287.     catch (GUIC_EventX   e) { cerr << "EventX() caught: "   << e.GetMessage() << endl; }
  288.     catch (GUIC_ConvertX c) { cerr << "ConvertX() caught: " << c.GetMessage() << endl; }
  289.     catch (GUIC_SystemX  s) { cerr << "SystemX() caught: "  << s.GetMessage() << endl; }
  290. }
  291.  
  292.