home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / edu / DITOdev.lha / DITOdev / Library / Examples / ShellDict.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-12  |  2.5 KB  |  133 lines

  1. /*
  2.  *   ShellDict © Dirk Holtwick, 1996
  3.  *
  4.  *   Small example to demonstrate how to write a program
  5.  *   wich uses the DICT files.
  6.  */
  7.  
  8. /// INCLUDE & DEFS
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #include <proto/exec.h>
  13. #include <proto/dito.h>
  14.  
  15. struct Library *DitoBase;
  16. ///
  17.  
  18. /// HandleDITOError()
  19.  
  20. // Easy routine for error handling
  21. int HandleDITOError(int error)
  22. {
  23.     switch(error)
  24.     {
  25.         case DITO_Error_FileNotFound:
  26.             puts("File not found!");
  27.             break;
  28.         case DITO_Error_NoDitoDatabase:
  29.             puts("No DITO Database!");
  30.             break;
  31.         case DITO_Error_WrongLanguage:
  32.             puts("Wrong Language!");
  33.             break;
  34.         case DITO_Error_Memory:
  35.             puts("Not enough memory!");
  36.             break;
  37.     }
  38.     return(error);
  39. }
  40. ///
  41. /// Main()
  42.  
  43. // Main program
  44. main(int argc, char *argv[])
  45. {
  46.     struct DITO_UserInfo    ui;
  47.     struct DITO_Dict        *dt;
  48.     struct DITO_Database    *data;
  49.     struct DITO_Entry       *entry;
  50.     ULONG  n;
  51.     char   pat[255];
  52.  
  53.     if(argc==2)
  54.     {
  55.         if(DitoBase = OpenLibrary(DITO_NAME, DITO_VMIN))
  56.         {
  57.  
  58.             // You have to create a database before adding or
  59.             // loading entries to the structure. Do not try to
  60.             // init the structure by yourself!
  61.  
  62.             DITO_GetUserInfo(&ui);
  63.  
  64.             if(ui.nr) printf("You are the registred user: %s, #%d\n\n",ui.name,ui.nr);
  65.             else puts("You are not registred. Please do it!\n");
  66.  
  67.             if(data = DITO_CreateDatabase())
  68.             {
  69.                 // You may now add entries to the database, e.g.
  70.                 // with DITO_LoadDatabase()
  71.  
  72.                 printf("     Loading Dictionary ...\n\n");
  73.  
  74.                 if(dt = DITO_OpenDict (argv[1]))
  75.                 {
  76.  
  77.                     printf("     To quit just press RETURN.\n");
  78.  
  79.                     do
  80.                     {
  81.                         // Delete all entries
  82.  
  83.                         DITO_InitDatabase(data);
  84.  
  85.                         // Ask pattern
  86.  
  87.                         printf ("\n     \033[1mSEARCHPATTERN: \033[0m");
  88.                         gets(pat);
  89.  
  90.                         if(pat[0])
  91.                         {
  92.                             // Load entries that fit the pattern
  93.  
  94.                             DITO_LoadDictData(data, dt, pat, DITO_Cmp_First);
  95.  
  96.                             // Show all found entries
  97.  
  98.                             printf("\n");
  99.  
  100.                             for (n=0; n<data->sum; n++)
  101.                             {
  102.                                 entry = DITO_GetPtr(data, n);
  103.  
  104.                                 printf(
  105.                                     "%3d. \033[1m%s,\033[0m %s\n",
  106.                                     n + 1,
  107.                                     DITO_GetStr (entry, DITO_Str_Word),
  108.                                     DITO_GetStr (entry, DITO_Str_Translation)
  109.                                 );
  110.                             }
  111.  
  112.                         }
  113.                     }
  114.                     while (pat[0]);
  115.  
  116.                     // Don't forget to close the DICT
  117.  
  118.                     DITO_CloseDict(dt);
  119.                 }
  120.  
  121.                 // Do not forget to dispose the DITO_Database!
  122.  
  123.                 DITO_DisposeDatabase(data);
  124.             }
  125.  
  126.             CloseLibrary(DitoBase);
  127.         }
  128.         else puts("Couldn't open actual version of dito.library.");
  129.     }
  130.     else puts("USAGE: ShellDict [file.dict]");
  131. }
  132. ///
  133.