home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / showprefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  8.1 KB  |  219 lines

  1. ;/* showprefs.c - Execute me to compile me with SAS C 5.10
  2. LC -b0 -d0 -cfis -v -j73 showprefs.c
  3. Blink FROM showprefs.o TO showprefs LIBRARY LIB:Amiga.lib
  4. quit
  5. **
  6. ** The following example shows a way to read a Preferences file.
  7. **
  8. ** showprefs.c - parse and show some info from an IFF Preferences file
  9. ** NOTE: This example requires upcoming 2.1 prefs/ include files.
  10. **
  11. ** IMPORTANT!! This example is not linked with startup code (eg. c.o).
  12. ** It uses strictly direct AmigaDOS stdio, and also demonstrates
  13. ** direct ReadArgs argument parsing.  Therefore it is a CLI-only
  14. ** example.  If launched from Workbench, packet errors would occur
  15. ** since the WbStartup message is still sitting in the process's
  16. ** pr_MsgPort, and the code would never be unloaded from memory.
  17. */
  18.  
  19. #include <exec/types.h>
  20. #include <dos/dos.h>
  21. #include <libraries/dos.h>
  22. #include <libraries/iffparse.h>
  23. #include <prefs/prefhdr.h>
  24. #include <prefs/font.h>
  25. #include <prefs/icontrol.h>
  26. #include <prefs/input.h>
  27. #include <prefs/overscan.h>
  28. #include <prefs/printergfx.h>
  29. #include <prefs/printertxt.h>
  30. #include <prefs/screenmode.h>
  31. #include <prefs/serial.h>
  32.  
  33. #include <clib/exec_protos.h>
  34. #include <clib/dos_protos.h>
  35. #include <clib/iffparse_protos.h>
  36.  
  37. struct ExecBase *SysBase;
  38. struct Library *DOSBase;
  39. struct Library *IFFParseBase;
  40.  
  41. static UBYTE *IFFErrTxt[] = {
  42.   "EOF",    /* (end of file, not an error) */
  43.   "EOC",     /* (end of context, not an error) */
  44.   "no lexical scope",
  45.   "insufficient memory",
  46.   "stream read error",
  47.   "stream write error",
  48.   "stream seek error",
  49.   "file corrupt",
  50.   "IFF syntax error",
  51.   "not an IFF file",
  52.   "required call-back hook missing",
  53.   NULL,     /* (return to client, never shown) */
  54. };
  55.  
  56. LONG main(void)
  57. {
  58.   struct RDArgs *readargs = NULL;
  59.   LONG rargs[2];
  60.   struct IFFHandle *iffhandle;
  61.   struct ContextNode *cnode;
  62.   struct StoredProperty *hdrsp;
  63.   struct StoredProperty *sp;
  64.   UBYTE *filename = NULL;
  65.   LONG ifferror, error = 0, rc = RETURN_OK;
  66.  
  67.   /* We must set up SysBase (we are not linked with startup code) */
  68.   SysBase = (*((struct Library **) 4));
  69.  
  70.   /* This no-startup-code example may not be used from Workbench */
  71.   if ((((struct Process *)FindTask(NULL))->pr_CLI)==NULL)
  72.         return(RETURN_FAIL);
  73.  
  74.   if (DOSBase = OpenLibrary("dos.library", 37)) {
  75.     if (IFFParseBase = OpenLibrary ("iffparse.library", 37)) {
  76.  
  77.       readargs = ReadArgs("FILE/A", rargs, NULL);
  78.       if( (readargs) && (rargs[0]) ) {
  79.  
  80.         filename = (UBYTE *)rargs[0];
  81.  
  82.         /* allocate an IFF handle */
  83.         if (iffhandle = AllocIFF()) {
  84.           /* Open the file for reading */
  85.           if (iffhandle->iff_Stream = (LONG)Open(filename, MODE_OLDFILE)) {
  86.             /* initialize the iff handle */
  87.             InitIFFasDOS (iffhandle);
  88.             if ((ifferror = OpenIFF (iffhandle, IFFF_READ)) == 0) {
  89.               PropChunk(iffhandle, ID_PREF, ID_PRHD);
  90.  
  91.               PropChunk(iffhandle, ID_PREF, ID_FONT);
  92.               PropChunk(iffhandle, ID_PREF, ID_ICTL);
  93.               PropChunk(iffhandle, ID_PREF, ID_INPT);
  94.               PropChunk(iffhandle, ID_PREF, ID_OSCN);
  95.               PropChunk(iffhandle, ID_PREF, ID_PGFX);
  96.               PropChunk(iffhandle, ID_PREF, ID_PTXT);
  97.               PropChunk(iffhandle, ID_PREF, ID_SCRM);
  98.               PropChunk(iffhandle, ID_PREF, ID_SERL);
  99.  
  100.               for (;;) {
  101.                 ifferror = ParseIFF(iffhandle, IFFPARSE_STEP);
  102.  
  103.                  if (ifferror == IFFERR_EOC)
  104.                    continue;
  105.                 else if (ifferror)
  106.                   break;
  107.  
  108.                 /* Do nothing is this is a PrefHeader chunk,
  109.                  * we'll pop it later when there is a pref
  110.                  * chunk.
  111.                  */
  112.                 if (cnode = CurrentChunk(iffhandle))
  113.                   if (cnode->cn_ID == ID_PRHD || cnode->cn_ID == ID_FORM)
  114.                     continue;
  115.  
  116.                 /* Get the preferences header, stored previously */
  117.                 hdrsp = FindProp(iffhandle, ID_PREF, ID_PRHD);
  118.  
  119.                 if (sp = FindProp(iffhandle, ID_PREF, ID_FONT)) {
  120.                   Printf("FrontPen:  %ld\n",
  121.                         ((struct FontPrefs *)sp->sp_Data)->fp_FrontPen);
  122.                   Printf("BackPen:   %ld\n",
  123.                         ((struct FontPrefs *)sp->sp_Data)->fp_BackPen);
  124.                   Printf("DrawMode:  %ld\n",
  125.                         ((struct FontPrefs *)sp->sp_Data)->fp_DrawMode);
  126.                   Printf("Font:      %s\n",
  127.                         (LONG)((struct FontPrefs *)sp->sp_Data)->fp_Name);
  128.                   Printf("ta_YSize:  %ld\n",
  129.                         ((struct FontPrefs *)sp->sp_Data)->fp_TextAttr.ta_YSize);
  130.                   Printf("ta_Style:  %ld\n",
  131.                         ((struct FontPrefs *)sp->sp_Data)->fp_TextAttr.ta_Style);
  132.                   Printf("ta_Flags:  %ld\n",
  133.                         ((struct FontPrefs *)sp->sp_Data)->fp_TextAttr.ta_Flags);
  134.                 } else   if (sp = FindProp(iffhandle, ID_PREF, ID_ICTL)) {
  135.                   Printf("TimeOut:   %ld\n",
  136.                         ((struct IControlPrefs *)sp->sp_Data)->ic_TimeOut);
  137.                   Printf("MetaDrag:  %ld\n",
  138.                         ((struct IControlPrefs *)sp->sp_Data)->ic_MetaDrag);
  139.                   Printf("WBtoFront: %ld\n",
  140.                         ((struct IControlPrefs *)sp->sp_Data)->ic_WBtoFront);
  141.                   Printf("FrontToBack: %ld\n",
  142.                         ((struct IControlPrefs *)sp->sp_Data)->ic_FrontToBack);
  143.                   Printf("ReqTrue:    %ld\n",
  144.                         ((struct IControlPrefs *)sp->sp_Data)->ic_ReqTrue);
  145.                   Printf("ReqFalse:    %ld\n",
  146.                         ((struct IControlPrefs *)sp->sp_Data)->ic_ReqFalse);
  147.                   /* etc */
  148.                 } else if (sp = FindProp(iffhandle, ID_PREF, ID_INPT)) {
  149.                   Printf("PointerTicks:      %ld\n",
  150.                         ((struct InputPrefs *)sp->sp_Data)->ip_PointerTicks);
  151.                   Printf("DoubleClick/Secs:  %ld\n",
  152.                         ((struct InputPrefs *)sp->sp_Data)->ip_DoubleClick.tv_secs);
  153.                   Printf("DoubleClick/Micro: %ld\n",
  154.                         ((struct InputPrefs *)sp->sp_Data)->ip_DoubleClick.tv_micro);
  155.                   /* etc */
  156.                 } else if (sp = FindProp(iffhandle, ID_PREF, ID_OSCN)) {
  157.                   Printf("DisplayID:  0x%lx\n",
  158.                         ((struct OverscanPrefs *)sp->sp_Data)->os_DisplayID);
  159.                   /* etc */
  160.                 } else if (sp = FindProp(iffhandle, ID_PREF, ID_PGFX)) {
  161.                   Printf("Aspect:     %ld\n",
  162.                         ((struct PrinterGfxPrefs *)sp->sp_Data)->pg_Aspect);
  163.                   /* etc */
  164.                 } else if (sp = FindProp(iffhandle, ID_PREF, ID_PTXT)) {
  165.                   Printf("Driver:     %s\n",
  166.                         (LONG)((struct PrinterTxtPrefs *)sp->sp_Data)->pt_Driver);
  167.                   /* etc */
  168.                 } else if (sp = FindProp(iffhandle, ID_PREF, ID_SCRM)) {
  169.                   Printf("DisplayID:  0x%lx\n",
  170.                         ((struct ScreenModePrefs *)sp->sp_Data)->sm_DisplayID);
  171.                   /* etc */
  172.                 } else if (sp = FindProp(iffhandle, ID_PREF, ID_SERL)) {
  173.                   Printf("BaudRate:   %ld\n",
  174.                         ((struct SerialPrefs *)sp->sp_Data)->sp_BaudRate);
  175.                   /* etc */
  176.                 }
  177.               }
  178.  
  179.               CloseIFF(iffhandle);
  180.             }
  181.  
  182.             if (ifferror != IFFERR_EOF) {
  183.               rargs[1] = (LONG)IFFErrTxt[-ifferror - 1];
  184.               VFPrintf(Output(), "%s: %s\n", rargs);
  185.               rc = RETURN_FAIL;
  186.             }
  187.  
  188.             Close(iffhandle->iff_Stream);
  189.           } else
  190.             error = IoErr();
  191.  
  192.           FreeIFF(iffhandle);
  193.         } else {
  194.           VFPrintf(Output(), "Can't allocate IFF handle\n", NULL);
  195.           rc = RETURN_FAIL;
  196.         }
  197.       } else
  198.         error = IoErr();
  199.       CloseLibrary(IFFParseBase);
  200.  
  201.  
  202.       SetIoErr(error);
  203.       if (error) {
  204.         rc = RETURN_FAIL;
  205.         PrintFault(error, filename ? filename : "");
  206.       }
  207.     }
  208.  
  209.     if(readargs) FreeArgs(readargs);
  210.     CloseLibrary(DOSBase);
  211.  
  212.   } else {
  213.       rc = RETURN_FAIL;
  214.       Write(Output(), "Kickstart 2.0 required\n", 23);
  215.     }
  216.  
  217.   return(rc);
  218. }
  219.