home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / SA_Examples / nonvolatile / GetCopyNV.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-31  |  4.2 KB  |  181 lines

  1. /* GetCopyNV.c
  2.  * 
  3.  * 
  4.  * test nonvolatile.library/GetCopyNV()
  5.  * 
  6.  */
  7.  
  8. /* Includes --------------------------------------------- */
  9. #include <exec/types.h>
  10. #include <exec/libraries.h>
  11. #include <exec/memory.h>
  12. #include <dos/dos.h>
  13. #include <dos/rdargs.h>
  14. #include <libraries/nonvolatile.h>
  15. #include <clib/exec_protos.h>
  16. #include <clib/dos_protos.h>
  17. #include <clib/nonvolatile_protos.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. #include <pragmas/nonvolatile_pragmas.h>
  22. #include "nv.h"
  23.  
  24. /* Defines ------------------------------------------ */
  25. #define PROGNAME "GetCopyNV"
  26. #define ERRMSG_LIBNOOPEN "Couldn't open %s V%ld (or better)!\n"
  27. #define TEMPLATE "LENGTH/A/N,NAME=APPNAME/K,ITEM=ITEMNAME/K,NN=NULLNAME/S,NI=NULLITEM/S,HEX=HEXDUMP/S,KILLREQ/S"
  28.  
  29.  
  30. /* Structs ------------------------------ */
  31. struct Opts {
  32.     ULONG   *length;        /* length of data.  Use GetNVList test prog to get this */
  33.     STRPTR  name;           /* app name */
  34.     STRPTR  item;           /* item name */
  35.     LONG    nullName;       /* test with null app name */
  36.     LONG    nullItem;       /* test with null item name */
  37.     LONG    hexDump;        /* do hex dump instead of ascii dump */
  38.     LONG    killReq;        /* eliminate requesters */
  39. };
  40.  
  41. /* Protos ------------------------------------------ */
  42. static VOID GoodBye(int);
  43. static LONG doInit(VOID);
  44. static VOID dumpHex(STRPTR,ULONG);
  45. static VOID dumpData(STRPTR,ULONG);
  46.  
  47. /* Globals --------------------------------------- */
  48. struct Opts     opts;
  49. struct RDArgs  *rdargs;
  50. struct Library *NVBase;
  51. STRPTR          data;   /* returned data from GetCopyNV() */
  52.  
  53. VOID main(int argc, UBYTE *argv[]) {
  54.     STRPTR name, item;        /* appname, itemname */
  55.     int rc = RETURN_OK;
  56.     BOOL killReq;
  57.  
  58.     if (!doInit()) {
  59.         GoodBye(RETURN_FAIL);
  60.     }
  61.  
  62.     name = ((opts.name) ? opts.name : (STRPTR)DEFAULT_APP_NAME);
  63.     item = ((opts.item) ? opts.item : (STRPTR)DEFAULT_ITEM_NAME);
  64.     killReq = ((opts.killReq) ? TRUE : FALSE);
  65.  
  66.  
  67.     if (opts.nullName) {
  68.         name = NULL;
  69.     }
  70.     if (opts.nullItem) {
  71.         item = NULL;
  72.     }
  73.  
  74.     Printf("Calling GetCopyNV('%s', '%s')\n", name, item);
  75.     if (data = GetCopyNV(name, item, killReq)) {
  76.         if (opts.hexDump) {
  77.             dumpHex(data, *opts.length);
  78.         }
  79.         else {
  80.             dumpData(data, *opts.length);
  81.         }
  82.     }
  83.     else {
  84.         PrintFault(IoErr(), NULL);
  85.         PutStr("GetCopyNV() returned NULL!\n");
  86.         rc = RETURN_WARN;
  87.     }
  88.     
  89.  
  90.     GoodBye(rc);
  91. }
  92.  
  93. /* dumpHex ==============================================
  94.     do hex dump of data, 1 char at a time...
  95.     should really do it a ulong at a time, but it doesn't matter much.
  96. */
  97. static VOID dumpHex(STRPTR s, ULONG slen) {
  98.     ULONG i;
  99.  
  100.     if (!s || !slen)
  101.         return;
  102.  
  103.     for (i = 0; i < slen; i++) {
  104.         Printf("%02lx", (ULONG)(s[i]));
  105.         if (!(i+1 % 8)) {
  106.             PutStr(" ");
  107.         }
  108.         if (!(i+1 % 32)) {
  109.             PutStr("\n");
  110.         }
  111.         if (CheckSignal(SIGBREAKF_CTRL_C)) {
  112.             return;
  113.         }
  114.     }
  115.     PutStr("\n");
  116.  
  117.     return;
  118. }
  119.  
  120.  
  121. /* dumpData ==============================================
  122.     do dump of data (consider it non-null-terminated ascii)
  123. */
  124. static VOID dumpData(STRPTR s, ULONG len) {
  125.  
  126.     if (!s || !len)
  127.         return;
  128.  
  129.     PutStr("\"");
  130.     Flush(Output());
  131.  
  132.     Write(Output(), s, (LONG)len);
  133.  
  134.     Flush(Output());
  135.     PutStr("\"\n");
  136.  
  137.     return;
  138. }
  139.  
  140. /* GoodBye ===============================================
  141.    Clean-exit routine.
  142.  */
  143. static VOID GoodBye(int rc) {
  144.  
  145.     if (data) {
  146.         PutStr("Calling FreeNVData(data)...\n");
  147.         FreeNVData(data);
  148.     }
  149.  
  150.     if (NVBase) {
  151.         CloseLibrary(NVBase);
  152.     }
  153.  
  154.     if (rdargs) {
  155.         FreeArgs(rdargs);
  156.     }
  157.  
  158.     exit(rc);
  159. }
  160.  
  161. /* doInit =============================================
  162.  * Open libraries, call ReadArgs() if necessary.
  163.  * Returns TRUE for success, FALSE otherwise.
  164.  */
  165. static LONG doInit(VOID) {
  166.     rdargs = ReadArgs(TEMPLATE, (LONG *)&opts, NULL);
  167.     if (!rdargs) {
  168.         PrintFault(IoErr(), PROGNAME);
  169.         return(FALSE);
  170.     }
  171.  
  172.     NVBase = OpenLibrary("nonvolatile.library", 40L);
  173.     if (!NVBase) {
  174.         Printf(ERRMSG_LIBNOOPEN, "nonvolatile.library", 40L);
  175.         return(FALSE);
  176.     }
  177.  
  178.     return(TRUE);
  179. }
  180.  
  181.