home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.5 / Examples / Printer / PrinterUnitList / PrinterUnitList.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-30  |  2.8 KB  |  129 lines

  1. /*
  2.  * COPYRIGHT:
  3.  *
  4.  *   Unless otherwise noted, all files are Copyright (c) 1999 Amiga, Inc.
  5.  *   All rights reserved.
  6.  *
  7.  * DISCLAIMER:
  8.  *
  9.  *   This software is provided "as is". No representations or warranties
  10.  *   are made with respect to the accuracy, reliability, performance,
  11.  *   currentness, or operation of this software, and all use is at your
  12.  *   own risk. Neither Amiga nor the authors assume any responsibility
  13.  *   or liability whatsoever with respect to your use of this software.
  14.  */
  15.  
  16.  
  17. /* includes */
  18. #include <dos/dos.h>
  19. #include <libraries/iffparse.h>
  20. #include <prefs/prefhdr.h>
  21. #include <prefs/printertxt.h>
  22.  
  23. /* prototypes */
  24. #include <clib/iffparse_protos.h>
  25. #include <clib/dos_protos.h>
  26. #include <clib/alib_protos.h>
  27. #include <clib/alib_stdio_protos.h>
  28. #include <string.h>
  29.  
  30. BPTR stdout;
  31.  
  32. /*****************************************************************************/
  33.  
  34. #define IFFPrefChunkCnt 2
  35. static LONG IFFPrefChunks[] =
  36. {
  37.     ID_PREF, ID_PRHD,
  38.     ID_PREF, ID_PDEV,
  39. };
  40.  
  41. void ReadUnitName(char *filename, char name[], int unitnum)
  42. {
  43.     BPTR fp;
  44.     BOOL ok;
  45.     struct IFFHandle *iff;
  46.     struct ContextNode *cn;
  47.     struct PrefHeader phead;
  48.     struct PrinterDeviceUnitPrefs pdev;
  49.  
  50.     sprintf(name,"Unit %ld",unitnum);
  51.     if (fp = Open(filename, MODE_OLDFILE))
  52.     {
  53.         if (iff = AllocIFF())
  54.         {
  55.             iff->iff_Stream = fp;
  56.             InitIFFasDOS(iff);
  57.  
  58.             if (!OpenIFF(iff, IFFF_READ))
  59.             {
  60.                 if (!ParseIFF(iff, IFFPARSE_STEP))
  61.                 {
  62.                     cn = CurrentChunk(iff);
  63.                     if (cn->cn_ID == ID_FORM && cn->cn_Type == ID_PREF)
  64.                     {
  65.                         if (!StopChunks(iff, IFFPrefChunks, IFFPrefChunkCnt))
  66.                         {
  67.                             ok = TRUE;
  68.                             while (ok)
  69.                             {
  70.                                 if (ParseIFF(iff, IFFPARSE_SCAN))
  71.                                     break;
  72.                                 cn = CurrentChunk(iff);
  73.                                 if (cn->cn_Type == ID_PREF)
  74.                                 {
  75.                                     switch (cn->cn_ID)
  76.                                     {
  77.                                         case ID_PRHD:
  78.                                             if (ReadChunkBytes(iff, &phead, sizeof(struct PrefHeader)) != sizeof(struct PrefHeader))
  79.                                             {
  80.                                                 ok = FALSE;
  81.                                                 break;
  82.                                             }
  83.                                             if (phead.ph_Version != 0)
  84.                                             {
  85.                                                 ok = FALSE;
  86.                                                 break;
  87.                                             }
  88.                                             break;
  89.                                         case ID_PDEV:
  90.                                             if (ReadChunkBytes(iff, &pdev, sizeof(pdev)) == sizeof(pdev))
  91.                                             {
  92.                                                 if (pdev.pd_UnitName[0])
  93.                                                     strcpy(name,pdev.pd_UnitName);
  94.                                             }
  95.                                             break;
  96.                                         default:
  97.                                             break;
  98.                                     }
  99.                                 }
  100.                             }
  101.                         }
  102.                     }
  103.                 }
  104.                 CloseIFF(iff);
  105.             }
  106.             FreeIFF(iff);
  107.         }
  108.         Close(fp);
  109.     }
  110. }
  111.  
  112. void main(void)
  113. {
  114.     char filename[30];
  115.     char name[UNITNAMESIZE];
  116.     int i;
  117.  
  118.     stdout = Output();
  119.     ReadUnitName("ENV:Sys/printer.prefs",name,0);
  120.     printf("Printer Unit Names:\n 0: %s\n",name);
  121.     strcpy(filename,"ENV:Sys/printerN.prefs");
  122.     for (i = 1; i < 10; i++)
  123.     {
  124.         filename[15] = '0' + i;
  125.         ReadUnitName(filename,name,i);
  126.         printf(" %ld: %s\n",i,name);
  127.     }
  128. }
  129.