home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 6 / Sonderheft_6-96.iso / pd / libraries / fileid / fileid_library / src / fileident / fileident.c next >
C/C++ Source or Header  |  1996-11-03  |  7KB  |  246 lines

  1. /***************************************LCKG
  2. *                                          *
  3. *      $VER: FileIdent C-Source 1.14       *
  4. *                                          *
  5. *       (C) Copyright 1993,1994,1995       *
  6. *                                          *
  7. *            sTRIDER sYNDIcATE             *
  8. *                                          *
  9. *           All Rights Reserved            *
  10. *                                          *
  11. * Desc.:                                   *
  12. * Example of using the FileID.library      *
  13. *                                          *
  14. * Do what U want with the source.          *
  15. * Note the copyright of the FileID.library *
  16. *                                          *
  17. * Note: Use tabulatorspace 2 to read the   *
  18. *       code as it was written             *
  19. *                                          *
  20. * ---------------------------------------- *
  21. * Name    : FileIdent                      *
  22. * Filename: FileIdent.c                    *
  23. * Version : 1.14                           *
  24. * Part    : FileIdent (1)                  *
  25. * Parts   : 2                              *
  26. * Begin   : 07-Nov-94 20:50:01             *
  27. * Date    : 26-Jul-95 13:45:23             *
  28. *******************************************/
  29.  
  30. char *VerStr="$VER: FileIdent 1.14 bY sTRIDER sYNDIcATE";
  31.  
  32. /* includes ----------------------------------------------------------------- */
  33.  
  34. #include <stdlib.h>
  35. #include <strings.h>
  36. #include <proto/dos.h>
  37. #include <proto/asl.h>
  38. #include <proto/exec.h>
  39. #include <proto/FileID.h>
  40.  
  41. /* defines -------------------------------------------------------------------*/
  42.  
  43. #define OPT_FILE  0
  44. #define OPT_COUNT 2
  45.  
  46. /* structs ------------------------------------------------------------------ */
  47.  
  48. struct FI_FileInfo        *MyFileInfo=NULL;
  49. struct FileIDBase            *FileIDBase=NULL;
  50. struct Library                *AslBase=NULL;
  51. struct FileRequester    *FileRequester;
  52.  
  53. /* Globale ------------------------------------------------------------------ */
  54.  
  55. LONG    *arg1;
  56. char    Buffer[256],
  57.             WorkStr[256];
  58. short    int    error=0;
  59.  
  60. /* prototypes --------------------------------------------------------------- */
  61.  
  62. extern    LONG __asm    StrMid(register __a0 STRPTR, register __a1 STRPTR,
  63.                 register __d1 LONG, register __d0 LONG);
  64.  
  65. void MyPrintf(char *ctl, ...);
  66. void Ident(char*,char*);
  67. void ShutDown(short int);
  68.  
  69. /* main --------------------------------------------------------------------- */
  70.  
  71. /* I don't want this CON: Window in the code, because i won't use stdio.
  72.  * This now reduces the codesize. Also other ANSI-C stuff
  73.  * will be reduced to a minimum. I will use wherever i can the AmigaOS.
  74.  *
  75.  * FileIdent v1.12 size was: 8792 bytes.
  76.  * Now FileIdent v1.14 size is: 2936 bytes.
  77.  */
  78.  
  79. void __autoopenfail(void) { _XCEXIT(0);}
  80.  
  81. void main()
  82. {
  83.     struct    RDArgs    *rda;
  84.     LONG        opts[OPT_COUNT];
  85.     char        **argptr,
  86.                     *curarg;
  87.  
  88.         /* try to open asl.library v37
  89.      */
  90.  
  91.     if(!(AslBase    =    OpenLibrary("asl.library",37L)))
  92.     ShutDown(1); /* Shutdown if fail opening the asl.library
  93.                                 */
  94.  
  95.         /* try to open FileID.library v6
  96.      */
  97.  
  98.     if(!(FileIDBase    =    (struct FileIDBase *)OpenLibrary("FileID.library",6L)))
  99.     ShutDown(2); /* Shutdown if fail opening the FileID.library
  100.                                 */
  101.  
  102.         /* Allocate FileInfo (needed for the Library struct FI_FileInfo)
  103.      */
  104.  
  105.     if(!(MyFileInfo = (struct FI_FileInfo *)FIAllocFileInfo()))
  106.     ShutDown(3); /* Shutdown if fail allocating the struct FI_FileInfo
  107.                                 */
  108.  
  109.     if(rda=ReadArgs("FILES/M",opts,NULL))
  110.     {
  111.         argptr = (char **)opts[OPT_FILE];
  112.  
  113.             /* This is a bit difficult, ReadArgs gives every time a (struct RDArgs)
  114.              * pointer back to you if u use multiple arguments as an template,
  115.              * because no arguments given from an endless number of choices must
  116.              * also be true if no argument ist given. It's not defined! Or can
  117.              * you define the other side: "What is endless???" this seems to be
  118.              * the reason that the pointer is also true if no argument is given.
  119.              *
  120.              * Because of this, u must first look if the first argument has a
  121.              * stringlength higher than zero bytes. If not, like here at first,
  122.              * no arguments are given.
  123.              */
  124.  
  125.         if(strlen(1L+*argptr)<1)
  126.         {
  127.             MyPrintf("\nFileIdent 1.14 (C) Copyright 1993,1994,1995 by sTRIDER oF sYNDIcATE.\n\nFile identify with the use of FileID.library v6.0+\n(C) Copyright 1993,1994,1995 BLOODROCK of SYNDICATE.\n");
  128.  
  129.             if(FileRequester = AllocAslRequestTags(ASL_FileRequest,
  130.             ASLFR_DoMultiSelect, TRUE,
  131.             ASLFR_DoPatterns, TRUE,
  132.             ASLFR_DoSaveMode, FALSE,
  133.             ASLFR_DrawersOnly, FALSE,
  134.             ASLFR_TitleText, (ULONG)"Select File(s) to be identified...",
  135.             ASLFR_InitialDrawer, (ULONG)"PROGDIR:",
  136.             TAG_DONE))
  137.             {
  138.               if(AslRequestTags(FileRequester,TAG_DONE))
  139.                 {
  140.                     if(FileRequester->rf_NumArgs<2) Ident(FileRequester->rf_Dir,FileRequester->rf_File);
  141.                     else
  142.                     {
  143.                             /* More files are selected, so handle them all.
  144.                              */
  145.  
  146.                         int FileNr=FileRequester->rf_NumArgs;
  147.  
  148.                         do
  149.                         {
  150.                             Ident(FileRequester->rf_Dir,FileRequester->rf_ArgList->wa_Name);
  151.                             FileRequester->rf_ArgList++;
  152.                             FileNr--;
  153.                         }
  154.                         while(FileNr);
  155.  
  156.                             /* Be sure to set the pointer back, or the asl.library will free
  157.                              * the ArgLists somewhere in your RAM! It's not good to free
  158.                              * memory behind the real allocated ArgList. U can also take
  159.                              * your own pointer: struct WBArg *fargs=FileRequester->rf_ArgList;
  160.                              * to be sure nothing bad can happen!
  161.                              */
  162.  
  163.                         FileRequester->rf_ArgList -= FileRequester->rf_NumArgs;
  164.                     }
  165.               } else MyPrintf("\nNothing selected.");
  166.                 FreeAslRequest(FileRequester);
  167.           }
  168.         }
  169.  
  170.             /* Here i know that arguments are given. As long as they are, i will
  171.              * use them now all in following while()
  172.              */
  173.  
  174.         else while(curarg = *argptr++) Ident(curarg,"");
  175.  
  176.         MyPrintf("\n\n");
  177.         FreeArgs(rda);
  178.     }
  179.  
  180.     ShutDown(error);
  181. }
  182.  
  183.  
  184. /* procedures --------------------------------------------------------------- */
  185.  
  186. void MyPrintf(char *ctl, ...)
  187. {
  188.     arg1 = (long *)(&ctl + 1);
  189.     RawDoFmt(ctl, arg1, (void (*))"\x16\xc0\x4e\x75",Buffer);
  190.     Write(Output(), Buffer, strlen(Buffer));
  191. }
  192.  
  193. void Ident(char *InDir,char *InFile)
  194. {
  195.     if(strlen(InDir)>225) InDir[225]='\0';
  196.     strcpy(WorkStr,InDir);
  197.     if((WorkStr[strlen(WorkStr)-1]!=':') && (WorkStr[strlen(WorkStr)-1]!='/') && strlen(InFile)>0) strcat(WorkStr,"/");
  198.     strcat(WorkStr,InFile);
  199.  
  200.     if(!(error=FIIdentifyFromName((struct FI_FileInfo *)MyFileInfo,WorkStr)))
  201.         MyPrintf("\n%-40s %s %ld %ld %ld %ld %ld",WorkStr,MyFileInfo->FI_Description,MyFileInfo->FI_ID,MyFileInfo->FI_GlobalFileClass,MyFileInfo->FI_DLPackNum,MyFileInfo->FI_DLPackType,MyFileInfo->FI_PackFlags);
  202.     else MyPrintf("\n%-40s %s",FilePart(WorkStr),MyFileInfo->FI_Description);
  203. }
  204.  
  205. void ShutDown(short int error)
  206. {
  207.     if(error>0) {
  208.         MyPrintf("\nError %ld! ",error);
  209.  
  210.         switch( error )
  211.         {
  212.             case 1: MyPrintf("Can't open asl.library!\n\n");
  213.                             error=RETURN_FAIL;
  214.             break;
  215.             case 2: MyPrintf("Can't open FileID.library!\n\n");
  216.                             error=RETURN_FAIL;
  217.             break;
  218.             case 3: MyPrintf("Can't allocate FI_FileInfo!\n\n");
  219.                             error=RETURN_FAIL;
  220.             break;
  221.             deafult: error=RETURN_ERROR;
  222.             break;
  223.         }
  224.         MyPrintf("\n");
  225.     }
  226.  
  227.         /* Free allocated memory
  228.          */
  229.  
  230.     if(MyFileInfo)    FIFreeFileInfo((struct FI_FileInfo *)MyFileInfo);
  231.  
  232.         /* Close FileID.library v6.0
  233.          */
  234.  
  235.     if(FileIDBase)    CloseLibrary((struct Library *)FileIDBase);
  236.  
  237.         /* Close asl.library
  238.          */
  239.  
  240.     if(AslBase)            CloseLibrary(AslBase);
  241.  
  242.     exit(error);
  243. }
  244.  
  245. /* EOF ---------------------------------------------------------------------- */
  246.