home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / CONFIG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  6.6 KB  |  196 lines

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // config.c
  4. #include "snipit.h"
  5.  
  6. static void DisplayCfgFile(pCHAR CfgPath, INT16 Level) ;
  7.  
  8. //=====================================================================
  9. //  Function:
  10. //          Configuration();
  11. //
  12. //  Description:
  13. //          This example dumps the information contained in the
  14. //          IDAPI configuration file, as well as displaying which
  15. //          configuration file is being used.
  16. //=====================================================================
  17. void
  18. Configuration (void)
  19. {
  20.     SYSConfig   sysConfig;  // Used for storing the system configuration
  21.                             //   information - used to determine the
  22.                             //   location of the configuration file
  23.     DBIResult   rslt;       // Return value from IDAPI functions
  24.  
  25.     Screen("*** Configuration Example ***\r\n");
  26.  
  27.     BREAK_IN_DEBUGGER();
  28.  
  29.     Screen("    Initializing IDAPI...");
  30.     rslt = DbiInit(NULL);
  31.     if (ChkRslt(rslt, "Init") != DBIERR_NONE)
  32.     {
  33.         Screen("\r\n*** End of Example ***");
  34.         return;
  35.     }
  36.  
  37.     // Turn on trace information if the debug layer is anabled (DLLSWAP.EXE).
  38.     DbiDebugLayerOptions(DEBUGON | OUTPUTTOFILE, "SNIPIT.INF");
  39.  
  40.     // Specify where temporary files are placed.
  41.     rslt = DbiSetPrivateDir(szPrivDirectory);
  42.     ChkRslt(rslt, "SetPrivateDir");
  43.  
  44.     // Determine which configuration file is being used.
  45.     rslt = DbiGetSysConfig(&sysConfig);
  46.     ChkRslt(rslt, "GetSysConfig");
  47.  
  48.     Screen("\r\n    Display the information in the currently used"
  49.            " configuration file:\r\n\r\n\t\t %s\r\n", sysConfig.szIniFile);
  50.  
  51.     // Call the recursive function which dumps information from the
  52.     //   configuration file.                 
  53.     DisplayCfgFile ("\\", 0) ;
  54.  
  55.     // Turn off trace information.
  56.     DbiDebugLayerOptions(0, NULL);
  57.  
  58.     // Close IDAPI.
  59.     Screen("\r\n    Exiting IDAPI...");
  60.     rslt = DbiExit();
  61.     ChkRslt(rslt, "Exit");
  62.  
  63.     Screen("\r\n*** End of Example ***");
  64. }
  65.  
  66. //=====================================================================
  67. //  Function:
  68. //          DisplayCfgFile(CfgPath, Level);
  69. //
  70. //  Input:  CfgPath -   Path within the CFG file to the desired
  71. //                      option within the configuration file. Starts
  72. //                      at "\\" for the root of the configuration tree
  73. //          Level   -   Depth of Recursion
  74. //
  75. //  Return: None
  76. //
  77. //  Desc:   This recursive function is used to display all information
  78. //          within the configuration file.
  79. //=====================================================================
  80. void
  81. DisplayCfgFile (pCHAR CfgPath, INT16 Level)
  82. {
  83.     DBIResult   rslt;       // Value to return
  84.     hDBICur     hCur;       // Handle to the cursor
  85.     pCHAR       szNode;     // String which contains the name of the node
  86.     pCFGDesc    CfgDesc;    // Configuration descriptor
  87.     INT16       iLoop;      // Loop variable - used for indenting 
  88.     INT16       BaseSize;   // Length of the current node
  89.  
  90.     // Open the Configuration file - returns the configuration options
  91.     //   for the current level in a schema table referenced by hCur.
  92.     rslt = DbiOpenCfgInfoList(NULL, dbiREADONLY, cfgPersistent, CfgPath,
  93.                               &hCur);
  94.     if (ChkRslt(rslt, "OpenCfgInfoList") == DBIERR_NONE)
  95.     {
  96.         // Allocate resources - make block large enough to contain all
  97.         //   information.
  98.         szNode = (pCHAR)malloc(512 * sizeof(CHAR));
  99.         if (! szNode)
  100.         {
  101.             Screen("    Error - Out of memory");
  102.             return;
  103.         }
  104.  
  105.         CfgDesc = (pCFGDesc)malloc(sizeof(CFGDesc));
  106.         if (! CfgDesc)
  107.         {
  108.             Screen("    Error - Out of memory");
  109.             return;
  110.         }
  111.  
  112.         // Initialize descriptor to 0.
  113.         memset((void *)CfgDesc, 0, sizeof(CFGDesc));
  114.  
  115.         // Process all nodes/paths.
  116.         //   Get the next record in the table - contains the next option
  117.         //   of the given level in the tree.
  118.         while (DbiGetNextRecord(hCur, dbiNOLOCK, (pBYTE) CfgDesc, NULL)
  119.                == DBIERR_NONE)
  120.         {
  121.             // Clear the szNode variable.
  122.             szNode[0] = 0;
  123.  
  124.             // Indent according to the depth of the configuration option.
  125.             for (iLoop = 0; iLoop < Level; iLoop++)
  126.             {
  127.                 strcat(szNode, "    ");
  128.             }
  129.  
  130.             // Output this node, copying the name of the node to the
  131.             //   szNode variable.
  132.             strcat(szNode, CfgDesc->szNodeName);
  133.             Screen(szNode);
  134.  
  135.             // Process this node if it has sub-nodes.
  136.             if (CfgDesc->bHasSubnodes)
  137.             {
  138.                 // Determine if this is a base node.
  139.                 if (Level == 0)
  140.                 {
  141.                     sprintf(szNode, "%s%s", CfgPath, CfgDesc->szNodeName);
  142.                 }
  143.                 else // If not a base node.
  144.                 {
  145.                     sprintf(szNode, "%s\\%s", CfgPath,
  146.                             CfgDesc->szNodeName);
  147.                 }
  148.  
  149.                 // Recursively call this function to get information about
  150.                 //   sub-nodes.
  151.                 DisplayCfgFile(szNode, (Level + 1));
  152.             }
  153.             else    // No sub-nodes...
  154.             {
  155.                 // Clear the szNode variable.
  156.                 szNode[0] = 0;
  157.  
  158.                 // Indent according to the depth of the config option.
  159.                 for (iLoop = 0; iLoop <= Level; iLoop++)
  160.                 {
  161.                     strcat(szNode, "    ");
  162.                 }
  163.  
  164.                 // Determine the length of the string.
  165.                 BaseSize = strlen(szNode);
  166.  
  167.                 // Display the remaining information & description.
  168.                 sprintf(&szNode[BaseSize], "Desc: %s",
  169.                         CfgDesc->szDescription);
  170.                 Screen(szNode);
  171.  
  172.                 // Display the type of the node.
  173.                 sprintf(&szNode[BaseSize], "Type: %d",
  174.                         CfgDesc->iDataType);
  175.                 Screen(szNode);
  176.  
  177.                 // Display the value of the node.
  178.                 sprintf(&szNode[BaseSize], " Val: %s", CfgDesc->szValue);
  179.                 Screen(szNode);
  180.             }
  181.  
  182.             // Initialize descriptor to 0.
  183.             memset((void *) CfgDesc, 0, sizeof(CFGDesc));
  184.         }
  185.  
  186.         // Clean up.
  187.         free(szNode);
  188.         free((pCHAR) CfgDesc);
  189.         if (hCur)
  190.         {
  191.             rslt = DbiCloseCursor(&hCur);
  192.             ChkRslt(rslt, "CloseCursor");
  193.         }
  194.     }
  195. }
  196.