"Fossies" - the Fresh Open Source Software Archive

Member "tvnserver-2.0.4/config-lib/IniFileSettingsManager.cpp" (3 Aug 2011, 5609 Bytes) of archive /windows/misc/tvnserver-2.0.4-src.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "IniFileSettingsManager.cpp" see the Fossies "Dox" file reference documentation.

    1 // Copyright (C) 2008, 2009, 2010 GlavSoft LLC.
    2 // All rights reserved.
    3 //
    4 //-------------------------------------------------------------------------
    5 // This file is part of the TightVNC software.  Please visit our Web site:
    6 //
    7 //                       http://www.tightvnc.com/
    8 //
    9 // This program is free software; you can redistribute it and/or modify
   10 // it under the terms of the GNU General Public License as published by
   11 // the Free Software Foundation; either version 2 of the License, or
   12 // (at your option) any later version.
   13 //
   14 // This program is distributed in the hope that it will be useful,
   15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17 // GNU General Public License for more details.
   18 //
   19 // You should have received a copy of the GNU General Public License along
   20 // with this program; if not, write to the Free Software Foundation, Inc.,
   21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   22 //-------------------------------------------------------------------------
   23 //
   24 
   25 #include "IniFileSettingsManager.h"
   26 
   27 #include <crtdbg.h>
   28 
   29 IniFileSettingsManager::IniFileSettingsManager(const TCHAR *pathToFile, const TCHAR *appName)
   30 {
   31   m_pathToFile.setString(pathToFile);
   32   m_appName.setString(appName);
   33 }
   34 
   35 IniFileSettingsManager::IniFileSettingsManager(const TCHAR *pathToFile)
   36 {
   37   m_pathToFile.setString(pathToFile);
   38 }
   39 
   40 IniFileSettingsManager::IniFileSettingsManager()
   41 {
   42 }
   43 
   44 IniFileSettingsManager::~IniFileSettingsManager()
   45 {
   46 }
   47 
   48 void IniFileSettingsManager::setApplicationName(const TCHAR *appName)
   49 {
   50   m_appName.setString(appName);
   51 }
   52 
   53 void IniFileSettingsManager::setPathToFile(const TCHAR *pathToFile)
   54 {
   55   m_pathToFile.setString(pathToFile);
   56 }
   57 
   58 bool IniFileSettingsManager::isOk()
   59 {
   60   return !m_pathToFile.isEmpty();
   61 }
   62 
   63 bool IniFileSettingsManager::keyExist(const TCHAR *name)
   64 {
   65 
   66   StringStorage value;
   67 
   68   const TCHAR * defaultValue = _T("TightVNC_Ini_File_Key_Does_Not_Exist_Test");
   69 
   70   getPrivateProfileString(name, &value, defaultValue);
   71 
   72   if (value.isEqualTo(defaultValue)) {
   73     return false;
   74   }
   75   return true;
   76 }
   77 
   78 bool IniFileSettingsManager::deleteKey(const TCHAR *name)
   79 {
   80   return WritePrivateProfileString(m_appName.getString(), name,
   81                                    NULL, m_pathToFile.getString()) == TRUE;
   82 }
   83 
   84 bool IniFileSettingsManager::getString(const TCHAR *name, StringStorage *storage)
   85 {
   86   if (!keyExist(name)) {
   87     return false;
   88   }
   89   getPrivateProfileString(name, storage, NULL);
   90   return true;
   91 }
   92 
   93 bool IniFileSettingsManager::setString(const TCHAR *name, const TCHAR *value)
   94 {
   95   if (value == NULL) {
   96     return false;
   97   }
   98   return WritePrivateProfileString(m_appName.getString(), name,
   99                                    value, m_pathToFile.getString()) == TRUE;
  100 }
  101 
  102 bool IniFileSettingsManager::getLong(const TCHAR *name, long *value)
  103 {
  104   _ASSERT(FALSE);
  105   return false;
  106 }
  107 
  108 bool IniFileSettingsManager::setLong(const TCHAR *name, long value)
  109 {
  110   _ASSERT(FALSE);
  111   return false;
  112 }
  113 
  114 bool IniFileSettingsManager::getBoolean(const TCHAR *name, bool *value)
  115 {
  116   int intVal;
  117   if (!getInt(name, &intVal)) {
  118     return false;
  119   }
  120   *value = intVal == 1;
  121   return true;
  122 }
  123 
  124 bool IniFileSettingsManager::setBoolean(const TCHAR *name, bool value)
  125 {
  126   return setInt(name, value ? 1 : 0);
  127 }
  128 
  129 bool IniFileSettingsManager::getUINT(const TCHAR *name, UINT *value)
  130 {
  131   return getIntAndCastTo<UINT>(name, value);
  132 }
  133 
  134 bool IniFileSettingsManager::setUINT(const TCHAR *name, UINT value)
  135 {
  136   StringStorage stringVal;
  137   stringVal.format(_T("%u"), value);
  138 
  139   return setString(name, stringVal.getString());
  140 }
  141 
  142 bool IniFileSettingsManager::getInt(const TCHAR *name, int *value)
  143 {
  144   UINT defVal = 0xABCDEF;
  145   UINT ret = GetPrivateProfileInt(m_appName.getString(), name, defVal,
  146                                   m_pathToFile.getString());
  147   if (ret == defVal) {
  148     return false;
  149   }
  150 
  151   *value = (int)ret;
  152 
  153   return true;
  154 }
  155 
  156 bool IniFileSettingsManager::setInt(const TCHAR *name, int value)
  157 {
  158   StringStorage stringVal;
  159   stringVal.format(_T("%d"), value);
  160 
  161   return setString(name, stringVal.getString());
  162 }
  163 
  164 bool IniFileSettingsManager::getByte(const TCHAR *name, char *value)
  165 {
  166   return getIntAndCastTo<char>(name, value);
  167 }
  168 
  169 bool IniFileSettingsManager::setByte(const TCHAR *name, char value)
  170 {
  171   return setInt(name, value);
  172 }
  173 
  174 bool IniFileSettingsManager::getBinaryData(const TCHAR *name, void *value, size_t *size)
  175 {
  176   _ASSERT(FALSE);
  177   return false;
  178 }
  179 
  180 bool IniFileSettingsManager::setBinaryData(const TCHAR *name, const void *value, size_t size)
  181 {
  182   _ASSERT(FALSE);
  183   return false;
  184 }
  185 
  186 void
  187 IniFileSettingsManager::getPrivateProfileString(const TCHAR *name,
  188                                                 StringStorage *value,
  189                                                 const TCHAR *defaultValue)
  190 {
  191   TCHAR *buffer = 0;
  192   size_t bufferSize = 0;
  193   size_t increaseStep = 1024;
  194 
  195   bool tooSmall = false;
  196 
  197   do {
  198     bufferSize += increaseStep;
  199     buffer = new TCHAR[bufferSize];
  200 
  201     DWORD ret = GetPrivateProfileString(m_appName.getString(), name,
  202                                         defaultValue, buffer, bufferSize,
  203                                         m_pathToFile.getString());
  204 
  205     tooSmall = (ret == bufferSize - 1);
  206 
  207     if (!tooSmall) {
  208       value->setString(buffer);
  209     }
  210 
  211     delete[] buffer;
  212 
  213     if (!tooSmall) {
  214       break;
  215     }
  216 
  217   } while (tooSmall);
  218 
  219 }