home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / System / ScalosPrefs / examples / C / multiple.c < prev    next >
C/C++ Source or Header  |  1980-07-31  |  3KB  |  95 lines

  1. /*  Simple example of using multiple data items per tag. */
  2.  
  3. /* Always included first */
  4. #include <exec/types.h>
  5.  
  6. /* So we can use the functions from these libraries */
  7. #include <proto/dos.h>
  8. #include <proto/exec.h>
  9. #include <proto/preferences.h>
  10.  
  11. /* For the MAKE_ID macro */
  12. #include <libraries/iffparse.h>
  13.  
  14. /* For the PrefsStruct structure */
  15. #include <scalos/preferences.h>
  16.  
  17. /* some standard stuff */
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21.  
  22. /* Pointer to the preferences.library base */
  23. struct Library  *PreferencesBase;
  24.  
  25.  
  26. int main(void)
  27. {
  28.     STRPTR  prefname = "Example";       /* Name to refer to our PrefsHandle with */
  29.     APTR    prefhandle;                 /* Handle to our preferences set */
  30.     ULONG   ID, Tag;                    /* ID and Tag are used to access a specific preferences item */
  31.     char    temp[128];                  /* Space to retrieve the data from the PrefsHandle */
  32.     LONG    en;                         /* The current entry number within the Tag */
  33.  
  34.  
  35.     if(NULL != (PreferencesBase = OpenLibrary("preferences.library", 39)) )
  36.     {
  37.         /* First we need to allocate a PrefsHandle by name */
  38.         if(NULL != (prefhandle = AllocPrefsHandle(prefname)) )
  39.         {
  40.             printf("PrefsHandle successfully allocated\n");
  41.  
  42.             /* We need an ID and Tag value to refer to a specific preference.
  43.              * so we create that here (you could always just put them in the library calls).
  44.              * Remember that the ID must be created from 4 ASCII characters and the Tag
  45.              * cannot be 0
  46.              */
  47.             ID = MAKE_ID('M','A','I','N');
  48.             Tag = 2;
  49.  
  50.             /* We keep track of the current entry number so that we can keep on adding
  51.              * at the end of the list of data items. You can of course pass any entry
  52.              * number and the data will be inserted in the correct place in the list,
  53.              * unless the data item already exists and the size is the same, in which
  54.              * case the old data will be overwritten
  55.              */
  56.             en = 0;
  57.  
  58.             /* A loop which allows the user to enter some data to be stored */
  59.             do
  60.             {
  61.                 /* Prompt user and get a string from them */
  62.                 printf("Enter a string or leave blank to quit and press return:\n    ");
  63.                 gets(temp);
  64.  
  65.                 /* If the string is empty we do not do anything with it, as that
  66.                  * is the condition for quitting the loop
  67.                  */
  68.                 if(strlen(temp) > 0)
  69.                 {
  70.                     SetEntry(prefhandle, ID, Tag, temp, strlen(temp)+1, en);
  71.                     en++;
  72.                 }
  73.             } while(strlen(temp) > 0);
  74.  
  75.  
  76.             /* Now we will show each data item in turn, as well as removing it,
  77.              * this means we do not need to play around with entry numbers here
  78.              */
  79.             while(GetEntry(prefhandle, ID, Tag, temp, 128, 0) > 0)
  80.             {
  81.                 printf("Retrieved: %s\n", temp);
  82.                 if(RemEntry(prefhandle, ID, Tag, 0)) printf("Entry removed\n"); else printf("Entry not removed\n");
  83.             }
  84.  
  85.  
  86.             /* Finally, we free the PrefsHandle */
  87.             FreePrefsHandle(prefhandle);
  88.         }
  89.  
  90.         CloseLibrary(PreferencesBase);
  91.     }
  92.     return(0);
  93. }
  94.  
  95.