ConfigFile.cpp
上传用户:xqtpzdz
上传日期:2022-05-21
资源大小:1764k
文件大小:10k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. /****************License************************************************
  2.  * Vocalocity OpenVXI
  3.  * Copyright (C) 2004-2005 by Vocalocity, Inc. All Rights Reserved.
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  * Vocalocity, the Vocalocity logo, and VocalOS are trademarks or 
  18.  * registered trademarks of Vocalocity, Inc. 
  19.  * OpenVXI is a trademark of Scansoft, Inc. and used under license 
  20.  * by Vocalocity.
  21.  ***********************************************************************/
  22. // -----1=0-------2=0-------3=0-------4=0-------5=0-------6=0-------7=0-------8
  23. #include <iostream>
  24. using namespace std;
  25. #include <string.h>
  26. #include <math.h>
  27. #include "ConfigFile.h"           // Header for these functions
  28. #include "VXIclientUtils.h"       
  29. #include "VXIclient.h"            // For VXIclientError( )
  30. #define READ_BUFFER_SIZE 2048
  31. #define MODULE_NAME L"VXIClient"
  32. /* Do not use atof( ), is not OS locale safe */
  33. static double StringToFloat(const char *str)
  34. {
  35.    double result = 0.0;
  36.    char *cp = NULL;
  37.    result = strtod(str, &cp);   // integer part
  38.    if ((cp != 0) && (*cp == '.'))  // has fraction?
  39.    {
  40.      cp++;  // skip over radix point
  41.      int decDigits = strspn(cp, "0123456789");
  42.      if (decDigits > 0) /* avoid divide by zero */
  43. #ifdef _solaris_
  44.        result += strtod(cp, &cp) / pow(10.0f, decDigits); // add fraction
  45. #else
  46. #if defined(__GNUC__) && ((__GNUC__ >= 3) && (__GLIBCPP__ >= 20030205))
  47.        result += strtod(cp, &cp) / pow(10.0, double(decDigits)); // add fraction
  48. #else 
  49.        result += strtod(cp, &cp) / pow(10, decDigits); // add fraction
  50. #endif
  51. #endif
  52.    }
  53.    return result;
  54. }
  55. extern "C"
  56. VXIplatformResult ParseConfigLine(char* buffer, VXIMap *configArgs)
  57. {
  58.   char *end;
  59.   const char *configKey, *configType, *configValue;
  60.   /* Skip blank lines or those starting with comments */
  61.   if((buffer[0] == '') || (buffer[0] == CONFIG_FILE_COMMENT_ID) ||
  62.      (strspn(buffer, CONFIG_FILE_SEPARATORS) == strlen(buffer))) {
  63.     return VXIplatform_RESULT_SUCCESS;
  64.   }
  65.   /* Get the configuration key */
  66.   configKey = buffer;
  67.   if(strchr(CONFIG_FILE_SEPARATORS, configKey[0]) != NULL) {
  68.      return VXIplatform_RESULT_FAILURE;
  69.   }
  70.   end = strpbrk(configKey, CONFIG_FILE_SEPARATORS);
  71.   if(end == NULL) {
  72.     return VXIplatform_RESULT_FAILURE;
  73.   }
  74.   *end = ''; /* terminate configKey */
  75.   /* Get the configuration type */
  76.   configType = end + 1;
  77.   configType = &configType[strspn(configType, CONFIG_FILE_SEPARATORS)];
  78.   if(configType[0] == '') {
  79.     return VXIplatform_RESULT_FAILURE;
  80.   }
  81.   end = strpbrk(configType, CONFIG_FILE_SEPARATORS);
  82.   if(end == NULL) {
  83.     return VXIplatform_RESULT_FAILURE;
  84.   }
  85.   *end = ''; /* terminate configType */
  86.   /* Get the configuration value, stripping trailing whitespace */
  87.   configValue = end + 1;
  88.   configValue = &configValue[strspn(configValue, CONFIG_FILE_SEPARATORS)];
  89.   if(configValue[0] == '') {
  90.     return VXIplatform_RESULT_FAILURE;
  91.   }
  92.   end = strchr(configValue, '');
  93.   while((end > configValue) &&
  94.         (strchr(CONFIG_FILE_SEPARATORS, *(end - 1)) != NULL)) end--;
  95.   if(end == configValue) {
  96.     return VXIplatform_RESULT_FAILURE;
  97.   }
  98.   *end = ''; /* terminate configValue */
  99.   string expStr("");
  100.   if(configValue != NULL) {
  101.     // Resolve environment variables, if any
  102.     const char *envVarBgn, *envVarEnd;
  103.     const VXIint lenBgn = strlen(CONFIG_FILE_ENV_VAR_BEGIN_ID);
  104.     const VXIint lenEnd = strlen(CONFIG_FILE_ENV_VAR_END_ID);
  105.     const char *ptrValue = configValue;
  106.     while((envVarBgn = strstr(ptrValue, CONFIG_FILE_ENV_VAR_BEGIN_ID)) != NULL)
  107.     {
  108.       if (envVarBgn != ptrValue)
  109.         expStr.assign (ptrValue, envVarBgn - ptrValue);
  110.       envVarBgn += lenBgn;
  111.       if(envVarBgn[0] == 0)
  112.         return VXIplatform_RESULT_FAILURE;
  113.       envVarEnd = strstr(envVarBgn, CONFIG_FILE_ENV_VAR_END_ID);
  114.       if((envVarEnd == NULL) || (envVarEnd == envVarBgn))
  115.         return VXIplatform_RESULT_FAILURE;
  116.       string envStr(envVarBgn, envVarEnd - envVarBgn);
  117.       const char *envVarValue = getenv(envStr.c_str());
  118.       if((envVarValue == NULL) || (envVarValue[0] == 0)) {
  119.         VXIclientError (NULL, MODULE_NAME, 110,
  120.                         L"Configuration file references an environment "
  121.                         L"variable that is not set",
  122.                         L"%s%S", L"envVar", envStr.c_str( ));
  123.         return VXIplatform_RESULT_FAILURE;
  124.       }
  125.       expStr += envVarValue;
  126.       ptrValue = envVarEnd + lenEnd;
  127.     }
  128.     expStr += ptrValue;
  129.     configValue = expStr.c_str();
  130.   }
  131.   VXIint keyLen = strlen(configKey) + 1;
  132.   VXIchar *wConfigKey = new VXIchar [keyLen];
  133.   if(wConfigKey == NULL) {
  134.     return VXIplatform_RESULT_OUT_OF_MEMORY;
  135.   }
  136.   char2wchar(wConfigKey, configKey, keyLen);
  137.   if(strcmp(configType, "Environment") == 0) { // Set environment variable
  138.     int cfgValLen = configValue ? strlen(configValue) : 0;
  139.     char *envVar = new char [strlen(configKey) + cfgValLen + 2];
  140.     if(envVar == NULL) {
  141.       delete [] wConfigKey;
  142.       return VXIplatform_RESULT_OUT_OF_MEMORY;
  143.     }
  144. #ifdef WIN32
  145.     sprintf(envVar, "%s=%s", configKey, configValue ? configValue : "");
  146.     _putenv(envVar);
  147. #else
  148.     if(configValue)
  149.       sprintf(envVar, "%s=%s", configKey, configValue);
  150.     else
  151.       strcpy(envVar, configKey); // Remove variable from environment
  152.     putenv(envVar);
  153. #endif
  154.     // 'envVar' must NOT be freed !!!
  155.   }
  156.   else if(strcmp(configType, "VXIPtr") == 0) {
  157.     // Only NULL pointers are supported
  158.     VXIMapSetProperty(configArgs, wConfigKey,
  159.                      (VXIValue *)VXIPtrCreate(NULL));
  160.   }
  161.   else {
  162.     if(configValue == NULL) {
  163.       delete [] wConfigKey;
  164.       return VXIplatform_RESULT_FAILURE;
  165.     }
  166.     if(strcmp(configType, "VXIInteger") == 0) {
  167.       VXIint32 intValue = (VXIint32)atoi(configValue);
  168.       if((intValue == 0) && (configValue[0] != '0')) {
  169.         delete [] wConfigKey;
  170.         return VXIplatform_RESULT_FAILURE;
  171.       }
  172.       VXIMapSetProperty(configArgs, wConfigKey, 
  173.                        (VXIValue *)VXIIntegerCreate(intValue));
  174.     }
  175.     else if(strcmp(configType, "VXIFloat") == 0) {
  176.       /* Do not use atof( ), is not OS locale safe */
  177.       VXIflt32 fltValue = (VXIflt32)StringToFloat(configValue);
  178.       if((fltValue == 0.0) && 
  179.         ((configValue[0] != '0') || (configValue[0] != '.'))) {
  180.         delete [] wConfigKey;
  181.         return VXIplatform_RESULT_FAILURE;
  182.       }
  183.       VXIMapSetProperty(configArgs, wConfigKey, 
  184.                        (VXIValue *)VXIFloatCreate(fltValue));
  185.     }
  186.     else if(strcmp(configType, "VXIString") == 0) {
  187.       VXIint valLen = strlen(configValue) + 1;
  188.       VXIchar *wConfigValue = new VXIchar [valLen];
  189.       if(wConfigValue == NULL) {
  190.         delete [] wConfigKey;
  191.         return VXIplatform_RESULT_OUT_OF_MEMORY;
  192.       }
  193.       char2wchar(wConfigValue, configValue, valLen);
  194.       VXIMapSetProperty(configArgs, wConfigKey, 
  195.                        (VXIValue *)VXIStringCreate(wConfigValue));
  196.       delete [] wConfigValue;
  197.     }
  198.     else {
  199.       delete [] wConfigKey;
  200.       return VXIplatform_RESULT_UNSUPPORTED;
  201.     }
  202.   }
  203.   delete [] wConfigKey;
  204.   return VXIplatform_RESULT_SUCCESS;
  205. }
  206. extern "C"
  207. VXIplatformResult ParseConfigFile (VXIMap **configArgs, const char *fileName)
  208. {
  209.   VXIplatformResult platformResult = VXIplatform_RESULT_SUCCESS;
  210.   char buffer[READ_BUFFER_SIZE];
  211.   VXIint lineNum = 0;
  212.   char *readResult;
  213.   if((configArgs == NULL) || (fileName == NULL)) {
  214.     return VXIplatform_RESULT_INVALID_ARGUMENT;
  215.   }
  216.   *configArgs = VXIMapCreate();
  217.   if( !*configArgs ) return VXIplatform_RESULT_OUT_OF_MEMORY;
  218.     
  219.   FILE *configFile = fopen(fileName, "r");
  220.   if(configFile == NULL) {
  221.     VXIclientError(NULL, MODULE_NAME, 108, 
  222.                    L"Cannot open configuration file", L"%s%S",
  223.                    L"file", fileName);
  224.     return VXIplatform_RESULT_SYSTEM_ERROR;
  225.   }
  226.   readResult = fgets(buffer, READ_BUFFER_SIZE, configFile);
  227.   while(readResult != NULL) {
  228.     lineNum++;
  229.     platformResult = ParseConfigLine(buffer, *configArgs);
  230.     if(platformResult == VXIplatform_RESULT_FAILURE) {
  231.       VXIclientError (NULL, MODULE_NAME, 300, 
  232.                       L"Configuration file syntax error", L"%s%S%s%d",
  233.                       L"file", fileName, L"line", lineNum);
  234.       platformResult = VXIplatform_RESULT_SUCCESS;
  235.     }
  236.     else if(platformResult == VXIplatform_RESULT_UNSUPPORTED) {
  237.       VXIclientError (NULL, MODULE_NAME, 301, 
  238.                       L"Unsupported configuration file data", L"%s%S%s%d",
  239.                       L"file", fileName, L"line", lineNum);
  240.       platformResult = VXIplatform_RESULT_SUCCESS;
  241.     }
  242.     else if(platformResult != VXIplatform_RESULT_SUCCESS) {
  243.       break;
  244.     }
  245.     readResult = fgets(buffer, READ_BUFFER_SIZE, configFile);
  246.   }
  247.   if((readResult == NULL) && !feof(configFile)) {
  248.     VXIclientError (NULL, MODULE_NAME, 109, 
  249.                     L"Configuration file read failed", L"%s%S",
  250.                     L"file", fileName);
  251.     platformResult = VXIplatform_RESULT_SYSTEM_ERROR;
  252.   }
  253.   fclose(configFile);
  254.   return platformResult;
  255. }