Commit 11e208a7eddbc6b0cbe2b61fc223a69efa832fab
1 parent
f90f4995
no message
Showing
16 changed files
with
15 additions
and
4311 deletions
Show diff stats
anubis_dev/vm/proj/win32/vm.dsp
| ... | ... | @@ -200,11 +200,11 @@ SOURCE=..\..\..\cipher\blowfish.c |
| 200 | 200 | # End Source File |
| 201 | 201 | # Begin Source File |
| 202 | 202 | |
| 203 | -SOURCE=..\..\src\debug_log.cpp | |
| 203 | +SOURCE=..\..\..\share\DebugLog.cpp | |
| 204 | 204 | # End Source File |
| 205 | 205 | # Begin Source File |
| 206 | 206 | |
| 207 | -SOURCE=..\..\src\FileIO.cpp | |
| 207 | +SOURCE=..\..\..\share\FileIO.cpp | |
| 208 | 208 | # End Source File |
| 209 | 209 | # Begin Source File |
| 210 | 210 | |
| ... | ... | @@ -252,7 +252,7 @@ SOURCE=..\..\src\vmtools.cpp |
| 252 | 252 | # End Source File |
| 253 | 253 | # Begin Source File |
| 254 | 254 | |
| 255 | -SOURCE=..\..\src\win32stuff.cpp | |
| 255 | +SOURCE=..\..\..\share\win32stuff.cpp | |
| 256 | 256 | # End Source File |
| 257 | 257 | # End Group |
| 258 | 258 | # Begin Group "Header Files" |
| ... | ... | @@ -292,10 +292,6 @@ SOURCE=..\..\src\dependencies.h |
| 292 | 292 | # End Source File |
| 293 | 293 | # Begin Source File |
| 294 | 294 | |
| 295 | -SOURCE=..\..\src\FileIO.h | |
| 296 | -# End Source File | |
| 297 | -# Begin Source File | |
| 298 | - | |
| 299 | 295 | SOURCE=..\..\src\graphics.h |
| 300 | 296 | # End Source File |
| 301 | 297 | # Begin Source File | ... | ... |
anubis_dev/vm/src/FileIO.cpp deleted
| 1 | - | |
| 2 | -#include "FileIO.h" | |
| 3 | -#include <string.h> | |
| 4 | -#include "debug_log.h" | |
| 5 | -#include "dependencies.h" | |
| 6 | - | |
| 7 | - | |
| 8 | -#if defined(__BEOS__) || defined(_LINUX_) | |
| 9 | -#include <unistd.h> | |
| 10 | -#include <stdlib.h> | |
| 11 | -#endif | |
| 12 | - | |
| 13 | -USING_NAMESPACE(CM); | |
| 14 | - | |
| 15 | -char FileSeparator(void) | |
| 16 | -{ | |
| 17 | -#ifdef WIN32 | |
| 18 | - return '\\'; | |
| 19 | -#elif defined(__BEOS__) || defined (_LINUX_) | |
| 20 | - return '/'; | |
| 21 | -#endif | |
| 22 | - | |
| 23 | -} | |
| 24 | - | |
| 25 | -void NormalizeFileName(char *pathName) | |
| 26 | -{ | |
| 27 | -int size = strlen(pathName); | |
| 28 | - | |
| 29 | - for( int i = 0; i < size; i++) | |
| 30 | - { | |
| 31 | - if(pathName[i] == '\\' || pathName[i] == '/') | |
| 32 | - { | |
| 33 | -#ifdef WIN32 | |
| 34 | - pathName[i] = '\\'; | |
| 35 | -#elif defined(__BEOS__) || defined (_LINUX_) | |
| 36 | - pathName[i] = '/'; | |
| 37 | -#endif | |
| 38 | - } | |
| 39 | - } | |
| 40 | -} | |
| 41 | - | |
| 42 | -String GetVMRootDir(char * argv0) | |
| 43 | -{ | |
| 44 | -#ifdef WIN32 | |
| 45 | - String execDir = argv0; | |
| 46 | - NormalizeFileName((char *)execDir.Cstr()); | |
| 47 | - int lastSlash = execDir.LastIndexOf('\\'); | |
| 48 | - if(lastSlash != -1) | |
| 49 | - execDir = execDir.Substring(0, lastSlash); | |
| 50 | - else if (execDir.LastIndexOf(':') == -1) | |
| 51 | - { | |
| 52 | - execDir = "."; | |
| 53 | - } | |
| 54 | - return execDir; | |
| 55 | -#endif | |
| 56 | -#if defined(__BEOS__) || defined(_LINUX_) | |
| 57 | - String execDir; | |
| 58 | - char *evPATH; | |
| 59 | - char *buf; | |
| 60 | - int i,j,k; | |
| 61 | - | |
| 62 | - if (argv0[0] == '/') | |
| 63 | - { | |
| 64 | - i = 0; | |
| 65 | - while (argv0[i] != 0) i++; | |
| 66 | - while (argv0[i] != '/') i--; | |
| 67 | - argv0[i] = 0; | |
| 68 | - execDir = argv0; | |
| 69 | - return execDir; | |
| 70 | - } | |
| 71 | - else | |
| 72 | - { | |
| 73 | - if ((evPATH = getenv("PATH")) == NULL) | |
| 74 | - { | |
| 75 | - LOGERROR("Your 'PATH' shell variable is not defined.\n"); // will probably never happen | |
| 76 | - my_exit(1); | |
| 77 | - } | |
| 78 | - | |
| 79 | - if ((buf = (char *)malloc(strlen(evPATH)+strlen(argv0)+10)) == NULL) | |
| 80 | - { | |
| 81 | - LOGERROR("Not enough memory.\n"); | |
| 82 | - my_exit(1); | |
| 83 | - } | |
| 84 | - | |
| 85 | - /* evPATH contains: | |
| 86 | - | |
| 87 | - "path_1:path_2:...:path_n" | |
| 88 | - | |
| 89 | - */ | |
| 90 | - | |
| 91 | - i = 0; // marker for beginning of path | |
| 92 | - | |
| 93 | - while (evPATH[i] != 0) // while there is still some path to check | |
| 94 | - { | |
| 95 | - if (evPATH[i] == ':') { i++; } // avoid a possible leading ':' | |
| 96 | - // copy path to buffer | |
| 97 | - for (j = 0; evPATH[i+j] != ':' && evPATH[i+j] != 0; j++) { buf[j] = evPATH[i+j]; } | |
| 98 | - // append "/anbexec" | |
| 99 | - buf[j] = '/'; | |
| 100 | - for (k = 0; argv0[k] != 0; k++) { buf[j+1+k] = argv0[k]; } | |
| 101 | - // put a delimiting zero | |
| 102 | - buf[j+1+k] = 0; | |
| 103 | - // check if the file "path/anbexec" exists | |
| 104 | - if (access(buf,F_OK) == 0) // our file is here | |
| 105 | - { | |
| 106 | - // replace the '/' just before 'anbexec' by 0 (j did not change) | |
| 107 | - buf[j] = 0; | |
| 108 | - printf("Directory of executable is '%s'\n",buf); | |
| 109 | - execDir = buf; | |
| 110 | - free(buf); | |
| 111 | - return execDir; | |
| 112 | - } | |
| 113 | - // advance i | |
| 114 | - i += j; | |
| 115 | - } | |
| 116 | - | |
| 117 | - LOGERROR("Unable to find directory of executable '%s'.\n",argv0); | |
| 118 | - my_exit(1); | |
| 119 | - return (String)NULL; | |
| 120 | - } | |
| 121 | -#endif | |
| 122 | -} | |
| 123 | - | |
| 124 | - |
anubis_dev/vm/src/FileIO.h deleted
anubis_dev/vm/src/IniFile.cpp deleted
| 1 | - /******************************************************************* | |
| 2 | - * Fonctions de gestion de fichiers de parametres (.inf) * | |
| 3 | - * * | |
| 4 | - * Fichier source des fonctions. * | |
| 5 | - * * | |
| 6 | - * (c) 1996 - Philippe Castella * | |
| 7 | - * (c) 2000-2002 - David RENE move to C++ class * | |
| 8 | - * (c) 2003-2004 - Carlipa Systems | |
| 9 | - *******************************************************************/ | |
| 10 | - | |
| 11 | -#include "AnubisSupport.h" | |
| 12 | -#include <stdio.h> | |
| 13 | -#include <stdlib.h> | |
| 14 | -#include <malloc.h> | |
| 15 | -#include <string.h> | |
| 16 | -//#include "root/CSErrors.h" | |
| 17 | -#include "IniFile.h" | |
| 18 | -//#include "root/time/MLDate.h" | |
| 19 | -//#include "root/DebugLog.h" | |
| 20 | -//#include "root/support/PortableMacros.h" | |
| 21 | -//#include "root/base64.h" | |
| 22 | -//#include "root/time/MLDate.h" //contient worldwideAtoF | |
| 23 | -//#include "root/Path.h" | |
| 24 | -//#include "root/FileIO.h" | |
| 25 | - | |
| 26 | -#define TYPE_DEFAULT 0 | |
| 27 | -#define TYPE_BASE64 1 | |
| 28 | - | |
| 29 | -USING_NAMESPACE(CM); | |
| 30 | - | |
| 31 | -/*-----------------------------------------------------------------* | |
| 32 | - * InfCreate : fonction de creation/ouverture d'un fichier .inf * | |
| 33 | - * * | |
| 34 | - * Entree : nom du fichier .inf * | |
| 35 | - * Sortie : un pointeur sur la nouvelle structure creee * | |
| 36 | - * * | |
| 37 | - * Derniere modification : 11 decembre 1996 * | |
| 38 | - *-----------------------------------------------------------------*/ | |
| 39 | -IniFile::IniFile(const String &fileName) | |
| 40 | -{ | |
| 41 | - Init(fileName.Cstr()); | |
| 42 | -} | |
| 43 | - | |
| 44 | -IniFile::IniFile (const char *nomFic) | |
| 45 | -{ | |
| 46 | - Init(nomFic); | |
| 47 | -} | |
| 48 | - | |
| 49 | -void IniFile::Init(const char *nomFic) | |
| 50 | -{ | |
| 51 | - _initCheck = B_ERROR; | |
| 52 | - _lastReadSection = NULL; | |
| 53 | - _lastIsSection = NULL; | |
| 54 | - _mustSave = false; | |
| 55 | - _readCache = NULL; | |
| 56 | - _cacheByteCount = 0; | |
| 57 | - _cacheByteIdx = 0; | |
| 58 | - _currentstrVar = NULL; | |
| 59 | - _readError = B_OK; | |
| 60 | - strIni = NULL; | |
| 61 | - | |
| 62 | - /* Ouverture du fichier */ | |
| 63 | - FILE *fileHandle = fopen (nomFic, "r"); | |
| 64 | - if(fileHandle == NULL /*&& FileExist(nomFic)*/) | |
| 65 | - { | |
| 66 | - _initCheck = B_ERROR; | |
| 67 | - return; | |
| 68 | - } | |
| 69 | - /* Allocation de la memoire necessaire e la structure */ | |
| 70 | - strIni = new inifile_struct; | |
| 71 | - if (strIni) | |
| 72 | - { | |
| 73 | - /* Mise jour de la structure */ | |
| 74 | - strIni->FileName = nomFic; | |
| 75 | - strIni->FirstSection = (InfSection *)0; | |
| 76 | - | |
| 77 | - /* Si fichier existe, on lit son contenu */ | |
| 78 | - if (fileHandle) | |
| 79 | - { /* Lecture ligne a ligne du fichier = remplir la structure */ | |
| 80 | - //bigtime_t beginTime = GetCurrentuS(); | |
| 81 | - ReadFile(fileHandle); | |
| 82 | - //bigtime_t endTime = GetCurrentuS(); | |
| 83 | - | |
| 84 | - //LOGTRACE("ReadFile [%s] %I64d milli ",nomFic, (endTime - beginTime)/1000); | |
| 85 | - } | |
| 86 | - _initCheck = B_OK; | |
| 87 | - } | |
| 88 | - else | |
| 89 | - { | |
| 90 | - strIni = (inifile_struct *)0; | |
| 91 | - _initCheck = B_NO_MEMORY; | |
| 92 | - } | |
| 93 | - /* Fermeture du fichier */ | |
| 94 | - if(fileHandle) | |
| 95 | - fclose (fileHandle); | |
| 96 | -} | |
| 97 | - | |
| 98 | -status_t IniFile::InitCheck() const | |
| 99 | -{ | |
| 100 | - return _initCheck; | |
| 101 | -} | |
| 102 | - | |
| 103 | -/*-----------------------------------------------------------------* | |
| 104 | - * InfWriteBool : Ajout d'une variable de type booleen. * | |
| 105 | - * InfWriteChar : Ajout d'une variable de type char. * | |
| 106 | - * InfWriteInteger : Ajout d'une variable de type entier. * | |
| 107 | - * InfWriteInt64 : Ajout d'une variable de type entier 64. * | |
| 108 | - * InfWriteLong : Ajout d'une variable de type long. * | |
| 109 | - * InfWriteFloat : Ajout d'une variable de type double. * | |
| 110 | - * InfWriteString : Ajout d'une variable de type chaine de car. * | |
| 111 | - * * | |
| 112 | - * Entree : nom de la section * | |
| 113 | - * nom de la variable * | |
| 114 | - * valeur de la variable * | |
| 115 | - * I/O : L'adresse du pointeur sur la structure de donnees. * | |
| 116 | - * * | |
| 117 | - * Derniere modification : 3/6/2002 * | |
| 118 | - *-----------------------------------------------------------------*/ | |
| 119 | -void IniFile::WriteBool (const char *section, const char *var, bool val) | |
| 120 | -{ | |
| 121 | - if (val) | |
| 122 | - AddVar (section, var, "1"); | |
| 123 | - else | |
| 124 | - AddVar (section, var, "0"); | |
| 125 | -} | |
| 126 | - | |
| 127 | -void IniFile::WriteChar (const char *section, const char *var, char val) | |
| 128 | -{ | |
| 129 | - char buf[10]; | |
| 130 | - | |
| 131 | - sprintf(buf, "%d", (int)val); | |
| 132 | -// itoa ((int)val, buf, 10); | |
| 133 | - AddVar (section, var, buf); | |
| 134 | -} | |
| 135 | - | |
| 136 | -void IniFile::WriteInteger (const char *section, const char *var, int val) | |
| 137 | -{ | |
| 138 | - char buf[10]; | |
| 139 | - | |
| 140 | - sprintf(buf, "%d", (int)val); | |
| 141 | - AddVar (section, var, buf); | |
| 142 | -} | |
| 143 | - | |
| 144 | -void IniFile::WriteInt64 (const char *section, const char *var, int64 val) | |
| 145 | -{ | |
| 146 | - char buf[32]; | |
| 147 | -#ifdef WIN32 | |
| 148 | - sprintf(buf, "%I64d", val); | |
| 149 | -#elif __BEOS__ | |
| 150 | - sprintf(buf, "%Ld", val); | |
| 151 | -#endif | |
| 152 | - | |
| 153 | - AddVar (section, var, buf); | |
| 154 | -} | |
| 155 | - | |
| 156 | -void IniFile::WriteInt32 (const char *section, const char *var, int32 val) | |
| 157 | -{ | |
| 158 | - char buf[10]; | |
| 159 | - | |
| 160 | - sprintf(buf, "%ld", (int32)val); | |
| 161 | -// ltoa (val, buf, 10); | |
| 162 | - AddVar (section, var, buf); | |
| 163 | -} | |
| 164 | - | |
| 165 | -void IniFile::WriteInt16 (const char *section, const char *var, int16 val) | |
| 166 | -{ | |
| 167 | - char buf[10]; | |
| 168 | - | |
| 169 | - sprintf(buf, "%d", (int16)val); | |
| 170 | -// ltoa (val, buf, 10); | |
| 171 | - AddVar (section, var, buf); | |
| 172 | -} | |
| 173 | - | |
| 174 | -void IniFile::WriteLong (const char *section, const char *var, long val) | |
| 175 | -{ | |
| 176 | - char buf[10]; | |
| 177 | - | |
| 178 | - sprintf(buf, "%ld", (long)val); | |
| 179 | -// ltoa (val, buf, 10); | |
| 180 | - AddVar (section, var, buf); | |
| 181 | -} | |
| 182 | - | |
| 183 | -void IniFile::WriteFloat (const char *section, const char *var, float val) | |
| 184 | -{ | |
| 185 | - char buf[32]; | |
| 186 | - | |
| 187 | - worldwideFtoA(val, buf, 31, 14, 'e'); | |
| 188 | - AddVar (section, var, buf); | |
| 189 | -} | |
| 190 | - | |
| 191 | -void IniFile::WriteDouble (const char *section, const char *var, double val) | |
| 192 | -{ | |
| 193 | - char buf[32]; | |
| 194 | - | |
| 195 | - worldwideFtoA(val, buf, 31, 14, 'g'); | |
| 196 | - AddVar (section, var, buf); | |
| 197 | -} | |
| 198 | - | |
| 199 | -void IniFile::WriteString (const char *section, const char *var, const char *val) | |
| 200 | -{ | |
| 201 | - AddVar (section, var, val); | |
| 202 | -} | |
| 203 | - | |
| 204 | -void IniFile::WriteString (const char *section, const char *var, const String &val) | |
| 205 | -{ | |
| 206 | - AddVar (section, var, val()); | |
| 207 | -} | |
| 208 | - | |
| 209 | -//void | |
| 210 | -//IniFile::WriteStringList(const char *section, const char *var, char separator, const StringList & strList) | |
| 211 | -//{ | |
| 212 | -// WriteString(section, var, strList.MakeFlattenedString(separator)); | |
| 213 | -//} | |
| 214 | -// | |
| 215 | -//void IniFile::WriteMessage(const char * section, const char *var, const PortableMessage &msg) | |
| 216 | -//{ | |
| 217 | -// struct buffer_st b; | |
| 218 | -// | |
| 219 | -// size_t msgSize = msg.FlattenedSize(); | |
| 220 | -// uint8 * buffer = new uint8[msgSize]; | |
| 221 | -// msg.Flatten(buffer); | |
| 222 | -// | |
| 223 | -// ::Base64Encode(&b, (const char *)buffer, msgSize); | |
| 224 | -// | |
| 225 | -// AddVar(section, var, b.data, TYPE_BASE64); | |
| 226 | -// | |
| 227 | -// SAFE_DELETE_TAB(buffer); | |
| 228 | -// buffer_delete(&b); | |
| 229 | -//} | |
| 230 | -/*-----------------------------------------------------------------* | |
| 231 | - * InfReadBool : Lecture d'une variable de type booleen. * | |
| 232 | - * InfReadChar : Lecture d'une variable de type char. * | |
| 233 | - * InfReadInteger : Lecture d'une variable de type entier. * | |
| 234 | - * InfReadLong : Lecture d'une variable de type long. * | |
| 235 | - * InfReadFloat : Lecture d'une variable de type double. * | |
| 236 | - * InfReadString : Lecture d'une variable de type chaine de car. * | |
| 237 | - * * | |
| 238 | - * Entree : Le pointeur sur la structure de donnees. * | |
| 239 | - * nom de la section * | |
| 240 | - * nom de la variable * | |
| 241 | - * valeur par defaut de la variable si non trouvee * | |
| 242 | - * I/O : valeur de la variable * | |
| 243 | - * * | |
| 244 | - * Derniere modification : 3/6/2002 * | |
| 245 | - * DR * | |
| 246 | - *-----------------------------------------------------------------*/ | |
| 247 | -status_t IniFile::ReadBool (const char *section, const char *var, bool def, bool *val) const | |
| 248 | -{ | |
| 249 | - char buf[129]; | |
| 250 | - | |
| 251 | - /* Recuperation de la valeur de la variable */ | |
| 252 | - status_t err = GetValeur(section, var, buf); | |
| 253 | - | |
| 254 | - /* Si rien trouve -> retourner val par defaut, sinon la valeur */ | |
| 255 | - if (! *buf) | |
| 256 | - *val = def; | |
| 257 | - else | |
| 258 | - *val = (bool)(atoi(buf) ? true : false); | |
| 259 | - return err; | |
| 260 | -} | |
| 261 | - | |
| 262 | -status_t IniFile::ReadChar (const char *section, const char *var, char def, char *val) const | |
| 263 | -{ | |
| 264 | - char buf[129]; | |
| 265 | - | |
| 266 | - /* Recuperation de la valeur de la variable */ | |
| 267 | - status_t err = GetValeur(section, var, buf); | |
| 268 | - | |
| 269 | - /* Si rien trouve -> retourner val par defaut, sinon la valeur */ | |
| 270 | - if (! *buf) | |
| 271 | - *val = def; | |
| 272 | - else | |
| 273 | - *val = (char)(atoi (buf)); | |
| 274 | - return err; | |
| 275 | -} | |
| 276 | - | |
| 277 | -status_t IniFile::ReadInteger (const char *section, const char *var, int def, int *val) const | |
| 278 | -{ | |
| 279 | - char buf[129]; | |
| 280 | - | |
| 281 | - /* Recuperation de la valeur de la variable */ | |
| 282 | - status_t err = GetValeur(section, var, buf); | |
| 283 | - | |
| 284 | - /* Si rien trouve -> retourner val par defaut, sinon la valeur */ | |
| 285 | - if (! *buf) | |
| 286 | - *val = def; | |
| 287 | - else | |
| 288 | - *val = atoi (buf); | |
| 289 | - return err; | |
| 290 | -} | |
| 291 | - | |
| 292 | -status_t IniFile::ReadInt64 (const char *section, const char *var, int64 def, int64 *val) const | |
| 293 | -{ | |
| 294 | - char buf[129]; | |
| 295 | - | |
| 296 | - /* Recuperation de la valeur de la variable */ | |
| 297 | - status_t err = GetValeur(section, var, buf); | |
| 298 | - | |
| 299 | - /* Si rien trouve -> retourner val par defaut, sinon la valeur */ | |
| 300 | - if (! *buf) | |
| 301 | - *val = def; | |
| 302 | - else | |
| 303 | - *val = atoI64(buf); | |
| 304 | - return err; | |
| 305 | -} | |
| 306 | - | |
| 307 | -status_t IniFile::ReadInt32 (const char *section, const char *var, int32 def, int32 *val) const | |
| 308 | -{ | |
| 309 | - char buf[129]; | |
| 310 | - | |
| 311 | - /* Recuperation de la valeur de la variable */ | |
| 312 | - status_t err = GetValeur(section, var, buf); | |
| 313 | - | |
| 314 | - /* Si rien trouve -> retourner val par defaut, sinon la valeur */ | |
| 315 | - if (! *buf) | |
| 316 | - *val = def; | |
| 317 | - else | |
| 318 | - *val = atol (buf); | |
| 319 | - return err; | |
| 320 | -} | |
| 321 | - | |
| 322 | -status_t IniFile::ReadInt16 (const char *section, const char *var, int16 def, int16 *val) const | |
| 323 | -{ | |
| 324 | - char buf[129]; | |
| 325 | - | |
| 326 | - /* Recuperation de la valeur de la variable */ | |
| 327 | - status_t err = GetValeur(section, var, buf); | |
| 328 | - | |
| 329 | - /* Si rien trouve -> retourner val par defaut, sinon la valeur */ | |
| 330 | - if (! *buf) | |
| 331 | - *val = def; | |
| 332 | - else | |
| 333 | - *val = atoi (buf); | |
| 334 | - return err; | |
| 335 | -} | |
| 336 | - | |
| 337 | - | |
| 338 | -status_t IniFile::ReadLong (const char *section, const char *var, long def, long *val) const | |
| 339 | -{ | |
| 340 | - char buf[129]; | |
| 341 | - | |
| 342 | - /* Recuperation de la valeur de la variable */ | |
| 343 | - status_t err = GetValeur(section, var, buf); | |
| 344 | - | |
| 345 | - /* Si rien trouve -> retourner val par defaut, sinon la valeur */ | |
| 346 | - if (! *buf) | |
| 347 | - *val = def; | |
| 348 | - else | |
| 349 | - *val = atol (buf); | |
| 350 | - return err; | |
| 351 | -} | |
| 352 | - | |
| 353 | -status_t IniFile::ReadDouble (const char *section, const char *var, double def, double *val) const | |
| 354 | -{ | |
| 355 | - char buf[129]; | |
| 356 | - | |
| 357 | - /* Recuperation de la valeur de la variable */ | |
| 358 | - status_t err = GetValeur(section, var, buf); | |
| 359 | - | |
| 360 | - /* Si rien trouve -> retourner val par defaut, sinon la valeur */ | |
| 361 | - if (! *buf) | |
| 362 | - *val = def; | |
| 363 | - else | |
| 364 | - *val = worldwideAtoF(buf); | |
| 365 | - return err; | |
| 366 | -} | |
| 367 | - | |
| 368 | -status_t IniFile::ReadFloat (const char *section, const char *var, float def, float *val) const | |
| 369 | -{ | |
| 370 | - char buf[129]; | |
| 371 | - | |
| 372 | - /* Recuperation de la valeur de la variable */ | |
| 373 | - status_t err = GetValeur(section, var, buf); | |
| 374 | - | |
| 375 | - /* Si rien trouve -> retourner val par defaut, sinon la valeur */ | |
| 376 | - if (! *buf) | |
| 377 | - *val = def; | |
| 378 | - else | |
| 379 | - { | |
| 380 | - *val = (float)worldwideAtoF(buf); | |
| 381 | - } | |
| 382 | - return err; | |
| 383 | -} | |
| 384 | - | |
| 385 | -status_t IniFile::ReadString (const char *section, const char *var, const char *def, char *val) const | |
| 386 | -{ | |
| 387 | - int len = 129; | |
| 388 | - char * buf = new char[129]; | |
| 389 | - | |
| 390 | - /* Recuperation de la valeur de la variable */ | |
| 391 | - status_t err = GetValeur(section, var, buf, &len); | |
| 392 | - if(err == B_NO_MEMORY) | |
| 393 | - { | |
| 394 | - SAFE_DELETE_TAB(buf); | |
| 395 | - buf = new char[len]; | |
| 396 | - err = GetValeur(section, var, buf, &len); | |
| 397 | - } | |
| 398 | - | |
| 399 | - /* Si rien trouve -> retourner val par defaut, sinon la valeur */ | |
| 400 | - if (! *buf) | |
| 401 | - strcpy (val, def); | |
| 402 | - else | |
| 403 | - strcpy (val, buf); | |
| 404 | - SAFE_DELETE_TAB(buf); | |
| 405 | - return err; | |
| 406 | -} | |
| 407 | - | |
| 408 | -status_t IniFile::ReadString (const char *section, const char *var, const char *def, String &val) const | |
| 409 | -{ | |
| 410 | - int len = 129; | |
| 411 | - char * buf = new char[129]; | |
| 412 | - | |
| 413 | - /* Recuperation de la valeur de la variable */ | |
| 414 | - status_t err = GetValeur(section, var, buf, &len); | |
| 415 | - if(err == B_NO_MEMORY) | |
| 416 | - { | |
| 417 | - SAFE_DELETE_TAB(buf); | |
| 418 | - buf = new char[len]; | |
| 419 | - err = GetValeur(section, var, buf, &len); | |
| 420 | - } | |
| 421 | - | |
| 422 | - /* Si rien trouve -> retourner val par defaut, sinon la valeur */ | |
| 423 | - if (! *buf) | |
| 424 | - val = def; | |
| 425 | - else | |
| 426 | - val = buf; | |
| 427 | - SAFE_DELETE_TAB(buf); | |
| 428 | - return err; | |
| 429 | -} | |
| 430 | - | |
| 431 | -//status_t IniFile::ReadStringList (const char *section, const char *var, char* pattern, StringList &val) const | |
| 432 | -//{ | |
| 433 | -// PortableString chaine; | |
| 434 | -// | |
| 435 | -// ReadString(section, var,"",chaine); | |
| 436 | -// val.ParseString(chaine, pattern); | |
| 437 | -// | |
| 438 | -// return CS_OK; | |
| 439 | -//} | |
| 440 | -// | |
| 441 | -//status_t IniFile::ReadMessage (const char *section, const char *var, PortableMessage &msg) const | |
| 442 | -//{ | |
| 443 | -// int llen = 2048; | |
| 444 | -// char * buf = new char[llen]; | |
| 445 | -// memset(buf, 0, llen); | |
| 446 | -// buffer_st result; | |
| 447 | -// | |
| 448 | -// /* Recuperation de la valeur de la variable */ | |
| 449 | -// status_t err = GetValeur(section, var, buf, &llen); | |
| 450 | -// if(err == CS_NO_MEMORY) | |
| 451 | -// { | |
| 452 | -// SAFE_DELETE_TAB(buf); | |
| 453 | -// buf = new char[llen]; | |
| 454 | -// err = GetValeur(section, var, buf, &llen); | |
| 455 | -// } | |
| 456 | -// if (err == CS_OK && *buf) | |
| 457 | -// { | |
| 458 | -// // buffer_new(&result); | |
| 459 | -// Base64Decode(&result, buf, strlen(buf)); | |
| 460 | -// msg.Unflatten((unsigned char *)result.data, result.offset); | |
| 461 | -// buffer_delete(&result); | |
| 462 | -// } | |
| 463 | -// SAFE_DELETE_TAB(buf); | |
| 464 | -// return err; | |
| 465 | -//} | |
| 466 | - | |
| 467 | -/*-----------------------------------------------------------------* | |
| 468 | - * InfFree : Fonction de restitution de la memoire allouee * | |
| 469 | - * dynamiquement. * | |
| 470 | - * * | |
| 471 | - * Derniere modification : 4 aout 2003 * | |
| 472 | - *-----------------------------------------------------------------*/ | |
| 473 | -void IniFile::InfFree () | |
| 474 | -{ | |
| 475 | - /* Si on ne pointe pas sur du vide */ | |
| 476 | - if (strIni != NULL) | |
| 477 | - { | |
| 478 | - /* Effacement de la structure */ | |
| 479 | - SAFE_DELETE(strIni); | |
| 480 | - } | |
| 481 | -} | |
| 482 | - | |
| 483 | -/*-----------------------------------------------------------------* | |
| 484 | - * InfDeleteVar : Fonction de suppression d'une variable. * | |
| 485 | - * * | |
| 486 | - * Entree : Le pointeur sur la structure de donnes. * | |
| 487 | - * Le nom de la section * | |
| 488 | - * Le nom de la variable * | |
| 489 | - * * | |
| 490 | - * Derniere modification : 25 decembre 1996 * | |
| 491 | - *-----------------------------------------------------------------*/ | |
| 492 | -void IniFile::DeleteVar (const char *section, const char *var) | |
| 493 | -{ | |
| 494 | - InfSection *strSection; | |
| 495 | - InfVar *strVar, *oldVar; | |
| 496 | - | |
| 497 | - if (strIni) | |
| 498 | - { | |
| 499 | - /* On recherche si la section existe bien */ | |
| 500 | - strSection = strIni->FirstSection; | |
| 501 | - while (strSection != (InfSection *)0) | |
| 502 | - { | |
| 503 | - if (strcmp(strSection->SectionName, section) == 0) | |
| 504 | - { | |
| 505 | - strVar = oldVar = strSection->FirstVar; | |
| 506 | - while (strVar != (InfVar *)0) | |
| 507 | - { | |
| 508 | - if (strcmp (strVar->VarName, var) == 0) | |
| 509 | - { | |
| 510 | - oldVar->NextVar = strVar->NextVar; | |
| 511 | - if (strSection->FirstVar == strVar) | |
| 512 | - strSection->FirstVar = strVar->NextVar; | |
| 513 | - free (strVar->VarName); | |
| 514 | - free (strVar->VarVal); | |
| 515 | - free (strVar); | |
| 516 | - return; | |
| 517 | - } | |
| 518 | - oldVar = strVar; | |
| 519 | - strVar = strVar->NextVar; | |
| 520 | - } | |
| 521 | - } | |
| 522 | - strSection = strSection->NextSection; | |
| 523 | - } | |
| 524 | - } | |
| 525 | -} | |
| 526 | - | |
| 527 | -/*-----------------------------------------------------------------* | |
| 528 | - * InfDeleteSection : Fonction de suppression d'une section. * | |
| 529 | - * * | |
| 530 | - * Entree : Le nom de la section * | |
| 531 | - * Le nom de la variable * | |
| 532 | - * I/O : L'adresse du pointeur sur la structure de donnees. * | |
| 533 | - * * | |
| 534 | - * Derniere modification : 25 decembre 1996 * | |
| 535 | - *-----------------------------------------------------------------*/ | |
| 536 | -void IniFile::DeleteSection (const char *section) | |
| 537 | -{ | |
| 538 | - InfSection *strSection, *oldSection; | |
| 539 | - | |
| 540 | - if (strIni) | |
| 541 | - { | |
| 542 | - /* On recherche si la section existe bien */ | |
| 543 | - strSection = strIni->FirstSection; | |
| 544 | - oldSection = strSection; | |
| 545 | - while (strSection != (InfSection *)0) | |
| 546 | - { | |
| 547 | - if (strcmp(strSection->SectionName, section) == 0) | |
| 548 | - { | |
| 549 | - if (oldSection == strSection) | |
| 550 | - strIni->FirstSection = strSection->NextSection; | |
| 551 | - else | |
| 552 | - oldSection->NextSection = strSection->NextSection; | |
| 553 | - free (strSection->SectionName); | |
| 554 | - if (strSection == _lastReadSection) // VB 20/09/04 lantage DeleteSection() suivi de AddVar() -> GetSection(); | |
| 555 | - _lastReadSection = NULL; | |
| 556 | - free (strSection); | |
| 557 | - return; | |
| 558 | - } | |
| 559 | - oldSection = strSection; | |
| 560 | - strSection = strSection->NextSection; | |
| 561 | - } | |
| 562 | - } | |
| 563 | -} | |
| 564 | - | |
| 565 | -int IniFile::ReadLine (FILE *ficIni, char *ligne, size_t lineSize) | |
| 566 | -{ | |
| 567 | -/*-----------------------------------------------------------------* | |
| 568 | - * ReadLine : Fonction de lecture d'une ligne dans un fichier ASCII* | |
| 569 | - * * | |
| 570 | - * Entree : Le descripteur du fichier ouvert avec fopen * | |
| 571 | - * I/O : un buffer pour stocker les donnees lues * | |
| 572 | - * Sortie : Le nombre de caracteres composant la ligne, -1 si EOF * | |
| 573 | - * * | |
| 574 | - * Derniere modification : 11 decembre 1996 * | |
| 575 | - *-----------------------------------------------------------------*/ | |
| 576 | - register char car; | |
| 577 | - register int cpt = 0; | |
| 578 | - | |
| 579 | - if(!_readCache || _cacheByteIdx == _cacheByteCount) | |
| 580 | - ReadCache(ficIni); | |
| 581 | - | |
| 582 | - if(!_cacheByteCount) | |
| 583 | - { | |
| 584 | - SAFE_DELETE(_readCache); | |
| 585 | - return -1; | |
| 586 | - } | |
| 587 | - /* Lire car. par car. jusqu'a la fin de ligne ou fin de fichier */ | |
| 588 | - | |
| 589 | - while ((car = _readCache[_cacheByteIdx++]) != '\n') | |
| 590 | - { | |
| 591 | - // If the line is too big, this is probably an error in the file. | |
| 592 | - // So to skip this line, we continue to read without saving the end | |
| 593 | - if(cpt < (int)lineSize - 1) | |
| 594 | - ligne[cpt++] = car; | |
| 595 | - else | |
| 596 | - _readError = B_LINE_TOO_LONG; | |
| 597 | - | |
| 598 | - if(_cacheByteIdx == _cacheByteCount) | |
| 599 | - { | |
| 600 | - ReadCache(ficIni); | |
| 601 | - if(!_cacheByteCount) | |
| 602 | - { | |
| 603 | - break; | |
| 604 | - } | |
| 605 | - } | |
| 606 | - } | |
| 607 | - ligne[cpt] = '\0'; | |
| 608 | - | |
| 609 | - /* Renvoie le nombre de caracteres lus */ | |
| 610 | - return cpt; | |
| 611 | -} | |
| 612 | - | |
| 613 | - | |
| 614 | -void IniFile::ReadCache(FILE* fileHandle) | |
| 615 | -{ | |
| 616 | - if(!_readCache) | |
| 617 | - { | |
| 618 | - _readCache = new char[65536*8]; | |
| 619 | - } | |
| 620 | - | |
| 621 | - _cacheByteCount = fread(_readCache,1, 65536*8, fileHandle); | |
| 622 | - _cacheByteIdx = 0; | |
| 623 | - | |
| 624 | -} | |
| 625 | - | |
| 626 | -#ifdef o | |
| 627 | -//int IniFile::ReadLine (FILE *ficIni, char *ligne) | |
| 628 | -//{ | |
| 629 | -///*-----------------------------------------------------------------* | |
| 630 | -// * ReadLine : Fonction de lecture d'une ligne dans un fichier ASCII* | |
| 631 | -// * * | |
| 632 | -// * Entree : Le descripteur du fichier ouvert avec fopen * | |
| 633 | -// * I/O : un buffer pour stocker les donnees lues * | |
| 634 | -// * Sortie : Le nombre de caracteres composant la ligne, -1 si EOF * | |
| 635 | -// * * | |
| 636 | -// * Derniere modification : 11 decembre 1996 * | |
| 637 | -// *-----------------------------------------------------------------*/ | |
| 638 | -// register char car; | |
| 639 | -// register int cpt = 0; | |
| 640 | -// | |
| 641 | -// | |
| 642 | -// | |
| 643 | -// /* Lire car. par car. jusqu'a la fin de ligne ou fin de fichier */ | |
| 644 | -// if(fgets(ligne, 1154, ficIni)) | |
| 645 | -// { | |
| 646 | -// cpt = strlen(ligne)-1; | |
| 647 | -// ligne[cpt] = '\0'; | |
| 648 | -// return cpt; | |
| 649 | -// } | |
| 650 | -// return -1; | |
| 651 | -//} | |
| 652 | -// | |
| 653 | -//int IniFile::ReadLine (FILE *ficIni, char *ligne) | |
| 654 | -//{ | |
| 655 | -///*-----------------------------------------------------------------* | |
| 656 | -// * ReadLine : Fonction de lecture d'une ligne dans un fichier ASCII* | |
| 657 | -// * * | |
| 658 | -// * Entree : Le descripteur du fichier ouvert avec fopen * | |
| 659 | -// * I/O : un buffer pour stocker les donnees lues * | |
| 660 | -// * Sortie : Le nombre de caracteres composant la ligne, -1 si EOF * | |
| 661 | -// * * | |
| 662 | -// * Derniere modification : 11 decembre 1996 * | |
| 663 | -// *-----------------------------------------------------------------*/ | |
| 664 | -// register char car; | |
| 665 | -// register int cpt = 0; | |
| 666 | -// | |
| 667 | -// | |
| 668 | -// /* Lire car. par car. jusqu'a la fin de ligne ou fin de fichier */ | |
| 669 | -// while (! feof(ficIni) && ((car = fgetc(ficIni)) != '\n')) | |
| 670 | -// ligne[cpt++] = car; | |
| 671 | -// ligne[cpt] = '\0'; | |
| 672 | -// | |
| 673 | -// /* Si fin de fichier, on le signale */ | |
| 674 | -// if (feof(ficIni)) | |
| 675 | -// cpt = -1; | |
| 676 | -// | |
| 677 | -// /* Renvoie le nombre de caracteres lus */ | |
| 678 | -// return cpt; | |
| 679 | -//} | |
| 680 | -#endif | |
| 681 | -/*-----------------------------------------------------------------* | |
| 682 | - * ReadFile : Fonction de lecture des donnees contenue dans le * | |
| 683 | - * fichier .inf * | |
| 684 | - * * | |
| 685 | - * I/O : L'adresse du pointeur sur la structure de donnees. * | |
| 686 | - * * | |
| 687 | - * Derniere modification : 22/09/2002 * | |
| 688 | - * * | |
| 689 | - * Remarque : une ligne ne doit pas depasser 1154 caracteres c-a-d * | |
| 690 | - * 128 car. pour la variable et 1024 pour la valeur. * | |
| 691 | - *-----------------------------------------------------------------*/ | |
| 692 | -void IniFile::ReadFile (FILE *fileHandle) | |
| 693 | -{ | |
| 694 | - register int nbCar, i, j; | |
| 695 | - char section[65]; | |
| 696 | - const size_t lineSize = 1154; | |
| 697 | - char read_buf[lineSize]; | |
| 698 | - char var[129]; | |
| 699 | - char val[1025]; | |
| 700 | - String myBuff; | |
| 701 | - _readError = B_OK; | |
| 702 | - | |
| 703 | - /* Lecture du fichier ligne par ligne */ | |
| 704 | - while ((nbCar = ReadLine (fileHandle, read_buf, lineSize)) > -1) | |
| 705 | - { | |
| 706 | - if (nbCar > 0) | |
| 707 | - { /* Pas une ligne vide */ | |
| 708 | - if (read_buf[0] == '[') // Si debut de Section | |
| 709 | - { // [nom de la section] | |
| 710 | - i = 1; | |
| 711 | - j = 0; | |
| 712 | - | |
| 713 | - // Lire titre de la section | |
| 714 | - while (read_buf[i] != ']' && read_buf[i] != '\0') | |
| 715 | - section[j++] = read_buf[i++]; | |
| 716 | - section[j] = '\0'; | |
| 717 | - | |
| 718 | - /* Creation de la section dans la structure de donnees */ | |
| 719 | - AddSectionInCache(section); | |
| 720 | -// AddSection(section); | |
| 721 | - } | |
| 722 | - else if(read_buf[0] == '#') //C'est un commentaire | |
| 723 | - { | |
| 724 | - continue; //donc on passe a la ligne suivante | |
| 725 | - } | |
| 726 | - else | |
| 727 | - { /* var=val */ | |
| 728 | - i = 0; | |
| 729 | - j = 0; | |
| 730 | - | |
| 731 | - /* Lire nom de la variable */ | |
| 732 | - while (read_buf[i] != '=' && read_buf[i] != '\0') | |
| 733 | - { | |
| 734 | - var[j++] = read_buf[i++]; | |
| 735 | - } | |
| 736 | - | |
| 737 | - //Il n'y a pas de nom de variable, on passe a la ligne suivante | |
| 738 | - if(read_buf[i] == '\0') | |
| 739 | - continue; | |
| 740 | - | |
| 741 | - var[j] = '\0'; | |
| 742 | - i++; | |
| 743 | - | |
| 744 | - //Si apres le = il y a @\n c'est que c'est du base 64 | |
| 745 | - if(strcmp(read_buf+i,"@BEGIN base64 - Don't Edit@") == 0) | |
| 746 | - { | |
| 747 | - myBuff =""; | |
| 748 | - | |
| 749 | - while ((nbCar = ReadLine (fileHandle, read_buf, lineSize)) > -1) | |
| 750 | - { | |
| 751 | - if(strcmp(read_buf,"@END base64@") == 0) | |
| 752 | - { | |
| 753 | - AddVar(section, var, myBuff(), TYPE_BASE64); | |
| 754 | - break; | |
| 755 | - } | |
| 756 | - else | |
| 757 | - myBuff << read_buf; | |
| 758 | - } | |
| 759 | - } | |
| 760 | - else /* Lire valeur de la variable */ | |
| 761 | - { | |
| 762 | - j = 0; | |
| 763 | - while (read_buf[i] != '\0') | |
| 764 | - val[j++] = read_buf[i++]; | |
| 765 | - val[j] = '\0'; | |
| 766 | - /* Creation de la variable dans la structure de donnees*/ | |
| 767 | - AddVar(section, var, val); | |
| 768 | - } | |
| 769 | - | |
| 770 | - } | |
| 771 | - } | |
| 772 | - } | |
| 773 | -} | |
| 774 | - | |
| 775 | -InfSection *IniFile::AddSectionInCache (const char *section) | |
| 776 | -{ | |
| 777 | -InfSection *NewSection; | |
| 778 | - | |
| 779 | - if(_lastReadSection) | |
| 780 | - { | |
| 781 | - NewSection = (InfSection *)malloc (sizeof(InfSection)); | |
| 782 | - if (NewSection) | |
| 783 | - { | |
| 784 | - /* Allocation memoire pour le nom de la section */ | |
| 785 | - NewSection->SectionName = (char *)malloc (strlen(section) + 1); | |
| 786 | - if (! NewSection->SectionName) | |
| 787 | - { | |
| 788 | - free (NewSection); | |
| 789 | - return (InfSection *)0; | |
| 790 | - } | |
| 791 | - | |
| 792 | - /* Mettre a jour le contenu de la structure */ | |
| 793 | - strcpy (NewSection->SectionName, section); | |
| 794 | - NewSection->NextSection = (InfSection *)0; | |
| 795 | - NewSection->FirstVar = (InfVar *)0; | |
| 796 | - | |
| 797 | - _lastReadSection->NextSection = NewSection; | |
| 798 | - | |
| 799 | - } | |
| 800 | - } | |
| 801 | - | |
| 802 | - return (InfSection *)0; | |
| 803 | -} | |
| 804 | - | |
| 805 | -/*-----------------------------------------------------------------* | |
| 806 | - * AddSection : Fonction permettant de creer une nouvelle section * | |
| 807 | - * si celle-ci n'existe pas deja. * | |
| 808 | - * * | |
| 809 | - * Entree : le nom de la section * | |
| 810 | - * I/O : L'adresse du pointeur sur la structure de donnees. * | |
| 811 | - * * | |
| 812 | - * Derniere modification : 11 decembre 1996 * | |
| 813 | - *-----------------------------------------------------------------*/ | |
| 814 | -InfSection *IniFile::AddSection (const char *section) | |
| 815 | -{ | |
| 816 | - InfSection *strSection = strIni->FirstSection; | |
| 817 | - InfSection *NewSection; | |
| 818 | - | |
| 819 | - /* On recherche si la section existe deja */ | |
| 820 | - while (strSection != (InfSection *)0) | |
| 821 | - { | |
| 822 | - if (strcmp(strSection->SectionName, section) == 0) | |
| 823 | - return strSection; | |
| 824 | - strSection = strSection->NextSection; | |
| 825 | - } | |
| 826 | - | |
| 827 | - /* Si elle n'existe pas, on la rajoute */ | |
| 828 | - NewSection = (InfSection *)malloc (sizeof(InfSection)); | |
| 829 | - if (NewSection) | |
| 830 | - { | |
| 831 | - /* Allocation memoire pour le nom de la section */ | |
| 832 | - NewSection->SectionName = (char *)malloc (strlen(section) + 1); | |
| 833 | - if (! NewSection->SectionName) | |
| 834 | - { | |
| 835 | - free (NewSection); | |
| 836 | - return (InfSection *)0; | |
| 837 | - } | |
| 838 | - | |
| 839 | - /* Mettre a jour le contenu de la structure */ | |
| 840 | - strcpy (NewSection->SectionName, section); | |
| 841 | - NewSection->NextSection = (InfSection *)0; | |
| 842 | - NewSection->FirstVar = (InfVar *)0; | |
| 843 | - | |
| 844 | - strSection = strIni->FirstSection; | |
| 845 | - /* Si premiere section ...*/ | |
| 846 | - if (strSection == (InfSection *)0) | |
| 847 | - { | |
| 848 | - strIni->FirstSection = NewSection; | |
| 849 | - return strIni->FirstSection; | |
| 850 | - } | |
| 851 | - else /* ...sinon */ | |
| 852 | - { | |
| 853 | - while (strSection->NextSection != (InfSection *)NULL) | |
| 854 | - strSection = strSection->NextSection; | |
| 855 | - | |
| 856 | - strSection->NextSection = NewSection; | |
| 857 | - return strSection->NextSection; | |
| 858 | - } | |
| 859 | - } | |
| 860 | - return (InfSection *)0; | |
| 861 | -} | |
| 862 | - | |
| 863 | -/*-----------------------------------------------------------------* | |
| 864 | - * AddVar : Fonction permettant de creer une nouvelle variable dans* | |
| 865 | - * une section donnees si elle n'existe pas deja. * | |
| 866 | - * * | |
| 867 | - * Entree : le nom de la section * | |
| 868 | - * le nom de la variable * | |
| 869 | - * la valeur de la variable * | |
| 870 | - * I/O : L'adresse du pointeur sur la structure de donnees. * | |
| 871 | - * * | |
| 872 | - * Derniere modification : 11 decembre 1996 * | |
| 873 | - *-----------------------------------------------------------------*/ | |
| 874 | -status_t IniFile::AddVar (const char *section, const char *var, const char *val, uint8 type) | |
| 875 | -{ | |
| 876 | - InfSection *strSection = NULL; | |
| 877 | - InfVar *strVar = NULL, *NewVar = NULL; | |
| 878 | - bool ok = false; | |
| 879 | - | |
| 880 | - if (strIni) | |
| 881 | - { | |
| 882 | - strSection = GetSection(section); | |
| 883 | - if(strSection == NULL) | |
| 884 | - strSection = AddSection (section); | |
| 885 | - if (strSection == (InfSection *)NULL) | |
| 886 | - return B_NO_MEMORY; | |
| 887 | - | |
| 888 | - /* On recherche si la variable existe deja */ | |
| 889 | - strVar = strSection->FirstVar; | |
| 890 | - while (strVar != (InfVar *)0) | |
| 891 | - { | |
| 892 | - /* Si on trouve la variable, on sort de la boucle */ | |
| 893 | - if (strcmp(strVar->VarName,var) == 0) | |
| 894 | - { | |
| 895 | - ok = true; | |
| 896 | - break; | |
| 897 | - } | |
| 898 | - strVar = strVar->NextVar; | |
| 899 | - } | |
| 900 | - | |
| 901 | - /* Si variable non trouvee, on la cree */ | |
| 902 | - if (! ok) | |
| 903 | - { | |
| 904 | - NewVar = (InfVar *)malloc (sizeof(InfVar)); | |
| 905 | - memset(NewVar, 0, sizeof(InfVar)); | |
| 906 | - if (NewVar) | |
| 907 | - { | |
| 908 | - NewVar->type = type; //Type de variable | |
| 909 | - NewVar->VarName = (char *)malloc(strlen(var) + 1); | |
| 910 | - if (! NewVar->VarName) | |
| 911 | - { | |
| 912 | - free (NewVar); | |
| 913 | - return B_NO_MEMORY; | |
| 914 | - } | |
| 915 | - NewVar->VarVal = NULL; | |
| 916 | - strcpy (NewVar->VarName, var); | |
| 917 | - NewVar->NextVar = (InfVar *)0; | |
| 918 | - | |
| 919 | - strVar = strSection->FirstVar; | |
| 920 | - if (strVar == (InfVar *)0) | |
| 921 | - { | |
| 922 | - strSection->FirstVar = NewVar; | |
| 923 | - strVar = strSection->FirstVar; | |
| 924 | - } | |
| 925 | - else | |
| 926 | - { | |
| 927 | - while (strVar->NextVar != (InfVar *)NULL) | |
| 928 | - strVar = strVar->NextVar; | |
| 929 | - | |
| 930 | - strVar->NextVar = NewVar; | |
| 931 | - strVar = strVar->NextVar; | |
| 932 | - } | |
| 933 | - } | |
| 934 | - } | |
| 935 | - | |
| 936 | - /* Mise a jour de la valeur de la variable */ | |
| 937 | - if(strVar->VarVal) | |
| 938 | - strVar->VarVal = (char *)realloc (strVar->VarVal, strlen(val) + 1); | |
| 939 | - else | |
| 940 | - strVar->VarVal = (char *)malloc (strlen(val) + 1); | |
| 941 | - if (strVar->VarVal) | |
| 942 | - strcpy (strVar->VarVal, val); | |
| 943 | - else | |
| 944 | - return B_NO_MEMORY; | |
| 945 | - } | |
| 946 | - return B_OK; | |
| 947 | -} | |
| 948 | - | |
| 949 | -status_t IniFile::Update(void) | |
| 950 | -{ | |
| 951 | - return SaveFileOnly(); | |
| 952 | -} | |
| 953 | - | |
| 954 | -IniFile::~IniFile(void) | |
| 955 | -{ | |
| 956 | - if(_mustSave) | |
| 957 | - SaveFileAndDelete(); | |
| 958 | - else | |
| 959 | - FreeTree(); | |
| 960 | - | |
| 961 | - InfFree (); | |
| 962 | -} | |
| 963 | - | |
| 964 | -/*------------------------------------------------------------------------* | |
| 965 | - * FreeTree : Fonction permettant d'effacer completement la structure. * | |
| 966 | - *------------------------------------------------------------------------*/ | |
| 967 | -void IniFile::FreeTree (void) | |
| 968 | -{ | |
| 969 | -InfSection *strSection = NULL; | |
| 970 | -InfSection *oldSection = NULL; | |
| 971 | -InfVar *strVar = NULL; | |
| 972 | -InfVar *oldVar = NULL; | |
| 973 | -//int index = 0; | |
| 974 | - | |
| 975 | - | |
| 976 | - if(strIni) | |
| 977 | - { | |
| 978 | - strSection = strIni->FirstSection; | |
| 979 | - while (strSection != (InfSection *)0) | |
| 980 | - { | |
| 981 | - /* Premiere variable */ | |
| 982 | - strVar = strSection->FirstVar; | |
| 983 | - while (strVar != (InfVar *)0) | |
| 984 | - { | |
| 985 | - /* Variable suivante */ | |
| 986 | - oldVar = strVar; | |
| 987 | - strVar = strVar->NextVar; | |
| 988 | - | |
| 989 | - /* Supprimer variable, valeur et structure */ | |
| 990 | - free (oldVar->VarName); | |
| 991 | - free (oldVar->VarVal); | |
| 992 | - free (oldVar); | |
| 993 | - } | |
| 994 | - | |
| 995 | - /* Section suivante */ | |
| 996 | - oldSection = strSection; | |
| 997 | - strSection = strSection->NextSection; | |
| 998 | - | |
| 999 | - /* Supprimer nom et structure */ | |
| 1000 | - free (oldSection->SectionName); | |
| 1001 | - free (oldSection); | |
| 1002 | - } | |
| 1003 | - strIni->FirstSection = NULL; | |
| 1004 | - } | |
| 1005 | - _lastReadSection = NULL; | |
| 1006 | -} | |
| 1007 | - | |
| 1008 | -FILE *IniFile::SafeCreate() | |
| 1009 | -{ | |
| 1010 | - FILE * fileHandle = NULL; | |
| 1011 | - if(strIni) | |
| 1012 | - { | |
| 1013 | - // Create the parent directoy if this is a pathname. | |
| 1014 | -// if((strchr(strIni->FileName.Cstr(), '\\') != NULL) || (strchr(strIni->FileName.Cstr(), '/') != NULL)) | |
| 1015 | -// { | |
| 1016 | -// Path pathName(strIni->FileName); | |
| 1017 | -// Path path; | |
| 1018 | -// pathName.GetParent(&path); | |
| 1019 | -// CreateAllDirectories(path.GetPath()); | |
| 1020 | -// } | |
| 1021 | - fileHandle = fopen(strIni->FileName.Cstr(), "w"); | |
| 1022 | - if(!fileHandle) | |
| 1023 | - { | |
| 1024 | -// LOGERROR("IniFile Can't create [%s] file", strIni->FileName.c_str()); | |
| 1025 | - _initCheck = B_ERROR; | |
| 1026 | - return NULL; | |
| 1027 | - } | |
| 1028 | - } | |
| 1029 | - return fileHandle; | |
| 1030 | -} | |
| 1031 | - | |
| 1032 | - | |
| 1033 | -/*-----------------------------------------------------------------* | |
| 1034 | - * SaveFile : Fonction permettant de sauvegarder le fichier .inf * | |
| 1035 | - * tout en effacant au fur et a mesure la structure. * | |
| 1036 | - *-----------------------------------------------------------------*/ | |
| 1037 | -status_t IniFile::SaveFileAndDelete(void) | |
| 1038 | -{ | |
| 1039 | -FILE *fileHandle; | |
| 1040 | -InfSection *strSection, *oldSection; | |
| 1041 | -InfVar *strVar, *oldVar; | |
| 1042 | -char *buffer; | |
| 1043 | -uint32 index = 0; | |
| 1044 | -char line[1024]; | |
| 1045 | -uint32 len; | |
| 1046 | - | |
| 1047 | - if(strIni == NULL) | |
| 1048 | - return B_NO_INIT; | |
| 1049 | - | |
| 1050 | -uint32 bufLen = 65536*8; | |
| 1051 | - buffer = (char *)malloc (sizeof (char) * (bufLen)); // 64KO | |
| 1052 | - if(buffer == NULL) | |
| 1053 | - { | |
| 1054 | -// LOGERROR("Can't allocate buffer in %s line %d", __FILE__, __LINE__); | |
| 1055 | - return B_NO_MEMORY; | |
| 1056 | - } | |
| 1057 | - | |
| 1058 | - /* Ouvrir fichier parametre */ | |
| 1059 | - fileHandle = SafeCreate(); | |
| 1060 | - if(fileHandle == NULL) | |
| 1061 | - { | |
| 1062 | -// LOGERROR("CS::Inifile::SaveFileAndDelete() can't create file !"); | |
| 1063 | - return B_FILE_ERROR; | |
| 1064 | - } | |
| 1065 | - | |
| 1066 | - strSection = strIni->FirstSection; | |
| 1067 | - while (strSection != (InfSection *)0) | |
| 1068 | - { | |
| 1069 | - /* Ecriture du nom de la section */ | |
| 1070 | - sprintf (line, "[%s]\n", strSection->SectionName); | |
| 1071 | - len = strlen(line); | |
| 1072 | - | |
| 1073 | - if(index + len > bufLen) | |
| 1074 | - { | |
| 1075 | - bufLen += len; | |
| 1076 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1077 | - } | |
| 1078 | - memcpy(buffer + index , line, len); | |
| 1079 | - index += len; | |
| 1080 | - | |
| 1081 | - /* Premiere variable */ | |
| 1082 | - strVar = strSection->FirstVar; | |
| 1083 | - while (strVar != (InfVar *)0) | |
| 1084 | - { | |
| 1085 | - switch(strVar->type) | |
| 1086 | - { | |
| 1087 | - case TYPE_DEFAULT: | |
| 1088 | - { | |
| 1089 | - /* Ecriture de la variable et de sa valeur */ | |
| 1090 | - sprintf (line, "%s=%s\n", strVar->VarName, strVar->VarVal); | |
| 1091 | - len = strlen(line); | |
| 1092 | - if(index + len > bufLen) | |
| 1093 | - { | |
| 1094 | - bufLen += len; | |
| 1095 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1096 | - } | |
| 1097 | - memcpy(buffer + index , line, len); | |
| 1098 | - index += len; | |
| 1099 | - break; | |
| 1100 | - } | |
| 1101 | - case TYPE_BASE64: | |
| 1102 | - { | |
| 1103 | - sprintf (line, "%s=@BEGIN base64 - Don't Edit@\n", strVar->VarName); | |
| 1104 | - len = strlen(line); | |
| 1105 | - if(index + len > bufLen) | |
| 1106 | - { | |
| 1107 | - bufLen += len; | |
| 1108 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1109 | - } | |
| 1110 | - memcpy(buffer + index , line, len); | |
| 1111 | - index += len; | |
| 1112 | - | |
| 1113 | - len = strlen(strVar->VarVal); | |
| 1114 | - if(index + len > bufLen) | |
| 1115 | - { | |
| 1116 | - bufLen += len; | |
| 1117 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1118 | - } | |
| 1119 | - memcpy(buffer + index , strVar->VarVal, len); | |
| 1120 | - index += len; | |
| 1121 | - | |
| 1122 | - // security control to handle an ununderstandable bug ('\n' not found in result buffer) | |
| 1123 | - if(buffer[index-1] != '\n') | |
| 1124 | - buffer[index++] = '\n'; | |
| 1125 | - | |
| 1126 | - sprintf (line, "@END base64@\n"); | |
| 1127 | - len = strlen(line); | |
| 1128 | - if(index + len > bufLen) | |
| 1129 | - { | |
| 1130 | - bufLen += len; | |
| 1131 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1132 | - } | |
| 1133 | - memcpy(buffer + index , line, len); | |
| 1134 | - index += len; | |
| 1135 | - break; | |
| 1136 | - } | |
| 1137 | - default: | |
| 1138 | - { | |
| 1139 | -// LOGWARNING("CSInifile save : unknown type [%d]", strVar->type); | |
| 1140 | - } | |
| 1141 | - | |
| 1142 | - } //switch | |
| 1143 | - | |
| 1144 | - /* Variable suivante */ | |
| 1145 | - oldVar = strVar; | |
| 1146 | - strVar = strVar->NextVar; | |
| 1147 | - | |
| 1148 | - /* Supprimer variable, valeur et structure */ | |
| 1149 | - free (oldVar->VarName); | |
| 1150 | - free (oldVar->VarVal); | |
| 1151 | - free (oldVar); | |
| 1152 | - | |
| 1153 | - //Un p'tit flush au cas ou | |
| 1154 | - if(fileHandle && index > 60000*8) | |
| 1155 | - { | |
| 1156 | - fwrite(buffer, index, 1, fileHandle); | |
| 1157 | - index = 0; | |
| 1158 | - } | |
| 1159 | - | |
| 1160 | - } //while | |
| 1161 | - | |
| 1162 | - sprintf (line, "\n"); | |
| 1163 | - len = strlen(line); | |
| 1164 | - if(index + len > bufLen) | |
| 1165 | - { | |
| 1166 | - bufLen += len; | |
| 1167 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1168 | - } | |
| 1169 | - memcpy(buffer + index , line, len); | |
| 1170 | - index += len; | |
| 1171 | - if(fileHandle && index > 60000*8) | |
| 1172 | - { | |
| 1173 | - fwrite(buffer, index, 1, fileHandle); | |
| 1174 | - index = 0; | |
| 1175 | - } | |
| 1176 | - /* Section suivante */ | |
| 1177 | - oldSection = strSection; | |
| 1178 | - strSection = strSection->NextSection; | |
| 1179 | - | |
| 1180 | - /* Supprimer nom et structure */ | |
| 1181 | - free (oldSection->SectionName); | |
| 1182 | - free (oldSection); | |
| 1183 | - } | |
| 1184 | - | |
| 1185 | - /* Fermer fichier parametre */ | |
| 1186 | - if(index && fileHandle) | |
| 1187 | - { | |
| 1188 | - fwrite(buffer, index, 1, fileHandle); | |
| 1189 | - } | |
| 1190 | - | |
| 1191 | - if(fileHandle) | |
| 1192 | - fclose (fileHandle); | |
| 1193 | - | |
| 1194 | - if(buffer) | |
| 1195 | - free (buffer); | |
| 1196 | - | |
| 1197 | - _mustSave = false; | |
| 1198 | - return B_OK; | |
| 1199 | -} | |
| 1200 | - | |
| 1201 | - | |
| 1202 | -/*-----------------------------------------------------------------* | |
| 1203 | - * SaveFile : Fonction permettant de sauvegarder le fichier .inf * | |
| 1204 | - * sans effacer la structure. * | |
| 1205 | - *-----------------------------------------------------------------*/ | |
| 1206 | -status_t IniFile::SaveFileOnly(void) | |
| 1207 | -{ | |
| 1208 | -FILE *fileHandle = NULL; | |
| 1209 | -InfSection *strSection = NULL; | |
| 1210 | -InfVar *strVar = NULL; | |
| 1211 | -char *buffer = NULL; | |
| 1212 | -uint32 index = 0; | |
| 1213 | -char line[1024]; | |
| 1214 | -uint32 len = 0; | |
| 1215 | - | |
| 1216 | - if(strIni == NULL) | |
| 1217 | - return B_NO_INIT; | |
| 1218 | - | |
| 1219 | -uint32 bufLen = 65536*8; | |
| 1220 | - buffer = (char *)malloc (sizeof (char) * (bufLen)); // 64KO | |
| 1221 | - | |
| 1222 | - if(buffer == NULL) | |
| 1223 | - { | |
| 1224 | -// LOGERROR("Can't allocate buffer in %s line %d", __FILE__, __LINE__); | |
| 1225 | - return B_NO_MEMORY; | |
| 1226 | - } | |
| 1227 | - | |
| 1228 | - /* Ouvrir fichier parametre */ | |
| 1229 | - fileHandle = SafeCreate(); | |
| 1230 | - if(fileHandle == NULL) | |
| 1231 | - { | |
| 1232 | -// LOGERROR("Can't open ini file '%s' in %s line %d", strIni->FileName.c_str(), __FILE__, __LINE__); | |
| 1233 | - return B_FILE_ERROR; | |
| 1234 | - } | |
| 1235 | - | |
| 1236 | - strSection = strIni->FirstSection; | |
| 1237 | - while (strSection != (InfSection *)0) | |
| 1238 | - { | |
| 1239 | - /* Ecriture du nom de la section */ | |
| 1240 | - sprintf (line, "[%s]\n", strSection->SectionName); | |
| 1241 | - len = strlen(line); | |
| 1242 | - if(index + len > bufLen) | |
| 1243 | - { | |
| 1244 | - bufLen += len; | |
| 1245 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1246 | - } | |
| 1247 | - memcpy(buffer + index , line, len); | |
| 1248 | - index += len; | |
| 1249 | - | |
| 1250 | - /* Premiere variable */ | |
| 1251 | - strVar = strSection->FirstVar; | |
| 1252 | - while (strVar != (InfVar *)0) | |
| 1253 | - { | |
| 1254 | - switch(strVar->type) | |
| 1255 | - { | |
| 1256 | - case TYPE_DEFAULT: | |
| 1257 | - { | |
| 1258 | - /* Ecriture de la variable et de sa valeur */ | |
| 1259 | - sprintf (line, "%s=%s\n", strVar->VarName, strVar->VarVal); | |
| 1260 | - len = strlen(line); | |
| 1261 | - if(index + len > bufLen) | |
| 1262 | - { | |
| 1263 | - bufLen += len; | |
| 1264 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1265 | - } | |
| 1266 | - memcpy(buffer + index , line, len); | |
| 1267 | - index += len; | |
| 1268 | - break; | |
| 1269 | - } | |
| 1270 | - case TYPE_BASE64: | |
| 1271 | - { | |
| 1272 | - sprintf (line, "%s=@BEGIN base64 - Don't Edit@\n", strVar->VarName); | |
| 1273 | - len = strlen(line); | |
| 1274 | - if(index + len > bufLen) | |
| 1275 | - { | |
| 1276 | - bufLen += len; | |
| 1277 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1278 | - } | |
| 1279 | - memcpy(buffer + index , line, len); | |
| 1280 | - index += len; | |
| 1281 | - | |
| 1282 | - len = strlen(strVar->VarVal); | |
| 1283 | - if(index + len > bufLen) | |
| 1284 | - { | |
| 1285 | - bufLen += len; | |
| 1286 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1287 | - } | |
| 1288 | - memcpy(buffer + index , strVar->VarVal, len); | |
| 1289 | - index += len; | |
| 1290 | - | |
| 1291 | - // security control to handle an ununderstandable bug ('\n' not found in result buffer) | |
| 1292 | - if(buffer[index-1] != '\n') | |
| 1293 | - buffer[index++] = '\n'; | |
| 1294 | - | |
| 1295 | - sprintf (line, "@END base64@\n"); | |
| 1296 | - len = strlen(line); | |
| 1297 | - if(index + len > bufLen) | |
| 1298 | - { | |
| 1299 | - bufLen += len; | |
| 1300 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1301 | - } | |
| 1302 | - memcpy(buffer + index , line, len); | |
| 1303 | - index += len; | |
| 1304 | - break; | |
| 1305 | - } | |
| 1306 | - default: | |
| 1307 | - { | |
| 1308 | -// LOGWARNING("CSInifile save : unknown type [%d]", strVar->type); | |
| 1309 | - } | |
| 1310 | - } //switch | |
| 1311 | - | |
| 1312 | -// /* Ecriture de la variable et de sa valeur */ | |
| 1313 | -// sprintf (line, "%s=%s\n", strVar->VarName, strVar->VarVal); | |
| 1314 | -// len = strlen(line); | |
| 1315 | -// memcpy(buffer + index , line, len); | |
| 1316 | -// index += len; | |
| 1317 | -// | |
| 1318 | - /* Variable suivante */ | |
| 1319 | - strVar = strVar->NextVar; | |
| 1320 | - } | |
| 1321 | - | |
| 1322 | - sprintf (line, "\n"); | |
| 1323 | - len = strlen(line); | |
| 1324 | - if(index + len > bufLen) | |
| 1325 | - { | |
| 1326 | - bufLen += len; | |
| 1327 | - buffer = (char *)realloc (buffer, bufLen); | |
| 1328 | - } | |
| 1329 | - memcpy(buffer + index , line, len); | |
| 1330 | - index += len; | |
| 1331 | - if(fileHandle && index > 60000) | |
| 1332 | - { | |
| 1333 | - fwrite(buffer, index, 1, fileHandle); | |
| 1334 | - index = 0; | |
| 1335 | - } | |
| 1336 | - /* Section suivante */ | |
| 1337 | - strSection = strSection->NextSection; | |
| 1338 | - } | |
| 1339 | - | |
| 1340 | - /* Fermer fichier parametre */ | |
| 1341 | - if(index && fileHandle) | |
| 1342 | - { | |
| 1343 | - fwrite(buffer, index, 1, fileHandle); | |
| 1344 | - } | |
| 1345 | - | |
| 1346 | - if(fileHandle) | |
| 1347 | - fclose (fileHandle); | |
| 1348 | - | |
| 1349 | - if(buffer) | |
| 1350 | - free (buffer); | |
| 1351 | - | |
| 1352 | - return B_OK; | |
| 1353 | -} | |
| 1354 | - | |
| 1355 | -bool IniFile::IsSection(const char *section) const | |
| 1356 | -{ | |
| 1357 | - InfSection *strSection; | |
| 1358 | - | |
| 1359 | - if(_lastIsSection && strcmp(_lastIsSection->SectionName, section) == 0) | |
| 1360 | - { | |
| 1361 | - _lastIsSection = _lastIsSection->NextSection; | |
| 1362 | - return true; | |
| 1363 | - } | |
| 1364 | - | |
| 1365 | - /* Recherche de la section */ | |
| 1366 | - strSection = strIni->FirstSection; | |
| 1367 | - while ((strSection != (InfSection *)0)) | |
| 1368 | - { | |
| 1369 | - if(strcmp(strSection->SectionName, section) == 0) | |
| 1370 | - { | |
| 1371 | - _lastIsSection = strSection->NextSection; | |
| 1372 | - return true; | |
| 1373 | - } | |
| 1374 | - strSection = strSection->NextSection; | |
| 1375 | - } | |
| 1376 | - | |
| 1377 | - return false; | |
| 1378 | -} | |
| 1379 | - | |
| 1380 | -/*-----------------------------------------------------------------* | |
| 1381 | - * GetValeur : Fonction permettant de recuperer le contenu d'une * | |
| 1382 | - * variable donnee d'une section donnee. * | |
| 1383 | - * * | |
| 1384 | - * Entree : Le pointeur sur la structure de donnees. * | |
| 1385 | - * le nom de la section * | |
| 1386 | - * le nom de la variable * | |
| 1387 | - * I/O : Le buffer qui va contenir la valeur * | |
| 1388 | - * * | |
| 1389 | - * Derniere modification : 11 decembre 1996 * | |
| 1390 | - *-----------------------------------------------------------------*/ | |
| 1391 | -status_t IniFile::GetValeur (const char *section, const char *var, char *buf, int * len) const | |
| 1392 | -{ | |
| 1393 | - status_t err = B_OK; | |
| 1394 | - InfSection *strSection = GetSection(section); | |
| 1395 | - InfVar *strVar; | |
| 1396 | - | |
| 1397 | - /* Si section non trouvee, buffer vide */ | |
| 1398 | - if (strSection == (InfSection *)0) | |
| 1399 | - { | |
| 1400 | - strcpy (buf, ""); | |
| 1401 | - err = B_NAME_NOT_FOUND; | |
| 1402 | - } | |
| 1403 | - else | |
| 1404 | - { | |
| 1405 | - /* Recherche de la variable */ | |
| 1406 | - strVar = strSection->FirstVar; | |
| 1407 | - while ((strVar != (InfVar *)0) && strcmp(strVar->VarName, var) != 0) | |
| 1408 | - strVar = strVar->NextVar; | |
| 1409 | - | |
| 1410 | - /* Si variable non trouvee, buffer vide */ | |
| 1411 | - if (strVar == (InfVar *)0) | |
| 1412 | - { | |
| 1413 | - strcpy (buf, ""); | |
| 1414 | - err = B_NAME_NOT_FOUND; | |
| 1415 | - } | |
| 1416 | - else /* sinon copier la valeur dans le buffer */ | |
| 1417 | - { | |
| 1418 | - //J'ai un buffer dont la longueur est donnee, c'est a dire que | |
| 1419 | - //je peux l'agrandir le cas echeant | |
| 1420 | - if(len) | |
| 1421 | - { | |
| 1422 | -// int llen = *len; | |
| 1423 | - int varLen = strlen(strVar->VarVal) + 1; | |
| 1424 | - | |
| 1425 | - if(*len < varLen) | |
| 1426 | - { | |
| 1427 | - *len = varLen; | |
| 1428 | - return B_NO_MEMORY; | |
| 1429 | -// SAFE_DELETE_TAB(buf); | |
| 1430 | -// buf = new char[varLen+1]; | |
| 1431 | -// *len = varLen; | |
| 1432 | - } | |
| 1433 | - } | |
| 1434 | - | |
| 1435 | - strcpy (buf, strVar->VarVal); | |
| 1436 | - } | |
| 1437 | - } | |
| 1438 | - return err; | |
| 1439 | -} | |
| 1440 | - | |
| 1441 | -bool IniFile::SetSectionName(const char *oldSectionName, const char *newSectionName) | |
| 1442 | -{ | |
| 1443 | - InfSection *infSection = GetSection(oldSectionName); | |
| 1444 | - if(infSection) | |
| 1445 | - { | |
| 1446 | - strcpy(infSection->SectionName, newSectionName); | |
| 1447 | - return true; | |
| 1448 | - } | |
| 1449 | - return false; | |
| 1450 | -} | |
| 1451 | - | |
| 1452 | -InfSection * | |
| 1453 | -IniFile::GetSection(const char *section) const | |
| 1454 | -{ | |
| 1455 | - InfSection *strSection = NULL; | |
| 1456 | - | |
| 1457 | - if(_lastReadSection && strcmp(_lastReadSection->SectionName, section) == 0) | |
| 1458 | - { | |
| 1459 | - strSection = _lastReadSection; | |
| 1460 | - } | |
| 1461 | - else if(_lastReadSection && | |
| 1462 | - _lastReadSection->NextSection && | |
| 1463 | - strcmp(_lastReadSection->NextSection->SectionName, section) == 0) | |
| 1464 | - { | |
| 1465 | - _lastReadSection = strSection = _lastReadSection->NextSection; | |
| 1466 | - } | |
| 1467 | - else | |
| 1468 | - { | |
| 1469 | - /* Recherche de la section */ | |
| 1470 | - strSection = strIni->FirstSection; | |
| 1471 | - while ((strSection != (InfSection *)0) && strcmp(strSection->SectionName, section) != 0) | |
| 1472 | - strSection = strSection->NextSection; | |
| 1473 | - | |
| 1474 | - if(strSection) | |
| 1475 | - _lastReadSection = strSection; | |
| 1476 | - } | |
| 1477 | - return strSection; | |
| 1478 | -} | |
| 1479 | - | |
| 1480 | -InfVar * | |
| 1481 | -IniFile::GetVariable(const char *var) const | |
| 1482 | -{ | |
| 1483 | - InfVar * strVar = _lastReadSection->FirstVar; | |
| 1484 | - while ((strVar != (InfVar *)0) && strcmp(strVar->VarName, var) != 0) | |
| 1485 | - strVar = strVar->NextVar; | |
| 1486 | - return strVar; | |
| 1487 | -} | |
| 1488 | - | |
| 1489 | -InfVar * | |
| 1490 | -IniFile::GetVariable(const char *section, const char *var) const | |
| 1491 | -{ | |
| 1492 | - GetSection(section); // setup the cached value of _lastReadSection | |
| 1493 | - return GetVariable(var); | |
| 1494 | -} | |
| 1495 | - | |
| 1496 | -bool | |
| 1497 | -IniFile::IsVariable(const char * sectionName, const char * var) const | |
| 1498 | -{ | |
| 1499 | - InfSection *strSection = GetSection(sectionName); | |
| 1500 | - if(strSection) | |
| 1501 | - { | |
| 1502 | - InfVar * strVar = GetVariable(var); | |
| 1503 | - if(strVar) | |
| 1504 | - return true; | |
| 1505 | - } | |
| 1506 | - return false; | |
| 1507 | -} | |
| 1508 | - | |
| 1509 | -bool | |
| 1510 | -IniFile::GetFirstVarName(const char * sectionName, char * var) | |
| 1511 | -{ | |
| 1512 | - InfSection *strSection = GetSection(sectionName); | |
| 1513 | - if(strSection) | |
| 1514 | - { | |
| 1515 | - InfVar * strVar = strSection->FirstVar; | |
| 1516 | - strcpy(var, strVar->VarName); | |
| 1517 | - _currentstrVar = strVar; | |
| 1518 | - return true; | |
| 1519 | - } | |
| 1520 | - _currentstrVar = NULL; | |
| 1521 | - return false; | |
| 1522 | -} | |
| 1523 | - | |
| 1524 | -bool | |
| 1525 | -IniFile::GetNextVarName(char * var) | |
| 1526 | -{ | |
| 1527 | - if(_currentstrVar) | |
| 1528 | - { | |
| 1529 | - InfVar * strVar = _currentstrVar->NextVar; | |
| 1530 | - if (strVar) | |
| 1531 | - { | |
| 1532 | - strcpy(var, strVar->VarName); | |
| 1533 | - _currentstrVar = strVar; | |
| 1534 | - return true; | |
| 1535 | - } | |
| 1536 | - } | |
| 1537 | - _currentstrVar = NULL; | |
| 1538 | - return false; | |
| 1539 | -} | |
| 1540 | - | |
| 1541 | -char * uI64toa( uint64 value,char * pstr) | |
| 1542 | -{ | |
| 1543 | -#ifdef WIN32 | |
| 1544 | - return _ui64toa(value, pstr, 10); | |
| 1545 | -#endif | |
| 1546 | - return NULL; | |
| 1547 | -} | |
| 1548 | - | |
| 1549 | -char * I64toa( int64 value,char * pstr) | |
| 1550 | -{ | |
| 1551 | -#ifdef WIN32 | |
| 1552 | - return _i64toa(value, pstr, 10); | |
| 1553 | -#endif | |
| 1554 | - return NULL; | |
| 1555 | -} | |
| 1556 | - | |
| 1557 | -int64 CM::atoI64(const char * pstr) | |
| 1558 | -{ | |
| 1559 | -#ifdef WIN32 | |
| 1560 | - return _atoi64(pstr); | |
| 1561 | -#endif | |
| 1562 | - return 0; | |
| 1563 | -} | |
| 1564 | - | |
| 1565 | -double CM::worldwideAtoF(const char *z) | |
| 1566 | -{ | |
| 1567 | - int sign = 1; | |
| 1568 | - long double v1 = 0.0; | |
| 1569 | - if( *z=='-' ) | |
| 1570 | - { | |
| 1571 | - sign = -1; | |
| 1572 | - z++; | |
| 1573 | - } | |
| 1574 | - else if( *z=='+' ) | |
| 1575 | - { | |
| 1576 | - z++; | |
| 1577 | - } | |
| 1578 | - | |
| 1579 | - while( isdigit(*z) ) | |
| 1580 | - { | |
| 1581 | - v1 = v1*10.0 + (*z - '0'); | |
| 1582 | - z++; | |
| 1583 | - } | |
| 1584 | - | |
| 1585 | - //in a worldwide, they are two possible decimal point, | |
| 1586 | - //the dot for english speaking country | |
| 1587 | - //and comma for the rest of world | |
| 1588 | - if( *z == '.' || *z == ',') | |
| 1589 | - { | |
| 1590 | - long double divisor = 1.0; | |
| 1591 | - z++; | |
| 1592 | - while( isdigit(*z) ) | |
| 1593 | - { | |
| 1594 | - v1 = v1*10.0 + (*z - '0'); | |
| 1595 | - divisor *= 10.0; | |
| 1596 | - z++; | |
| 1597 | - } | |
| 1598 | - v1 /= divisor; | |
| 1599 | - } | |
| 1600 | - | |
| 1601 | - if( *z=='e' || *z=='E' ){ | |
| 1602 | - int esign = 1; | |
| 1603 | - int eval = 0; | |
| 1604 | - long double scale = 1.0; | |
| 1605 | - z++; | |
| 1606 | - if( *z=='-' ) | |
| 1607 | - { | |
| 1608 | - esign = -1; | |
| 1609 | - z++; | |
| 1610 | - } | |
| 1611 | - else if( *z=='+' ) | |
| 1612 | - { | |
| 1613 | - z++; | |
| 1614 | - } | |
| 1615 | - | |
| 1616 | - while( isdigit(*z) ) | |
| 1617 | - { | |
| 1618 | - eval = eval*10 + *z - '0'; | |
| 1619 | - z++; | |
| 1620 | - } | |
| 1621 | - while( eval>=64 ) | |
| 1622 | - { | |
| 1623 | - scale *= 1.0e+64; eval -= 64; | |
| 1624 | - } | |
| 1625 | - while( eval>=16 ) | |
| 1626 | - { | |
| 1627 | - scale *= 1.0e+16; eval -= 16; | |
| 1628 | - } | |
| 1629 | - while( eval>=4 ) | |
| 1630 | - { | |
| 1631 | - scale *= 1.0e+4; eval -= 4; | |
| 1632 | - } | |
| 1633 | - while( eval>=1 ) | |
| 1634 | - { | |
| 1635 | - scale *= 1.0e+1; eval -= 1; | |
| 1636 | - } | |
| 1637 | - if( esign<0 ) | |
| 1638 | - { | |
| 1639 | - v1 /= scale; | |
| 1640 | - }else | |
| 1641 | - { | |
| 1642 | - v1 *= scale; | |
| 1643 | - } | |
| 1644 | - } | |
| 1645 | - return sign<0 ? -v1 : v1; | |
| 1646 | -} | |
| 1647 | - | |
| 1648 | - /* | |
| 1649 | - ** ftoa - FLOATING POINT TO ASCII CONVERSION | |
| 1650 | - ** | |
| 1651 | - ** CODE derived from ingres, ~ingres/source/gutil/ftoa.c | |
| 1652 | - ** | |
| 1653 | - ** 'Value' is converted to an ascii character string and stored | |
| 1654 | - ** into 'ascii'. Ascii should have room for at least 'width' + 1 | |
| 1655 | - ** characters. 'Width' is the width of the output field (max). | |
| 1656 | - ** 'Prec' is the number of characters to put after the decimal | |
| 1657 | - ** point. The format of the output string is controlled by | |
| 1658 | - ** 'format'. | |
| 1659 | - ** | |
| 1660 | - ** 'Format' can be: | |
| 1661 | - ** e or E: "E" format output | |
| 1662 | - ** f or F: "F" format output | |
| 1663 | - ** g or G: "F" format output if it will fit, otherwise | |
| 1664 | - ** use "E" format. | |
| 1665 | - ** n or N: same as G, but decimal points will not always | |
| 1666 | - ** be aligned. | |
| 1667 | - ** | |
| 1668 | - ** If 'format' is upper case, the "E" comes out in upper case; | |
| 1669 | - ** otherwise it comes out in lower case. | |
| 1670 | - ** | |
| 1671 | - ** When the field width is not big enough, it fills the field with | |
| 1672 | - ** stars ("*****") and returns zero. Normal return is the width | |
| 1673 | - ** of the output field (sometimes shorter than 'width'). | |
| 1674 | - */ | |
| 1675 | - int CM::worldwideFtoA(double value, char *ascii, int width, int prec1, char format) | |
| 1676 | -{ | |
| 1677 | -auto int expon; | |
| 1678 | -auto int sign; | |
| 1679 | -int avail = 0; | |
| 1680 | -char *a = NULL; | |
| 1681 | -char *p = NULL; | |
| 1682 | -char mode; | |
| 1683 | -int lowercase; | |
| 1684 | -int prec; | |
| 1685 | - | |
| 1686 | - /* extern char *ecvt(), *fcvt();*/ | |
| 1687 | - | |
| 1688 | - prec = prec1; | |
| 1689 | - mode = format; | |
| 1690 | - lowercase = 'a' - 'A'; | |
| 1691 | - if (mode >= 'a') | |
| 1692 | - mode -= 'a' - 'A'; | |
| 1693 | - else | |
| 1694 | - lowercase = 0; | |
| 1695 | - | |
| 1696 | - if (mode != 'E') | |
| 1697 | - { | |
| 1698 | - /* try 'F' style output */ | |
| 1699 | - p = fcvt(value, prec, &expon, &sign); | |
| 1700 | - avail = width; | |
| 1701 | - a = ascii; | |
| 1702 | - | |
| 1703 | - /* output sign */ | |
| 1704 | - if (sign) | |
| 1705 | - { | |
| 1706 | - avail--; | |
| 1707 | - *a++ = '-'; | |
| 1708 | - } | |
| 1709 | - | |
| 1710 | - /* output '0' before the decimal point */ | |
| 1711 | - if (expon <= 0) | |
| 1712 | - { | |
| 1713 | - *a++ = '0'; | |
| 1714 | - avail--; | |
| 1715 | - } | |
| 1716 | - | |
| 1717 | - /* compute space length left after dec pt and fraction */ | |
| 1718 | - avail -= prec + 1; | |
| 1719 | - if (mode == 'G') | |
| 1720 | - avail -= 4; | |
| 1721 | - | |
| 1722 | - if (avail >= expon) | |
| 1723 | - { | |
| 1724 | - | |
| 1725 | - /* it fits. output */ | |
| 1726 | - while (expon > 0) | |
| 1727 | - { | |
| 1728 | - /* output left of dp */ | |
| 1729 | - expon--; | |
| 1730 | - if (*p) | |
| 1731 | - *a++ = *p++; | |
| 1732 | - else | |
| 1733 | - *a++ = '0'; | |
| 1734 | - } | |
| 1735 | - | |
| 1736 | - /* output fraction (right of dec pt) */ | |
| 1737 | - avail = expon; | |
| 1738 | - goto frac_out; | |
| 1739 | - } | |
| 1740 | - /* won't fit; let's hope for G format */ | |
| 1741 | - } | |
| 1742 | - | |
| 1743 | - if (mode != 'F') | |
| 1744 | - { | |
| 1745 | - /* try to do E style output */ | |
| 1746 | - p = ecvt(value, prec + 1, &expon, &sign); | |
| 1747 | - avail = width - 5; | |
| 1748 | - a = ascii; | |
| 1749 | - | |
| 1750 | - /* output the sign */ | |
| 1751 | - if (sign) | |
| 1752 | - { | |
| 1753 | - *a++ = '-'; | |
| 1754 | - avail--; | |
| 1755 | - } | |
| 1756 | - } | |
| 1757 | - | |
| 1758 | - /* check for field too small */ | |
| 1759 | - if (mode == 'F' || avail < prec) | |
| 1760 | - { | |
| 1761 | - /* sorry joker, you lose */ | |
| 1762 | - a = ascii; | |
| 1763 | - for (avail = width; avail > 0; avail--) | |
| 1764 | - *a++ = '*'; | |
| 1765 | - *a = 0; | |
| 1766 | - return 0; | |
| 1767 | - } | |
| 1768 | - | |
| 1769 | - /* it fits; output the number */ | |
| 1770 | - mode = 'E'; | |
| 1771 | - | |
| 1772 | - /* output the LHS single digit */ | |
| 1773 | - *a++ = *p++; | |
| 1774 | - expon--; | |
| 1775 | - | |
| 1776 | - /* output the rhs */ | |
| 1777 | - avail = 1; | |
| 1778 | - | |
| 1779 | - frac_out: | |
| 1780 | - *a++ = '.'; | |
| 1781 | - while (prec > 0) | |
| 1782 | - { | |
| 1783 | - prec--; | |
| 1784 | - if (avail < 0) | |
| 1785 | - { | |
| 1786 | - avail++; | |
| 1787 | - *a++ = '0'; | |
| 1788 | - } | |
| 1789 | - else | |
| 1790 | - { | |
| 1791 | - if (*p) | |
| 1792 | - *a++ = *p++; | |
| 1793 | - else | |
| 1794 | - *a++ = '0'; | |
| 1795 | - } | |
| 1796 | - } | |
| 1797 | - | |
| 1798 | - /* output the exponent */ | |
| 1799 | - if (mode == 'E') | |
| 1800 | - { | |
| 1801 | - *a++ = 'E' + lowercase; | |
| 1802 | - if (expon < 0) | |
| 1803 | - { | |
| 1804 | - *a++ = '-'; | |
| 1805 | - expon = -expon; | |
| 1806 | - } | |
| 1807 | - else | |
| 1808 | - *a++ = '+'; | |
| 1809 | - *a++ = (expon / 10) % 10 + '0'; | |
| 1810 | - *a++ = expon % 10 + '0'; | |
| 1811 | - } | |
| 1812 | - | |
| 1813 | - /* output spaces on the end in G format */ | |
| 1814 | - if (mode == 'G') | |
| 1815 | - { | |
| 1816 | - *a++ = ' '; | |
| 1817 | - *a++ = ' '; | |
| 1818 | - *a++ = ' '; | |
| 1819 | - *a++ = ' '; | |
| 1820 | - } | |
| 1821 | - | |
| 1822 | - /* finally, we can return */ | |
| 1823 | - *a = 0; | |
| 1824 | - avail = a - ascii; | |
| 1825 | - return avail; | |
| 1826 | -} |
anubis_dev/vm/src/IniFile.h deleted
| 1 | -#ifndef __INIFILE_H__ | |
| 2 | -#define __INIFILE_H__ | |
| 3 | - | |
| 4 | -#include "AnubisSupport.h" | |
| 5 | -#include "String.h" | |
| 6 | -//#include "MediaListeBuild.h" | |
| 7 | -//#include "root/support/PortableTypeDefs.h" | |
| 8 | -//#include "root/PortableString.h" | |
| 9 | -//#include "root/PortableMessage.h" | |
| 10 | -//#include "root/StringList.h" | |
| 11 | - | |
| 12 | -#include <stdio.h> | |
| 13 | - | |
| 14 | -BEGIN_NAMESPACE(CM); | |
| 15 | - | |
| 16 | -/* Une ligne du fichier (autre qu'un entete de section) ==> NomVariable=valeur */ | |
| 17 | -typedef struct _var | |
| 18 | -{ | |
| 19 | - char *VarName; /* Nom de la variable */ | |
| 20 | - char *VarVal; /* Valeur de la variable */ | |
| 21 | - uint8 type; /* Type de variable */ | |
| 22 | - struct _var *NextVar; /* Variable suivante */ | |
| 23 | -} InfVar; | |
| 24 | - | |
| 25 | -/* Liste des sections ==> [Nom de la section] */ | |
| 26 | -typedef struct _sect | |
| 27 | -{ | |
| 28 | - char *SectionName; /* Nom de la Section */ | |
| 29 | - InfVar *FirstVar; /* Premier element de la section */ | |
| 30 | - struct _sect *NextSection; /* Section suivante */ | |
| 31 | -} InfSection; | |
| 32 | - | |
| 33 | -/* Structure d'un fichier .inf */ | |
| 34 | -typedef struct _inf | |
| 35 | -{ | |
| 36 | - String FileName; | |
| 37 | - InfSection *FirstSection; | |
| 38 | -} inifile_struct; | |
| 39 | - | |
| 40 | -/** | |
| 41 | - * Fonctions de fichier INI comme MS Windows | |
| 42 | - */ | |
| 43 | - | |
| 44 | - | |
| 45 | - | |
| 46 | -/** | |
| 47 | - */ | |
| 48 | - | |
| 49 | -class IniFile | |
| 50 | -{ | |
| 51 | - | |
| 52 | - public: | |
| 53 | - | |
| 54 | - IniFile(const char *nomFic); | |
| 55 | - | |
| 56 | - IniFile(const String &fileName); | |
| 57 | - | |
| 58 | - virtual ~IniFile(void); | |
| 59 | - | |
| 60 | - /** | |
| 61 | - * Give the initialization status. Especialy return an error when openning | |
| 62 | - * the file fail (maybe due to a lock by another process?). | |
| 63 | - * Call this before to read or write value. | |
| 64 | - */ | |
| 65 | - status_t InitCheck() const; | |
| 66 | - | |
| 67 | - /** Ecrit un booleen | |
| 68 | - * @param section nom de la section dans laquelle se trouve la valeur a ecrire | |
| 69 | - * @param var nom de la variable a ecrire dans la section | |
| 70 | - * @param val valeur a ecrire | |
| 71 | - */ | |
| 72 | -void WriteBool(const char *section, const char *var, bool val); | |
| 73 | -void WriteChar(const char *section, const char *var, char val); | |
| 74 | -void WriteInteger(const char *section, const char *var, int val); | |
| 75 | -void WriteInt64(const char *section, const char *var, int64 val); | |
| 76 | -void WriteInt32(const char *section, const char *var, int32 val); | |
| 77 | -void WriteInt16(const char *section, const char *var, int16 val); | |
| 78 | -void WriteLong(const char *section, const char *var, long val); | |
| 79 | -void WriteFloat(const char *section, const char *var, float val); | |
| 80 | -void WriteDouble(const char *section, const char *var, double val); | |
| 81 | -void WriteString(const char *section, const char *var, const char *val); | |
| 82 | -void WriteString(const char *section, const char *var, const String &str); | |
| 83 | -//void WriteStringList(const char *section, const char *var, char separator, const StringList & strList); | |
| 84 | -//void WriteMessage(const char *section, const char *var, const PortableMessage &); | |
| 85 | - /** | |
| 86 | - * Lit un booleen dans le fichier ini | |
| 87 | - * @return _B_OK si la variable a été trouvée, B_NAME_NOT_FOUND sinon | |
| 88 | - * @param section nom de la section dans laquelle se trouve la valeur recherche | |
| 89 | - * @param var nom de la variable a recherche dans la section | |
| 90 | - * @param def valeur par defaut au cas ou la valeur n'est pas trouve | |
| 91 | - * @param val pointeur sur la variable devant recevoir le resultat | |
| 92 | - */ | |
| 93 | -status_t ReadBool(const char *section, const char *var, bool def, bool *val) const; | |
| 94 | -status_t ReadChar(const char *section, const char *var, char def, char *val) const; | |
| 95 | -status_t ReadInteger(const char *section, const char *var, int def, int *val) const; | |
| 96 | -status_t ReadInt64(const char *section, const char *var, int64 def, int64 *val) const; | |
| 97 | -status_t ReadInt32(const char *section, const char *var, int32 def, int32 *val)const; | |
| 98 | -status_t ReadInt16(const char *section, const char *var, int16 def, int16 *val) const; | |
| 99 | -status_t ReadLong(const char *section, const char *var, long def, long *val) const; | |
| 100 | -status_t ReadFloat(const char *section, const char *var, float def, float *val) const; | |
| 101 | -status_t ReadDouble(const char *section, const char *var, double def, double *val) const; | |
| 102 | -status_t ReadString(const char *section, const char *var, const char *def, char *val) const; | |
| 103 | -status_t ReadString(const char *section, const char *var, const char *def, String &val) const; | |
| 104 | -//status_t ReadMessage(const char *section, const char *var, PortableMessage &val) const; | |
| 105 | -//status_t ReadStringList(const char *section, const char *var, char *pattern, StringList &val) const; | |
| 106 | - | |
| 107 | -/* Fonction de suppression de variables et de sections */ | |
| 108 | - | |
| 109 | - /** Permet de verifier si une section existe dans le fichier INI associee l'objet. | |
| 110 | - * @param sectionName section dont on cherche l'existence dans le fichier. | |
| 111 | - * @return true si la section demande existe sinon false | |
| 112 | - */ | |
| 113 | - bool IsSection(const char * sectionName) const; | |
| 114 | - /** | |
| 115 | - * Allow section name modification | |
| 116 | - * @param oldSectionName original section Name to be replaced | |
| 117 | - * @param newSectionName new section Name | |
| 118 | - */ | |
| 119 | - bool SetSectionName(const char *oldSectionName, const char *newSectionName); | |
| 120 | - | |
| 121 | - /** | |
| 122 | - * Permet de verifier si une variable existe dans dasn une section donnée. | |
| 123 | - * @param sectionName section contenant la variable | |
| 124 | - * @param var nom de la variable recherchée | |
| 125 | - * @return true si la section demande existe sinon false | |
| 126 | - */ | |
| 127 | - bool IsVariable(const char * sectionName, const char * var) const; | |
| 128 | - | |
| 129 | - /** | |
| 130 | - * Permet connaitre la première entrée dans la section donnée. | |
| 131 | - * @param sectionName section contenant la variable | |
| 132 | - * @param var nom de la première variable | |
| 133 | - * @return true si une variable existe sinon false | |
| 134 | - */ | |
| 135 | - bool GetFirstVarName(const char * sectionName, char * var); | |
| 136 | - /** | |
| 137 | - * Permet connaitre l'entrée suivante après l'appel à la fonction GetFirstVarName(). | |
| 138 | - * @param var nom de la variable | |
| 139 | - * @return true si une variable existe sinon false | |
| 140 | - */ | |
| 141 | - bool GetNextVarName(char * var); | |
| 142 | - | |
| 143 | - InfSection *AddSectionInCache (const char *section); | |
| 144 | - void ReadCache(FILE* fileHandle); | |
| 145 | - | |
| 146 | - /** Sauve le contenu de l'arborescence en memoire sur disque | |
| 147 | - * */ | |
| 148 | - status_t Update(void); | |
| 149 | - void MustSaveOnQuit(void) | |
| 150 | - { _mustSave = true;} | |
| 151 | - | |
| 152 | - /** | |
| 153 | - * If an error occurs while reading the file, this function returns the error code. | |
| 154 | - * When a read error is encountered, the IniFile is still usable but some | |
| 155 | - * datas may be missing. | |
| 156 | - */ | |
| 157 | - status_t GetReadError() const { | |
| 158 | - return _readError; | |
| 159 | - } | |
| 160 | - | |
| 161 | - /** | |
| 162 | - * Delete all sections and variables from this IniFile. | |
| 163 | - */ | |
| 164 | - void Clear() { | |
| 165 | - FreeTree(); | |
| 166 | - } | |
| 167 | - /** | |
| 168 | - * Delete a conplete section with all the subsections. | |
| 169 | - */ | |
| 170 | - void DeleteSection (const char *section); | |
| 171 | - /** | |
| 172 | - * Delete a variable in a section | |
| 173 | - */ | |
| 174 | - void DeleteVar(const char *section, const char *var); | |
| 175 | - | |
| 176 | -private: | |
| 177 | - status_t _initCheck; | |
| 178 | - inifile_struct *strIni; | |
| 179 | - mutable InfSection *_lastReadSection; | |
| 180 | - mutable InfSection *_lastIsSection; | |
| 181 | - char * _readCache; | |
| 182 | - size_t _cacheByteCount; | |
| 183 | - size_t _cacheByteIdx; | |
| 184 | - // Variable servant uniquement pour les fonctions GetFirstVarName() et GetNextVarName() | |
| 185 | - InfVar * _currentstrVar; | |
| 186 | - | |
| 187 | - void Init(const char *nomFic); | |
| 188 | - FILE *SafeCreate(); | |
| 189 | - int ReadLine (FILE *ficIni, char *ligne, size_t lineSize); | |
| 190 | - void ReadFile (FILE *fileHandle); | |
| 191 | - InfSection *AddSection (const char *section); | |
| 192 | - | |
| 193 | - status_t AddVar (const char *section, const char *var, const char *val, uint8 = 0); | |
| 194 | - | |
| 195 | - status_t SaveFileAndDelete(void); | |
| 196 | - status_t SaveFileOnly(void); | |
| 197 | - void FreeTree(void); | |
| 198 | - /** | |
| 199 | - * Free all datas without saving them. | |
| 200 | - */ | |
| 201 | - void InfFree (void); | |
| 202 | - | |
| 203 | - /** | |
| 204 | - * Fonction permettant de recuperer le contenu d'une variable donnee d'une | |
| 205 | - * section donnee. | |
| 206 | - * | |
| 207 | - * @return B_OK if the section and value if found, B_NAME_NOT_FOUND else | |
| 208 | - * @param section le nom de la section | |
| 209 | - * @param var le nom de la variable | |
| 210 | - * @param len target buffer size | |
| 211 | - */ | |
| 212 | - status_t GetValeur(const char *section, const char *var, char *buf, int * len = 0 ) const; | |
| 213 | - | |
| 214 | - /** | |
| 215 | - * Returns the section named \a section. | |
| 216 | - */ | |
| 217 | - InfSection * GetSection(const char *section) const; | |
| 218 | - /** | |
| 219 | - * Returns the variable named \a var from the section named \a section. | |
| 220 | - */ | |
| 221 | - InfVar * GetVariable(const char *section, const char *var) const; | |
| 222 | - /** | |
| 223 | - * Returns the variable named \a var from the current section | |
| 224 | - * (section in \c _lastReadSection). | |
| 225 | - */ | |
| 226 | - InfVar * GetVariable(const char *var) const; | |
| 227 | - | |
| 228 | - bool _mustSave; | |
| 229 | - status_t _readError; | |
| 230 | -}; | |
| 231 | - | |
| 232 | -char * uI64toa( uint64 value, char * pstr); | |
| 233 | -char * I64toa( int64 value, char * pstr); | |
| 234 | -int64 atoI64( const char * pstr); | |
| 235 | -double worldwideAtoF(const char *z); | |
| 236 | -int worldwideFtoA(double value, char *ascii, int width, int prec1, char format); | |
| 237 | - | |
| 238 | -END_NAMESPACE(CM); | |
| 239 | - | |
| 240 | -#endif |
anubis_dev/vm/src/String.cpp deleted
| 1 | -/* This file is Copyright 2005 Level Control Systems. See the included LICENSE.txt file for details. */ | |
| 2 | - | |
| 3 | -#include "AnubisSupport.h" | |
| 4 | -#include "String.h" | |
| 5 | -//#include "system/GlobalMemoryAllocator.h" | |
| 6 | -#include <stdarg.h> | |
| 7 | -#include <stdio.h> | |
| 8 | -#include <stdlib.h> | |
| 9 | - | |
| 10 | -BEGIN_NAMESPACE(CM); | |
| 11 | - | |
| 12 | -String :: ~String() | |
| 13 | -{ | |
| 14 | - if (_buffer != _smallBuffer) | |
| 15 | - SAFE_FREE(_buffer); | |
| 16 | -} | |
| 17 | - | |
| 18 | -status_t | |
| 19 | -String::SetFromString(const String & s, uint32 firstChar, uint32 afterLastChar) | |
| 20 | -{ | |
| 21 | - afterLastChar = anubisMin(afterLastChar, s.Length()); | |
| 22 | - uint32 len = (afterLastChar > firstChar) ? (afterLastChar-firstChar) : 0; | |
| 23 | - if (len > 0) | |
| 24 | - { | |
| 25 | - if (EnsureBufferSize(len+1, false) != B_NO_ERROR) return B_ERROR; | |
| 26 | - if (&s == this) memmove(_buffer, s()+firstChar, len); | |
| 27 | - else memcpy( _buffer, s()+firstChar, len); | |
| 28 | - _buffer[len] = '\0'; | |
| 29 | - _length = len; | |
| 30 | - } | |
| 31 | - else Clear(); | |
| 32 | - | |
| 33 | - return B_NO_ERROR; | |
| 34 | -} | |
| 35 | - | |
| 36 | -status_t | |
| 37 | -String::SetCstr(const char * str, uint32 maxLen) | |
| 38 | -{ | |
| 39 | - // If (str)'s got a NUL byte before maxLen, make (maxLen) smaller. | |
| 40 | - // We can't call strlen(str) because we don't have any guarantee that the NUL | |
| 41 | - // byte even exists! Without a NUL byte, strlen() could run off into the weeds... | |
| 42 | - uint32 sLen = 0; | |
| 43 | - if (str) {while((sLen<maxLen)&&(str[sLen] != '\0')) sLen++;} | |
| 44 | - if (sLen < maxLen) maxLen = sLen; | |
| 45 | - if (maxLen > 0) | |
| 46 | - { | |
| 47 | - if (str[maxLen-1] != '\0') maxLen++; // make room to add the NUL byte if necessary | |
| 48 | - if (EnsureBufferSize(maxLen, false) != B_NO_ERROR) return B_ERROR; | |
| 49 | - if (anubisInRange((char *)str, _buffer, &_buffer[_length])) memmove(_buffer, str, maxLen-1); | |
| 50 | - else memcpy(_buffer, str, maxLen-1); | |
| 51 | - _buffer[maxLen-1] = '\0'; | |
| 52 | - _length = maxLen-1; | |
| 53 | - } | |
| 54 | - else Clear(); | |
| 55 | - | |
| 56 | - return B_NO_ERROR; | |
| 57 | -} | |
| 58 | - | |
| 59 | -String & | |
| 60 | -String::operator-=(const char aChar) | |
| 61 | -{ | |
| 62 | - int idx = LastIndexOf(aChar); | |
| 63 | - if (idx >= 0) | |
| 64 | - { | |
| 65 | - String temp = Substring(idx+1); | |
| 66 | - (*this) = this->Substring(0, idx); | |
| 67 | - (*this) += temp; | |
| 68 | - } | |
| 69 | - return *this; | |
| 70 | -} | |
| 71 | - | |
| 72 | -String & | |
| 73 | -String::operator+=(const String &other) | |
| 74 | -{ | |
| 75 | - uint32 otherLen = other.Length(); | |
| 76 | - if ((otherLen > 0)&&(EnsureBufferSize(Length() + otherLen + 1, true) == B_NO_ERROR)) | |
| 77 | - { | |
| 78 | - memcpy(&_buffer[_length], other(), other.Length()+1); | |
| 79 | - _length += otherLen; | |
| 80 | - } | |
| 81 | - return *this; | |
| 82 | -} | |
| 83 | - | |
| 84 | -String & | |
| 85 | -String::operator+=(const char * other) | |
| 86 | -{ | |
| 87 | - if (other == NULL) other = ""; | |
| 88 | - uint32 otherLen = strlen(other); | |
| 89 | - if (otherLen > 0) | |
| 90 | - { | |
| 91 | - if (anubisInRange(other, (const char *)_buffer, (const char *)(_buffer+_length))) return operator+=(String(other)); // avoid self-entanglement | |
| 92 | - else if (EnsureBufferSize(Length() + otherLen + 1, true) == B_NO_ERROR) | |
| 93 | - { | |
| 94 | - memcpy(&_buffer[_length], other, otherLen+1); | |
| 95 | - _length += otherLen; | |
| 96 | - } | |
| 97 | - } | |
| 98 | - return *this; | |
| 99 | -} | |
| 100 | - | |
| 101 | -String & | |
| 102 | -String::operator-=(const String &other) | |
| 103 | -{ | |
| 104 | - if (*this == other) *this = ""; | |
| 105 | - else | |
| 106 | - { | |
| 107 | - int idx = LastIndexOf(other); | |
| 108 | - if (idx >= 0) | |
| 109 | - { | |
| 110 | - String temp = Substring(idx+other.Length()); | |
| 111 | - (*this) = Substring(0, idx); | |
| 112 | - (*this) += temp; | |
| 113 | - } | |
| 114 | - } | |
| 115 | - return *this; | |
| 116 | -} | |
| 117 | - | |
| 118 | -String & | |
| 119 | -String::operator<<(int rhs) | |
| 120 | -{ | |
| 121 | - char buff[64]; | |
| 122 | - sprintf(buff, "%d", rhs); | |
| 123 | - return *this << buff; | |
| 124 | -} | |
| 125 | - | |
| 126 | -String & | |
| 127 | -String::operator<<(float rhs) | |
| 128 | -{ | |
| 129 | - char buff[64]; | |
| 130 | - sprintf(buff, "%.2f", rhs); | |
| 131 | - return *this << buff; | |
| 132 | -} | |
| 133 | - | |
| 134 | -String & | |
| 135 | -String::operator<<(bool rhs) | |
| 136 | -{ | |
| 137 | - const char* val = rhs ? "true" : "false"; | |
| 138 | - return *this << val; | |
| 139 | -} | |
| 140 | - | |
| 141 | -void | |
| 142 | -String::Reverse() | |
| 143 | -{ | |
| 144 | - if ((_buffer)&&(Length() > 0)) | |
| 145 | - { | |
| 146 | - uint32 from = 0; | |
| 147 | - uint32 to = Length()-1; | |
| 148 | - while(from<to) anubisSwap(_buffer[from++], _buffer[to--]); | |
| 149 | - } | |
| 150 | -} | |
| 151 | - | |
| 152 | -uint32 | |
| 153 | -String::Replace(char findChar, char replaceChar) | |
| 154 | -{ | |
| 155 | - uint32 ret = 0; | |
| 156 | - if ((_buffer)&&(findChar != replaceChar)) | |
| 157 | - { | |
| 158 | - char * c = _buffer; | |
| 159 | - while(*c) | |
| 160 | - { | |
| 161 | - if (*c == findChar) | |
| 162 | - { | |
| 163 | - *c = replaceChar; | |
| 164 | - ret++; | |
| 165 | - } | |
| 166 | - c++; | |
| 167 | - } | |
| 168 | - } | |
| 169 | - return ret; | |
| 170 | -} | |
| 171 | - | |
| 172 | -int32 | |
| 173 | -String::Replace(const String & replaceMe, const String & withMe) | |
| 174 | -{ | |
| 175 | - if (replaceMe.Length() == 0) return -1; // can't replace an empty string, that's silly! | |
| 176 | - if (replaceMe == withMe) return GetNumInstancesOf(replaceMe); // no changes necessary! | |
| 177 | - | |
| 178 | - String temp; | |
| 179 | - int32 perInstanceDelta = ((int32)withMe.Length())-((int32)replaceMe.Length()); | |
| 180 | - if (perInstanceDelta > 0) | |
| 181 | - { | |
| 182 | - // If we are replacing a shorter string with a longer string, we'll have to do a copy-and-swap | |
| 183 | - uint32 numInstances = GetNumInstancesOf(replaceMe); | |
| 184 | - if (numInstances == 0) return 0; // no changes necessary! | |
| 185 | - if (temp.Prealloc(Length()+(perInstanceDelta*numInstances)) != B_NO_ERROR) return -1; | |
| 186 | - } | |
| 187 | - | |
| 188 | - // This code works for both the in-place and the copy-over modes! | |
| 189 | - int32 ret = 0; | |
| 190 | - const char * readPtr = Cstr(); | |
| 191 | - char * writePtr = (perInstanceDelta > 0) ? temp._buffer : NULL; | |
| 192 | - while(1) | |
| 193 | - { | |
| 194 | - char * nextReplaceMe = strstr((char *) readPtr, (char *) replaceMe()); | |
| 195 | - if (nextReplaceMe) | |
| 196 | - { | |
| 197 | - ret++; | |
| 198 | - if (writePtr) | |
| 199 | - { | |
| 200 | - uint32 numBytes = nextReplaceMe-readPtr; | |
| 201 | - if (perInstanceDelta != 0) memmove(writePtr, readPtr, numBytes); | |
| 202 | - writePtr += numBytes; | |
| 203 | - } | |
| 204 | - else writePtr = nextReplaceMe; | |
| 205 | - | |
| 206 | - memcpy(writePtr, withMe(), withMe.Length()); | |
| 207 | - readPtr = nextReplaceMe + replaceMe.Length(); | |
| 208 | - writePtr += withMe.Length(); | |
| 209 | - } | |
| 210 | - else | |
| 211 | - { | |
| 212 | - if (writePtr) | |
| 213 | - { | |
| 214 | - // Finish up | |
| 215 | - uint32 numBytes = Cstr()+Length()-readPtr; | |
| 216 | - if (perInstanceDelta != 0) memmove(writePtr, readPtr, numBytes); | |
| 217 | - writePtr += numBytes; | |
| 218 | - *writePtr = '\0'; | |
| 219 | - if (perInstanceDelta > 0) | |
| 220 | - { | |
| 221 | - temp._length = writePtr-temp(); | |
| 222 | - SwapContents(temp); | |
| 223 | - } | |
| 224 | - else _length = writePtr-Cstr(); | |
| 225 | - } | |
| 226 | - return ret; | |
| 227 | - } | |
| 228 | - } | |
| 229 | - return ret; // just to shut the compiler up; we never actually get here | |
| 230 | -} | |
| 231 | - | |
| 232 | -void | |
| 233 | -String::SwapContents(String & s) | |
| 234 | -{ | |
| 235 | -// TCHECKPOINT; | |
| 236 | - | |
| 237 | - if (&s != this) | |
| 238 | - { | |
| 239 | - bool thisSmall = ( _buffer == _smallBuffer); | |
| 240 | - bool sSmall = (s._buffer == s._smallBuffer); | |
| 241 | - | |
| 242 | - if ((sSmall)&&(thisSmall)) | |
| 243 | - { | |
| 244 | - for (int32 i=anubisMax(_length, s._length); i>=0; i--) anubisSwap(_smallBuffer[i], s._smallBuffer[i]); | |
| 245 | - } | |
| 246 | - else if (thisSmall) | |
| 247 | - { | |
| 248 | - _buffer = s._buffer; | |
| 249 | - _bufferLen = s._bufferLen; | |
| 250 | - s._buffer = s._smallBuffer; | |
| 251 | - s._bufferLen = sizeof(s._smallBuffer); | |
| 252 | - memcpy(s._smallBuffer, _smallBuffer, _length+1); | |
| 253 | - } | |
| 254 | - else if (sSmall) | |
| 255 | - { | |
| 256 | - s._buffer = _buffer; | |
| 257 | - s._bufferLen = _bufferLen; | |
| 258 | - _buffer = _smallBuffer; | |
| 259 | - _bufferLen = sizeof(_smallBuffer); | |
| 260 | - memcpy(_smallBuffer, s._smallBuffer, s._length+1); | |
| 261 | - } | |
| 262 | - else | |
| 263 | - { | |
| 264 | - anubisSwap(_buffer, s._buffer); | |
| 265 | - anubisSwap(_bufferLen, s._bufferLen); | |
| 266 | - } | |
| 267 | - | |
| 268 | - anubisSwap(_length, s._length); // always do this | |
| 269 | - } | |
| 270 | -} | |
| 271 | - | |
| 272 | -int | |
| 273 | -String::LastIndexOf(const String &s2, uint32 fromIndex) const | |
| 274 | -{ | |
| 275 | - if (s2.Length() == 0) return Length()-1; | |
| 276 | - if (fromIndex >= Length()) return -1; | |
| 277 | - for (int i=fromIndex; i>=0; i--) if (strncmp(Cstr()+i, s2.Cstr(), s2.Length()) == 0) return i; | |
| 278 | - return -1; | |
| 279 | -} | |
| 280 | - | |
| 281 | -int | |
| 282 | -String::LastIndexOf(const char * s2, uint32 fromIndex) const | |
| 283 | -{ | |
| 284 | - if (s2 == NULL) s2 = ""; | |
| 285 | - uint32 s2Len = strlen(s2); | |
| 286 | - if (s2Len == 0) return Length()-1; | |
| 287 | - if (fromIndex >= Length()) return -1; | |
| 288 | - for (int i=fromIndex; i>=0; i--) if (strncmp(Cstr()+i, s2, s2Len) == 0) return i; | |
| 289 | - return -1; | |
| 290 | -} | |
| 291 | - | |
| 292 | -String | |
| 293 | -String::ToLowerCase() const | |
| 294 | -{ | |
| 295 | - String ret(*this); | |
| 296 | - if (ret._buffer) for (uint32 i=0; i<ret.Length(); i++) ret._buffer[i] = (char)tolower(ret._buffer[i]); | |
| 297 | - return ret; | |
| 298 | -} | |
| 299 | - | |
| 300 | -String | |
| 301 | -String::ToUpperCase() const | |
| 302 | -{ | |
| 303 | - String ret(*this); | |
| 304 | - if (ret._buffer) for (uint32 i=0; i<ret.Length(); i++) ret._buffer[i] = (char)toupper(ret._buffer[i]); | |
| 305 | - return ret; | |
| 306 | -} | |
| 307 | - | |
| 308 | -String | |
| 309 | -String::Trim() const | |
| 310 | -{ | |
| 311 | - int32 len = (int32) Length(); | |
| 312 | - const char * s = Cstr(); | |
| 313 | - int32 startIdx; for (startIdx = 0; startIdx<len; startIdx++) if (!IsSpaceChar(s[startIdx])) break; | |
| 314 | - int32 endIdx; for (endIdx = len-1; endIdx>startIdx; endIdx--) if (!IsSpaceChar(s[endIdx])) break; | |
| 315 | - return String(*this, (uint32)startIdx, (uint32)(endIdx+1)); | |
| 316 | -} | |
| 317 | - | |
| 318 | -uint32 String :: GetNumInstancesOf(char ch) const | |
| 319 | -{ | |
| 320 | - uint32 ret = 0; | |
| 321 | - for (const char * s = Cstr(); (*s != '\0'); s++) if (*s == ch) ret++; | |
| 322 | - return ret; | |
| 323 | -} | |
| 324 | - | |
| 325 | -uint32 String :: GetNumInstancesOf(const String & substring) const | |
| 326 | -{ | |
| 327 | - uint32 ret = 0; | |
| 328 | - if (substring.Length() > 0) | |
| 329 | - { | |
| 330 | - uint32 lastIdx = 0; | |
| 331 | - int32 idx; | |
| 332 | - while((idx = IndexOf(substring, lastIdx)) >= 0) | |
| 333 | - { | |
| 334 | - ret++; | |
| 335 | - lastIdx = idx + substring.Length(); | |
| 336 | - } | |
| 337 | - } | |
| 338 | - return ret; | |
| 339 | -} | |
| 340 | - | |
| 341 | -uint32 String :: GetNumInstancesOf(const char * substring) const | |
| 342 | -{ | |
| 343 | - if (substring == NULL) substring = ""; | |
| 344 | - uint32 ret = 0; | |
| 345 | - uint32 substringLength = strlen(substring); | |
| 346 | - if (substringLength > 0) | |
| 347 | - { | |
| 348 | - uint32 lastIdx = 0; | |
| 349 | - int32 idx; | |
| 350 | - while((idx = IndexOf(substring, lastIdx)) >= 0) | |
| 351 | - { | |
| 352 | - ret++; | |
| 353 | - lastIdx = idx + substringLength; | |
| 354 | - } | |
| 355 | - } | |
| 356 | - return ret; | |
| 357 | -} | |
| 358 | - | |
| 359 | -String String :: Prepend(const String & str, uint32 count) const | |
| 360 | -{ | |
| 361 | - String ret; | |
| 362 | - uint32 newLen = (count*str.Length())+Length(); | |
| 363 | - if (ret.Prealloc(newLen) == B_NO_ERROR) | |
| 364 | - { | |
| 365 | - char * b = ret._buffer; | |
| 366 | - | |
| 367 | - if (str.Length() > 0) | |
| 368 | - { | |
| 369 | - for (uint32 i=0; i<count; i++) | |
| 370 | - { | |
| 371 | - memcpy(b, str(), str.Length()); | |
| 372 | - b += str.Length(); | |
| 373 | - } | |
| 374 | - } | |
| 375 | - if (Length() > 0) | |
| 376 | - { | |
| 377 | - memcpy(b, Cstr(), Length()); | |
| 378 | - b += Length(); | |
| 379 | - } | |
| 380 | - ret._length = (b-ret._buffer); | |
| 381 | - ret._buffer[ret._length] = '\0'; // terminate the string | |
| 382 | - } | |
| 383 | - return ret; | |
| 384 | -} | |
| 385 | - | |
| 386 | -String String :: Prepend(const char * str, uint32 count) const | |
| 387 | -{ | |
| 388 | - if (anubisInRange(str, (const char *)_buffer, (const char *)(_buffer+Length()))) return Prepend(String(str), count); // avoid self-entanglement! | |
| 389 | - else | |
| 390 | - { | |
| 391 | - if (str == NULL) str = ""; | |
| 392 | - uint32 sLen = strlen(str); | |
| 393 | - String ret; | |
| 394 | - uint32 newLen = (count*sLen)+Length(); | |
| 395 | - if (ret.Prealloc(newLen) == B_NO_ERROR) | |
| 396 | - { | |
| 397 | - char * b = ret._buffer; | |
| 398 | - | |
| 399 | - if (sLen > 0) | |
| 400 | - { | |
| 401 | - for (uint32 i=0; i<count; i++) | |
| 402 | - { | |
| 403 | - memcpy(b, str, sLen); | |
| 404 | - b += sLen; | |
| 405 | - } | |
| 406 | - } | |
| 407 | - if (Length() > 0) | |
| 408 | - { | |
| 409 | - memcpy(b, Cstr(), Length()); | |
| 410 | - b += Length(); | |
| 411 | - } | |
| 412 | - ret._length = (b-ret._buffer); | |
| 413 | - ret._buffer[ret._length] = '\0'; // terminate the string | |
| 414 | - } | |
| 415 | - return ret; | |
| 416 | - } | |
| 417 | -} | |
| 418 | - | |
| 419 | -String String :: Append(const String & str, uint32 count) const | |
| 420 | -{ | |
| 421 | - String ret; | |
| 422 | - uint32 newLen = Length()+(count*str.Length()); | |
| 423 | - if (ret.Prealloc(newLen) == B_NO_ERROR) | |
| 424 | - { | |
| 425 | - char * b = ret._buffer; | |
| 426 | - if (Length() > 0) | |
| 427 | - { | |
| 428 | - memcpy(b, Cstr(), Length()); | |
| 429 | - b += Length(); | |
| 430 | - } | |
| 431 | - if (str.Length() > 0) | |
| 432 | - { | |
| 433 | - for (uint32 i=0; i<count; i++) | |
| 434 | - { | |
| 435 | - memcpy(b, str(), str.Length()); | |
| 436 | - b += str.Length(); | |
| 437 | - } | |
| 438 | - } | |
| 439 | - ret._length = (b-ret._buffer); | |
| 440 | - ret._buffer[ret._length] = '\0'; // terminate the string | |
| 441 | - } | |
| 442 | - return ret; | |
| 443 | -} | |
| 444 | - | |
| 445 | -String String :: Append(const char * str, uint32 count) const | |
| 446 | -{ | |
| 447 | - if (anubisInRange(str, (const char *)_buffer, (const char *)(_buffer+Length()))) return Append(String(str), count); // avoid self-entanglement! | |
| 448 | - else | |
| 449 | - { | |
| 450 | - if (str == NULL) str = ""; | |
| 451 | - uint32 sLen = strlen(str); | |
| 452 | - String ret; | |
| 453 | - uint32 newLen = Length()+(count*sLen); | |
| 454 | - if (ret.Prealloc(newLen) == B_NO_ERROR) | |
| 455 | - { | |
| 456 | - char * b = ret._buffer; | |
| 457 | - if (Length() > 0) | |
| 458 | - { | |
| 459 | - memcpy(b, Cstr(), Length()); | |
| 460 | - b += Length(); | |
| 461 | - } | |
| 462 | - if (sLen > 0) | |
| 463 | - { | |
| 464 | - for (uint32 i=0; i<count; i++) | |
| 465 | - { | |
| 466 | - memcpy(b, str, sLen); | |
| 467 | - b += sLen; | |
| 468 | - } | |
| 469 | - } | |
| 470 | - ret._length = (b-ret._buffer); | |
| 471 | - ret._buffer[ret._length] = '\0'; // terminate the string | |
| 472 | - } | |
| 473 | - return ret; | |
| 474 | - } | |
| 475 | -} | |
| 476 | - | |
| 477 | -// This method tries to ensure that at least (newBufLen) chars | |
| 478 | -// are available for storing data in. (requestedBufLen) should include | |
| 479 | -// the terminating NUL. If (retainValue) is true, the current string value | |
| 480 | -// will be retained; otherwise it should be set right after this call | |
| 481 | -// returns... | |
| 482 | -status_t String::EnsureBufferSize(uint32 requestedBufLen, bool retainValue) | |
| 483 | -{ | |
| 484 | - if (requestedBufLen > _bufferLen) | |
| 485 | - { | |
| 486 | - if (requestedBufLen <= sizeof(_smallBuffer)) // guaranteed only to be true the very first time we are called! | |
| 487 | - { | |
| 488 | - // For small initial requests, we can just set up our buffer pointer to point to our static-buffer area. Cheap! | |
| 489 | - _buffer = _smallBuffer; | |
| 490 | - _bufferLen = sizeof(_smallBuffer); | |
| 491 | - } | |
| 492 | - else | |
| 493 | - { | |
| 494 | - // If we're doing a first-time allocation, allocate exactly the number of the bytes requested. | |
| 495 | - // If it's a re-allocation, allocate more than requested as it's more likely to happen yet another time... | |
| 496 | - uint32 newBufLen = (_buffer == NULL) ? requestedBufLen : (requestedBufLen * 2); | |
| 497 | - if (retainValue) | |
| 498 | - { | |
| 499 | - if ((_buffer)&&(_buffer != _smallBuffer)) | |
| 500 | - { | |
| 501 | - // We can call muscleRealloc() to hopefully avoid data copying | |
| 502 | - char * newBuf = (char *)realloc(_buffer, newBufLen); | |
| 503 | - if (newBuf) | |
| 504 | - { | |
| 505 | - _buffer = newBuf; | |
| 506 | - _bufferLen = newBufLen; | |
| 507 | - } | |
| 508 | - else | |
| 509 | - { | |
| 510 | -// WARN_OUT_OF_MEMORY; | |
| 511 | - return B_ERROR; | |
| 512 | - } | |
| 513 | - } | |
| 514 | - else | |
| 515 | - { | |
| 516 | - // Oops, muscleRealloc() won't do.... we'll just have to copy the bytes over | |
| 517 | - char * newBuf = (char *) malloc(newBufLen); | |
| 518 | - if (newBuf == NULL) | |
| 519 | - { | |
| 520 | -// WARN_OUT_OF_MEMORY; | |
| 521 | - return B_ERROR; | |
| 522 | - } | |
| 523 | - if (_buffer) | |
| 524 | - memcpy(newBuf, _buffer, Length()+1); | |
| 525 | - if (_buffer != _smallBuffer) | |
| 526 | - SAFE_FREE(_buffer); | |
| 527 | - _buffer = newBuf; | |
| 528 | - _bufferLen = newBufLen; | |
| 529 | - } | |
| 530 | - } | |
| 531 | - else | |
| 532 | - { | |
| 533 | - // If the caller doesn't care about retaining the value, then it's | |
| 534 | - // probably cheaper just to free our buffer and get a new one. | |
| 535 | - char * newBuf = (char *) malloc(newBufLen); | |
| 536 | - if (newBuf == NULL) | |
| 537 | - { | |
| 538 | -// WARN_OUT_OF_MEMORY; | |
| 539 | - return B_ERROR; | |
| 540 | - } | |
| 541 | - if (_buffer != _smallBuffer) | |
| 542 | - SAFE_FREE(_buffer); | |
| 543 | - _buffer = newBuf; | |
| 544 | - _bufferLen = newBufLen; | |
| 545 | - } | |
| 546 | - } | |
| 547 | - } | |
| 548 | - return B_NO_ERROR; | |
| 549 | -} | |
| 550 | - | |
| 551 | -String String :: Pad(uint32 minLength, bool padOnRight, char padChar) const | |
| 552 | -{ | |
| 553 | - if (Length() < minLength) | |
| 554 | - { | |
| 555 | - uint32 padLen = minLength-Length(); | |
| 556 | - String temp; temp += padChar; | |
| 557 | - return (padOnRight) ? Append(temp, padLen) : Prepend(temp, padLen); | |
| 558 | - } | |
| 559 | - else return *this; | |
| 560 | -} | |
| 561 | - | |
| 562 | -#define ARG_IMPLEMENTATION \ | |
| 563 | - char buf[256]; \ | |
| 564 | - sprintf(buf, fmt, value); \ | |
| 565 | - return ArgAux(buf) | |
| 566 | - | |
| 567 | -String String :: Arg(int8 value, const char * fmt) const {ARG_IMPLEMENTATION;} | |
| 568 | -String String :: Arg(uint8 value, const char * fmt) const {ARG_IMPLEMENTATION;} | |
| 569 | -String String :: Arg(int16 value, const char * fmt) const {ARG_IMPLEMENTATION;} | |
| 570 | -String String :: Arg(uint16 value, const char * fmt) const {ARG_IMPLEMENTATION;} | |
| 571 | -String String :: Arg(int32 value, const char * fmt) const {ARG_IMPLEMENTATION;} | |
| 572 | -String String :: Arg(uint32 value, const char * fmt) const {ARG_IMPLEMENTATION;} | |
| 573 | -String String :: Arg(int64 value, const char * fmt) const {ARG_IMPLEMENTATION;} | |
| 574 | -String String :: Arg(uint64 value, const char * fmt) const {ARG_IMPLEMENTATION;} | |
| 575 | -String String :: Arg(double value, const char * fmt) const {ARG_IMPLEMENTATION;} | |
| 576 | -String String :: Arg(const String & value) const {return ArgAux(value());} | |
| 577 | -String String :: Arg(const char * value) const {return ArgAux(value);} | |
| 578 | - | |
| 579 | -String String :: ArgAux(const char * buf) const | |
| 580 | -{ | |
| 581 | - int32 lowestArg = -1; | |
| 582 | - const char * s = Cstr(); | |
| 583 | - while(*s != '\0') | |
| 584 | - { | |
| 585 | - if (*s == '%') | |
| 586 | - { | |
| 587 | - s++; | |
| 588 | - if (anubisInRange(*s, '0', '9')) | |
| 589 | - { | |
| 590 | - int32 val = atol(s); | |
| 591 | - lowestArg = (lowestArg < 0) ? val : anubisMin(val, lowestArg); | |
| 592 | - while(anubisInRange(*s, '0', '9')) s++; | |
| 593 | - } | |
| 594 | - } | |
| 595 | - else s++; | |
| 596 | - } | |
| 597 | - | |
| 598 | - if (lowestArg >= 0) | |
| 599 | - { | |
| 600 | - char token[64]; | |
| 601 | - sprintf(token, "%%%li", lowestArg); | |
| 602 | - String ret(*this); | |
| 603 | - (void) ret.Replace(token, buf); | |
| 604 | - return ret; | |
| 605 | - } | |
| 606 | - else return *this; | |
| 607 | -} | |
| 608 | - | |
| 609 | -const String & GetEmptyString() | |
| 610 | -{ | |
| 611 | - static const String _empty; | |
| 612 | - return _empty; | |
| 613 | -} | |
| 614 | - | |
| 615 | -/*--- ElfHash --------------------------------------------------- | |
| 616 | - * The published hash algorithm used in the UNIX ELF format | |
| 617 | - * for object files. Accepts a pointer to a string to be hashed | |
| 618 | - * and returns an unsigned long. | |
| 619 | - * jaf: stolen from: http://www.ddj.com/articles/1996/9604/9604b/9604b.htm?topic=algorithms | |
| 620 | - *-------------------------------------------------------------*/ | |
| 621 | -uint32 CStringHashFunc(const char * n) | |
| 622 | -{ | |
| 623 | - uint32 h = 0, g; | |
| 624 | - const unsigned char * name = (const unsigned char *) n; | |
| 625 | - while (*name) | |
| 626 | - { | |
| 627 | - h = (h << 4) + *name++; | |
| 628 | - if ((g = h & 0xF0000000) != 0) h ^= g >> 24; | |
| 629 | - h &= ~g; | |
| 630 | - } | |
| 631 | - return h; | |
| 632 | -} | |
| 633 | - | |
| 634 | -int CStringCompareFunc(const char * const & s1, const char * const & s2, void *) | |
| 635 | -{ | |
| 636 | - return strcmp(s1, s2); | |
| 637 | -} | |
| 638 | - | |
| 639 | -int StringCompareFunc(const String & s1, const String & s2, void *) | |
| 640 | -{ | |
| 641 | - return anubisCompare(s1, s2); | |
| 642 | -} | |
| 643 | - | |
| 644 | -END_NAMESPACE(CM); |
anubis_dev/vm/src/String.h deleted
| 1 | -/* This file is Copyright 2005 Level Control Systems. See the included LICENSE.txt file for details. */ | |
| 2 | -/* This class was derived from the String class written by Michael Olivero (mike95@mike95.com) */ | |
| 3 | - | |
| 4 | -/* NOTE TO MACOS/X X-CODE USERS: If you are trying to #include <string.h> | |
| 5 | - * and X-Code is "helpfully" pulling in this file instead (because the | |
| 6 | - * OS/X filesystem is case-insensitive), you can get around that problem | |
| 7 | - * by adding "USE_HEADERMAP = NO" to your X-Code target settings. | |
| 8 | - * ref: http://lists.apple.com/archives/xcode-users/2004/Aug/msg00934.html | |
| 9 | - * --Jeremy | |
| 10 | - */ | |
| 11 | - | |
| 12 | -#ifndef MuscleString_h | |
| 13 | -#define MuscleString_h | |
| 14 | - | |
| 15 | -#include <string.h> | |
| 16 | -#include <ctype.h> | |
| 17 | -#include "AnubisSupport.h" | |
| 18 | - | |
| 19 | -BEGIN_NAMESPACE(CM); | |
| 20 | - | |
| 21 | -#ifndef SMALL_MUSCLE_STRING_LENGTH | |
| 22 | -# define SMALL_MUSCLE_STRING_LENGTH 7 // strings shorter than this length can be stored inline, without requiring an extra new[]. | |
| 23 | -#endif | |
| 24 | - | |
| 25 | -/** A nice hashing function for use with (const char *)'s */ | |
| 26 | -uint32 CStringHashFunc(const char * str); | |
| 27 | - | |
| 28 | -/** A character string class. Represents a dynamically resizable NUL-terminated string. */ | |
| 29 | -class String | |
| 30 | -{ | |
| 31 | -public: | |
| 32 | - /** Constructor. | |
| 33 | - * @param str If non-NULL, the initial value for this String. | |
| 34 | - * @param maxLen The maximum number of characters to place into | |
| 35 | - * this String (not including the NUL terminator byte). | |
| 36 | - * Default is unlimited (i.e. scan the entire string no matter how long it is) | |
| 37 | - */ | |
| 38 | - String(const char * str = NULL, uint32 maxLen = ((uint32)-1)) : _buffer(NULL), _bufferLen(0), _length(0) {if (str) (void) SetCstr(str, maxLen);} | |
| 39 | - | |
| 40 | - /** Copy Constructor. | |
| 41 | - * @param str String to become a copy of. | |
| 42 | - */ | |
| 43 | - String(const String & str) : _buffer(NULL), _bufferLen(0), _length(0) {(void) SetFromString(str);} | |
| 44 | - | |
| 45 | - /** This constructor sets this String to be a substring of the specified String. | |
| 46 | - * @param str String to become a copy of. | |
| 47 | - * @param beginIndex Index of the first character in (str) to include. | |
| 48 | - * @param endIndex Index after the last character in (str) to include. | |
| 49 | - * Defaults to a very large number, so that by default the entire remainder of the string is included. | |
| 50 | - */ | |
| 51 | - String(const String & str, uint32 beginIndex, uint32 endIndex=((uint32)-1)) : _buffer(NULL), _bufferLen(0), _length(0) {(void) SetFromString(str, beginIndex, endIndex);} | |
| 52 | - | |
| 53 | - /** Destructor. */ | |
| 54 | - virtual ~String(); | |
| 55 | - | |
| 56 | - /** Assignment Operator. Sets this string to be a one-character string | |
| 57 | - * @param val The character that should be the sole character in this String. | |
| 58 | - */ | |
| 59 | - String & operator = (char val) {(void) SetCstr(&val, 1); return *this;} | |
| 60 | - | |
| 61 | - /** Assignment Operator. | |
| 62 | - * @param val Pointer to the C-style string to copy from. If NULL, this string will become "". | |
| 63 | - */ | |
| 64 | - String & operator = (const char * val) {(void) SetCstr(val); return *this;} | |
| 65 | - | |
| 66 | - /** Assignment Operator. | |
| 67 | - * @param rhs String to become a copy of. | |
| 68 | - */ | |
| 69 | - String & operator = (const String &rhs) {(void) SetFromString(rhs); return *this;} | |
| 70 | - | |
| 71 | - /** Append Operator. | |
| 72 | - * @param rhs A string to append to this string. | |
| 73 | - */ | |
| 74 | - String & operator += (const String &rhs); | |
| 75 | - | |
| 76 | - /** Append Operator. | |
| 77 | - * @param rhs A string to append to this string. If NULL, this operation is a no-op. | |
| 78 | - */ | |
| 79 | - String & operator += (const char * rhs); | |
| 80 | - | |
| 81 | - /** Append Operator. | |
| 82 | - * @param ch A character to append to this string. | |
| 83 | - */ | |
| 84 | - String & operator += (const char ch) | |
| 85 | - { | |
| 86 | - if (EnsureBufferSize(Length()+2, true) == B_NO_ERROR) | |
| 87 | - { | |
| 88 | - _buffer[_length++] = ch; | |
| 89 | - _buffer[_length] = '\0'; | |
| 90 | - } | |
| 91 | - return *this; | |
| 92 | - } | |
| 93 | - | |
| 94 | - /** Remove Operator. | |
| 95 | - * @param rhs A substring to remove from this string; the | |
| 96 | - * last instance of the substring will be cut out. | |
| 97 | - * If (rhs) is not found, there is no effect. | |
| 98 | - */ | |
| 99 | - String & operator -= (const String &rhs); | |
| 100 | - | |
| 101 | - /** Remove Operator. | |
| 102 | - * @param ch A character to remove from this string; the last | |
| 103 | - * instance of this char will be cut out. If (ch) is | |
| 104 | - * not found, there is no effect. | |
| 105 | - */ | |
| 106 | - String & operator -= (const char ch); | |
| 107 | - | |
| 108 | - /** Append 'Stream' Operator. | |
| 109 | - * @param rhs A String to append to this string. | |
| 110 | - * @return a non const String refrence to 'this' so you can chain appends. | |
| 111 | - */ | |
| 112 | - String & operator << (const String& rhs) {return (*this += rhs);} | |
| 113 | - | |
| 114 | - /** Append 'Stream' Operator. | |
| 115 | - * @param rhs A const char* to append to this string. | |
| 116 | - * @return a non const String refrence to 'this' so you can chain appends. | |
| 117 | - */ | |
| 118 | - String & operator << (const char* rhs) {return (*this += rhs);} | |
| 119 | - | |
| 120 | - /** Append 'Stream' Operator. | |
| 121 | - * @param rhs An int to append to this string. | |
| 122 | - * @return a non const String refrence to 'this' so you can chain appends. | |
| 123 | - */ | |
| 124 | - String & operator << (int rhs); | |
| 125 | - | |
| 126 | - /** Append 'Stream' Operator. | |
| 127 | - * @param rhs A float to append to this string. Formatting is set at 2 decimals of precision. | |
| 128 | - * @return a non const String refrence to 'this' so you can chain appends. | |
| 129 | - */ | |
| 130 | - String & operator << (float rhs); | |
| 131 | - | |
| 132 | - /** Append 'Stream' Operator. | |
| 133 | - * @param rhs A bool to append to this string. Converts to 'true' and 'false' strings appropriately. | |
| 134 | - * @return a non const String refrence to 'this' so you can chain appends. | |
| 135 | - */ | |
| 136 | - String & operator << (bool rhs); | |
| 137 | - | |
| 138 | - /** Comparison Operator. Returns true if the two strings are equal (as determined by strcmp()) | |
| 139 | - * @param rhs A string to compare ourself with | |
| 140 | - */ | |
| 141 | - bool operator == (const String &rhs) const {return ((this == &rhs)||((Length() == rhs.Length())&&(strcmp(Cstr(), rhs.Cstr()) == 0)));} | |
| 142 | - | |
| 143 | - /** Comparison Operator. Returns true if the two strings are equal (as determined by strcmp()) | |
| 144 | - * @param rhs Pointer to a C string to compare with. NULL pointers are considered a synonym for "". | |
| 145 | - */ | |
| 146 | - bool operator == (const char * rhs) const {return (strcmp(Cstr(), rhs?rhs:"") == 0);} | |
| 147 | - | |
| 148 | - /** Comparison Operator. Returns true if the two strings are not equal (as determined by strcmp()) | |
| 149 | - * @param rhs A string to compare ourself with | |
| 150 | - */ | |
| 151 | - bool operator != (const String &rhs) const {return !(*this == rhs);} | |
| 152 | - | |
| 153 | - /** Comparison Operator. Returns true if the two strings are not equal (as determined by strcmp()) | |
| 154 | - * @param rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 155 | - */ | |
| 156 | - bool operator != (const char * rhs) const {return (strcmp(Cstr(), rhs?rhs:"") != 0);} | |
| 157 | - | |
| 158 | - /** Comparison Operator. Returns true if this string comes before (rhs) lexically. | |
| 159 | - * @param rhs A string to compare ourself with | |
| 160 | - */ | |
| 161 | - bool operator < (const String &rhs) const {return (this == &rhs) ? false : (strcmp(Cstr(), rhs.Cstr()) < 0);} | |
| 162 | - | |
| 163 | - /** Comparison Operator. Returns true if this string comes before (rhs) lexically. | |
| 164 | - * @param rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 165 | - */ | |
| 166 | - bool operator < (const char * rhs) const {return (strcmp(Cstr(), rhs?rhs:"") < 0);} | |
| 167 | - | |
| 168 | - /** Comparison Operator. Returns true if this string comes after (rhs) lexically. | |
| 169 | - * @param rhs A string to compare ourself with | |
| 170 | - */ | |
| 171 | - bool operator > (const String &rhs) const {return (this == &rhs) ? false : (strcmp(Cstr(), rhs.Cstr()) > 0);} | |
| 172 | - | |
| 173 | - /** Comparison Operator. Returns true if this string comes after (rhs) lexically. | |
| 174 | - * @param rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 175 | - */ | |
| 176 | - bool operator > (const char * rhs) const {return (strcmp(Cstr(), rhs?rhs:"") > 0);} | |
| 177 | - | |
| 178 | - /** Comparison Operator. Returns true if the two strings are equal, or this string comes before (rhs) lexically. | |
| 179 | - * @param rhs A string to compare ourself with | |
| 180 | - */ | |
| 181 | - bool operator <= (const String &rhs) const {return (this == &rhs) ? true : (strcmp(Cstr(), rhs.Cstr()) <= 0);} | |
| 182 | - | |
| 183 | - /** Comparison Operator. Returns true if the two strings are equal, or this string comes before (rhs) lexically. | |
| 184 | - * @param rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 185 | - */ | |
| 186 | - bool operator <= (const char * rhs) const {return (strcmp(Cstr(), rhs?rhs:"") <= 0);} | |
| 187 | - | |
| 188 | - /** Comparison Operator. Returns true if the two strings are equal, or this string comes after (rhs) lexically. | |
| 189 | - * @param rhs A string to compare ourself with | |
| 190 | - */ | |
| 191 | - bool operator >= (const String &rhs) const {return (this == &rhs) ? true : (strcmp(Cstr(), rhs.Cstr()) >= 0);} | |
| 192 | - | |
| 193 | - /** Comparison Operator. Returns true if the two strings are equal, or this string comes after (rhs) lexically. | |
| 194 | - * @param rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 195 | - */ | |
| 196 | - bool operator >= (const char * rhs) const {return (strcmp(Cstr(), rhs?rhs:"") >= 0);} | |
| 197 | - | |
| 198 | - /** Array Operator. Used to get easy access to the characters that make up this string. | |
| 199 | - * @param index Index of the character to return. Be sure to only use valid indices! | |
| 200 | - */ | |
| 201 | - char operator [] (uint32 index) const {VerifyIndex(index); return _buffer[index];} | |
| 202 | - | |
| 203 | - /** Array Operator. Used to get easy access to the characters that make up this string. | |
| 204 | - * @param index Index of the character to set. Be sure to only use valid indices! | |
| 205 | - */ | |
| 206 | - char & operator [] (uint32 index) {VerifyIndex(index); return _buffer[index];} | |
| 207 | - | |
| 208 | - /** Returns the character at the (index)'th position in the string. | |
| 209 | - * @param index A value between 0 and (Length()-1), inclusive. | |
| 210 | - * @return A character value. | |
| 211 | - */ | |
| 212 | - char CharAt(uint32 index) const {return operator[](index);} | |
| 213 | - | |
| 214 | - /** Compares this string to another string using strcmp() | |
| 215 | - * @param rhs A string to compare ourself with | |
| 216 | - */ | |
| 217 | - int CompareTo(const String & rhs) const {return strcmp(Cstr(), rhs.Cstr());} | |
| 218 | - | |
| 219 | - /** Compares this string to a C string using strcmp() | |
| 220 | - * @param rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 221 | - */ | |
| 222 | - int CompareTo(const char * rhs) const {return strcmp(Cstr(), rhs?rhs:"");} | |
| 223 | - | |
| 224 | - /** Returns a read-only C-style pointer to our held character string. */ | |
| 225 | - const char * Cstr() const {return _buffer ? _buffer : "";} | |
| 226 | - | |
| 227 | - /** Convenience synonym for Cstr(). */ | |
| 228 | - const char * operator()() const {return Cstr();} | |
| 229 | - | |
| 230 | - /** Clears this string so that it contains no characters. Equivalent to setting it to "". */ | |
| 231 | - void Clear() {if (_buffer) _buffer[0] = '\0'; _length = 0;} | |
| 232 | - | |
| 233 | - /** Sets our state from the given C-style string. | |
| 234 | - * @param str The new string to copy from. If maxLen is negative, this may be NULL. | |
| 235 | - * @param maxLen If set, the number of characters to copy (not including the NUL | |
| 236 | - * terminator byte). By default, the number of characters is determined | |
| 237 | - * automatically by scanning the string. | |
| 238 | - */ | |
| 239 | - status_t SetCstr(const char * str, uint32 maxLen = ((uint32)-1)); | |
| 240 | - | |
| 241 | - /** Sets our state from the given String. This is similar to the copy constructor, except | |
| 242 | - * that it allows you to optionally specify a maximum length, and it allows you to detect | |
| 243 | - * out-of-memory errors. | |
| 244 | - * @param str The new string to copy from. | |
| 245 | - * @param beginIndex Index of the first character in (str) to include. | |
| 246 | - * @param endIndex Index after the last character in (str) to include. | |
| 247 | - * Defaults to a very large number, so that by default the entire remainder of the string is included. | |
| 248 | - * @returns B_NO_ERROR on success, or B_ERROR on failure (out of memory?) | |
| 249 | - */ | |
| 250 | - status_t SetFromString(const String & str, uint32 firstChar = 0, uint32 maxLen = ((uint32)-1)); | |
| 251 | - | |
| 252 | - /** Returns true iff this string starts with (prefix) | |
| 253 | - * @param c a character to check for at the end of this String. | |
| 254 | - */ | |
| 255 | - bool EndsWith(char c) const {return (_length > 0)&&(_buffer[_length-1] == c);} | |
| 256 | - | |
| 257 | - /** Returns true iff this string ends with (suffix) | |
| 258 | - * @param suffix a String to check for at the end of this String. | |
| 259 | - */ | |
| 260 | - bool EndsWith(const String &suffix) const {return (Length() < suffix.Length()) ? false : (strcmp(Cstr()+(Length()-suffix.Length()), suffix.Cstr()) == 0);} | |
| 261 | - | |
| 262 | - /** Returns true iff this string ends with (suffix) | |
| 263 | - * @param suffix a String to check for at the end of this String. NULL pointers are treated as a synonym for "". | |
| 264 | - */ | |
| 265 | - bool EndsWith(const char * suffix) const | |
| 266 | - { | |
| 267 | - if (suffix == NULL) suffix = ""; | |
| 268 | - uint32 suffixLen = strlen(suffix); | |
| 269 | - return (Length() < suffixLen) ? false : (strcmp(Cstr()+(Length()-suffixLen), suffix) == 0); | |
| 270 | - } | |
| 271 | - | |
| 272 | - /** Returns true iff this string is equal to (string), as determined by strcmp(). | |
| 273 | - * @param str a String to compare this String with. | |
| 274 | - */ | |
| 275 | - bool Equals(const String & str) const {return (*this == str);} | |
| 276 | - | |
| 277 | - /** Returns true iff this string is equal to (str), as determined by strcmp(). | |
| 278 | - * @param str Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 279 | - */ | |
| 280 | - bool Equals(const char * str) const {return (*this == str);} | |
| 281 | - | |
| 282 | - /** Returns true iff this string contains a single character (c). | |
| 283 | - * @param c a character to compare this String with. | |
| 284 | - */ | |
| 285 | - bool Equals(char c) const {return (_length == 1)&&(_buffer[0] == c);} | |
| 286 | - | |
| 287 | - /** Returns the first index of (ch) in this string starting at or after (fromIndex), or -1 if not found. | |
| 288 | - * @param ch A character to look for in this string. | |
| 289 | - * @param fromIndex Index of the first character to start searching at in this String. Defaults to zero (i.e. start from the first character) | |
| 290 | - */ | |
| 291 | - int IndexOf(char ch, uint32 fromIndex = 0) const | |
| 292 | - { | |
| 293 | - const char * temp = (fromIndex < Length()) ? strchr(Cstr()+fromIndex, ch) : NULL; | |
| 294 | - return temp ? (temp - Cstr()) : -1; | |
| 295 | - } | |
| 296 | - | |
| 297 | - /** Returns the first index of substring (str) in this string starting at or after (fromIndex), or -1 if not found. | |
| 298 | - * @param str A String to look for in this string. | |
| 299 | - * @param fromIndex Index of the first character to start searching at in this String. Defaults to zero (i.e. start from the first character) | |
| 300 | - */ | |
| 301 | - int IndexOf(const String &str, uint32 fromIndex = 0) const | |
| 302 | - { | |
| 303 | - const char * temp = (fromIndex < Length()) ? strstr(Cstr()+fromIndex, str()) : NULL; | |
| 304 | - return temp ? (temp - Cstr()) : -1; | |
| 305 | - } | |
| 306 | - | |
| 307 | - /** Returns the first index of substring (str) in this string starting at or after (fromIndex), or -1 if not found. | |
| 308 | - * @param str Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 309 | - * @param fromIndex Index of the first character to start searching at in this String. Defaults to zero (i.e. start from the first character) | |
| 310 | - */ | |
| 311 | - int IndexOf(const char * str, uint32 fromIndex = 0) const | |
| 312 | - { | |
| 313 | - const char * temp = (fromIndex < Length()) ? strstr(Cstr()+fromIndex, str?str:"") : NULL; | |
| 314 | - return temp ? (temp - Cstr()) : -1; | |
| 315 | - } | |
| 316 | - | |
| 317 | - /** Returns the last index of (ch) in this string starting at or after (fromIndex), or -1 if not found. | |
| 318 | - * @param ch A character to look for in this string. | |
| 319 | - * @param fromIndex Index of the first character to start searching at in this String. Defaults to zero (i.e. start from the first character) | |
| 320 | - */ | |
| 321 | - int LastIndexOf(char ch, uint32 fromIndex = 0) const | |
| 322 | - { | |
| 323 | - const char * lio = (fromIndex < Length()) ? strrchr(Cstr()+fromIndex, ch) : NULL; | |
| 324 | - return lio ? (lio - Cstr()) : -1; | |
| 325 | - } | |
| 326 | - | |
| 327 | - /** Returns the last index of substring (str) in this string | |
| 328 | - * @param str A String to look for in this string. | |
| 329 | - * @param fromIndex Index of the first character to start searching at in this String. Defaults to zero (i.e. start from the first character) | |
| 330 | - */ | |
| 331 | - int LastIndexOf(const String &str) const {return (str.Length() <= Length()) ? LastIndexOf(str, Length()-str.Length()) : -1;} | |
| 332 | - | |
| 333 | - /** Returns the last index of substring (str) in this string | |
| 334 | - * @param str Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 335 | - */ | |
| 336 | - int LastIndexOf(const char * str) const | |
| 337 | - { | |
| 338 | - if (str == NULL) str = ""; | |
| 339 | - uint32 strLen = strlen(str); | |
| 340 | - return (strLen <= Length()) ? LastIndexOf(str, Length()-strLen) : -1; | |
| 341 | - } | |
| 342 | - | |
| 343 | - /** Returns the last index of substring (str) in this string starting at or after (fromIndex), or -1 if not found. | |
| 344 | - * @param str A String to look for in this string. | |
| 345 | - * @param fromIndex Index of the first character to start searching at in this String. Defaults to zero (i.e. start from the first character) | |
| 346 | - */ | |
| 347 | - int LastIndexOf(const String &str, uint32 fromIndex) const; | |
| 348 | - | |
| 349 | - /** Returns the last index of substring (str) in this string starting at or after (fromIndex), or -1 if not found. | |
| 350 | - * @param str Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 351 | - * @param fromIndex Index of the first character to start searching at in this String. Defaults to zero (i.e. start from the first character) | |
| 352 | - */ | |
| 353 | - int LastIndexOf(const char * str, uint32 fromIndex) const; | |
| 354 | - | |
| 355 | - /** Returns the number of characters in the string (not including the terminating NUL byte) */ | |
| 356 | - uint32 Length() const {return _length;} | |
| 357 | - | |
| 358 | - /** Returns the number of instances of (c) in this string. */ | |
| 359 | - uint32 GetNumInstancesOf(char ch) const; | |
| 360 | - | |
| 361 | - /** Returns the number of instances of (substring) in this string. */ | |
| 362 | - uint32 GetNumInstancesOf(const String & substring) const; | |
| 363 | - | |
| 364 | - /** Returns the number of instances of (substring) in this string. */ | |
| 365 | - uint32 GetNumInstancesOf(const char * substring) const; | |
| 366 | - | |
| 367 | - /** Returns true iff this string starts with (prefix) */ | |
| 368 | - bool StartsWith(char c) const {return (_length > 0)&&(_buffer[0] == c);} | |
| 369 | - | |
| 370 | - /** Returns true iff this string starts with (prefix) */ | |
| 371 | - bool StartsWith(const String &str) const {return ((Length() >= str.Length())&&(strncmp(Cstr(), str(), str.Length()) == 0));} | |
| 372 | - | |
| 373 | - /** Returns true iff this string starts with (prefix) | |
| 374 | - * @param str Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 375 | - */ | |
| 376 | - bool StartsWith(const char * prefix) const | |
| 377 | - { | |
| 378 | - if (prefix == NULL) prefix = ""; | |
| 379 | - uint32 prefixLen = strlen(prefix); | |
| 380 | - return (Length() < prefixLen) ? false : (strncmp(Cstr(), prefix, prefixLen) == 0); | |
| 381 | - } | |
| 382 | - | |
| 383 | - /** Returns true iff this string starts with the first (offset) characters of (prefix) */ | |
| 384 | - bool StartsWith(const String &prefix, uint32 offset) const {return ((offset+prefix.Length()<=Length())&&(strncmp(Cstr()+offset, prefix.Cstr(), prefix.Length()) == 0));} | |
| 385 | - | |
| 386 | - /** Returns true iff this string starts with the first (offset) characters of (prefix) | |
| 387 | - * @param str Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 388 | - */ | |
| 389 | - bool StartsWith(const char * prefix, uint32 offset) const | |
| 390 | - { | |
| 391 | - if (prefix == NULL) prefix = ""; | |
| 392 | - uint32 prefixLen = strlen(prefix); | |
| 393 | - return ((offset+prefixLen<=Length())&&(strncmp(Cstr()+offset, prefix, prefixLen) == 0)); | |
| 394 | - } | |
| 395 | - | |
| 396 | - /** Returns a string that consists of (count) copies of (str), followed by this string. */ | |
| 397 | - String Prepend(const String & str, uint32 count = 1) const; | |
| 398 | - | |
| 399 | - /** Returns a string that consists of (count) copies of (str), followed by this string. | |
| 400 | - * @param str Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 401 | - * @param count How many instances of (str) should be prepended to this string. Defaults to 1. | |
| 402 | - */ | |
| 403 | - String Prepend(const char * str, uint32 count = 1) const; | |
| 404 | - | |
| 405 | - /** Returns a string that consists of this string followed by (count) copies of (str). */ | |
| 406 | - String Append(const String & str, uint32 count = 1) const; | |
| 407 | - | |
| 408 | - /** Returns a string that consists of this string followed by (count) copies of (str). | |
| 409 | - * @param str Pointer to a C string to compare to. NULL pointers are considered a synonym for "". | |
| 410 | - * @param count How many instances of (str) should be appended to this string. Defaults to 1. | |
| 411 | - */ | |
| 412 | - String Append(const char * str, uint32 count = 1) const; | |
| 413 | - | |
| 414 | - /** Returns a string that is like this string, but padded out to the specified minimum length with (padChar). | |
| 415 | - * @param minLength Minimum length that the returned string should be. | |
| 416 | - * @param padOnRight If true, (padChar)s will be added to the right; if false (the default), they will be added on the left. | |
| 417 | - * @param padChar The character to pad out the string with. Defaults to ' '. | |
| 418 | - * @returns the new, padded String. | |
| 419 | - */ | |
| 420 | - String Pad(uint32 minLength, bool padOnRight = false, char padChar = ' ') const; | |
| 421 | - | |
| 422 | - /** Returns a string that consists of only the last part of this string, starting with index (beginIndex). Does not modify the string it is called on. */ | |
| 423 | - String Substring(uint32 beginIndex) const {return String(*this, beginIndex);} | |
| 424 | - | |
| 425 | - /** Returns a string that consists of only the characters in this string from range (beginIndex) to (endIndex-1). Does not modify the string it is called on. */ | |
| 426 | - String Substring(uint32 beginIndex, uint32 endIndex) const {return String(*this, beginIndex, endIndex);} | |
| 427 | - | |
| 428 | - /** Returns a string that consists of only the last part of this string, starting with the first character after the last instance of (markerString). | |
| 429 | - * If (markerString) is not found in the string, then this entire String is returned. | |
| 430 | - * For example, String("this is a test").Substring("is a") returns " test". | |
| 431 | - * Does not modify the string it is called on. | |
| 432 | - */ | |
| 433 | - String Substring(const String & markerString) const | |
| 434 | - { | |
| 435 | - int idx = LastIndexOf(markerString); | |
| 436 | - return (idx >= 0) ? String(*this, idx+markerString.Length()) : *this; | |
| 437 | - } | |
| 438 | - | |
| 439 | - /** See the Substring(const String &) documentation for details. */ | |
| 440 | - String Substring(const char * markerString) const | |
| 441 | - { | |
| 442 | - int idx = LastIndexOf(markerString); | |
| 443 | - return (idx >= 0) ? String(*this, idx+strlen(markerString)) : *this; // if (idx >= 0), then we know markerString is non-NULL | |
| 444 | - } | |
| 445 | - | |
| 446 | - /** Returns a string that consists of only the characters in the string from range (beginIndex) until the character just before | |
| 447 | - * the first character in (markerString). If (markerString) is not found, then the entire substring starting at (beginIndex) is returned. | |
| 448 | - * For example, String("this is a test").Substring(1, "is a") returns "his ". | |
| 449 | - * Does not modify the string it is called on. | |
| 450 | - */ | |
| 451 | - String Substring(uint32 beginIndex, const String & markerString) const {return String(*this, beginIndex, (uint32) IndexOf(markerString, beginIndex));} | |
| 452 | - | |
| 453 | - /** See the Substring(uint32, const String &) documentation for details. */ | |
| 454 | - String Substring(uint32 beginIndex, const char * markerString) const {return String(*this, beginIndex, (uint32) IndexOf(markerString, beginIndex));} | |
| 455 | - | |
| 456 | - /** Returns an all lower-case version of this string. Does not modify the string it is called on. */ | |
| 457 | - String ToLowerCase() const; | |
| 458 | - | |
| 459 | - /** Returns an all upper-case version of this string. Does not modify the string it is called on. */ | |
| 460 | - String ToUpperCase() const; | |
| 461 | - | |
| 462 | - /** Returns an version of this string that has all leading and trailing whitespace removed. Does not modify the string it is called on. */ | |
| 463 | - String Trim() const; | |
| 464 | - | |
| 465 | - /** Swaps the state of this string with (swapWithMe). Very efficient since little or no data copying is required. */ | |
| 466 | - void SwapContents(String & swapWithMe); | |
| 467 | - | |
| 468 | - /** Like CompareTo(), but case insensitive. */ | |
| 469 | - int CompareToIgnoreCase(const String &s) const {return ToLowerCase().CompareTo(s.ToLowerCase());} | |
| 470 | - | |
| 471 | - /** Like EndsWith(), but case insensitive. */ | |
| 472 | - bool EndsWithIgnoreCase(char c) const {return (_length > 0)&&(tolower(_buffer[_length-1]) == tolower(c));} | |
| 473 | - | |
| 474 | - /** Like EndsWith(), but case insensitive. */ | |
| 475 | - bool EndsWithIgnoreCase(const String &s) const {return ToLowerCase().EndsWith(s.ToLowerCase());} | |
| 476 | - | |
| 477 | - /** Like Equals(), but case insensitive. */ | |
| 478 | - bool EqualsIgnoreCase(const String &s) const {return ToLowerCase().Equals(s.ToLowerCase());} | |
| 479 | - | |
| 480 | - /** Like Equals(), but case insensitive. */ | |
| 481 | - bool EqualsIgnoreCase(char c) const {return (_length==1)&&(tolower(_buffer[0])==tolower(c));} | |
| 482 | - | |
| 483 | - /** Like IndexOf(), but case insensitive. */ | |
| 484 | - int IndexOfIgnoreCase(const String &s) const {return ToLowerCase().IndexOf(s.ToLowerCase());} | |
| 485 | - | |
| 486 | - /** Like IndexOf(), but case insensitive. */ | |
| 487 | - int IndexOfIgnoreCase(const String &s, uint32 f) const {return ToLowerCase().IndexOf(s.ToLowerCase(),f);} | |
| 488 | - | |
| 489 | - /** Like IndexOf(), but case insensitive. */ | |
| 490 | - int IndexOfIgnoreCase(char ch) const {return ToLowerCase().IndexOf((char)tolower(ch));} | |
| 491 | - | |
| 492 | - /** Like IndexOf(), but case insensitive. */ | |
| 493 | - int IndexOfIgnoreCase(char ch, uint32 f) const {return ToLowerCase().IndexOf((char)tolower(ch),f);} | |
| 494 | - | |
| 495 | - /** Like LastIndexOf(), but case insensitive. */ | |
| 496 | - int LastIndexOfIgnoreCase(const String &s) const {return ToLowerCase().LastIndexOf(s.ToLowerCase());} | |
| 497 | - | |
| 498 | - /** Like LastIndexOf(), but case insensitive. */ | |
| 499 | - int LastIndexOfIgnoreCase(const String &s, uint32 f) const {return ToLowerCase().LastIndexOf(s.ToLowerCase(),f);} | |
| 500 | - | |
| 501 | - /** Like LastIndexOf(), but case insensitive. */ | |
| 502 | - int LastIndexOfIgnoreCase(char ch) const {return ToLowerCase().LastIndexOf((char)tolower(ch));} | |
| 503 | - | |
| 504 | - /** Like LastIndexOf(), but case insensitive. */ | |
| 505 | - int LastIndexOfIgnoreCase(char ch, uint32 f) const {return ToLowerCase().LastIndexOf((char)tolower(ch),f);} | |
| 506 | - | |
| 507 | - /** Like EndsWith(), but case insensitive. */ | |
| 508 | - bool StartsWithIgnoreCase(char c) const {return (_length > 0)&&(tolower(_buffer[0]) == tolower(c));} | |
| 509 | - | |
| 510 | - /** Like StartsWith(), but case insensitive. */ | |
| 511 | - bool StartsWithIgnoreCase(const String &s) const {return ToLowerCase().StartsWith(s.ToLowerCase());} | |
| 512 | - | |
| 513 | - /** Like StartsWith(), but case insensitive. */ | |
| 514 | - bool StartsWithIgnoreCase(const String &s, uint32 o) const {return ToLowerCase().StartsWith(s.ToLowerCase(),o);} | |
| 515 | - | |
| 516 | - /** Returns a hash code for this string */ | |
| 517 | - inline uint32 HashCode() const {return CStringHashFunc(Cstr());} | |
| 518 | - | |
| 519 | - /** Replaces all instances of (oldChar) in this string with (newChar). | |
| 520 | - * @param replaceMe The character to search for. | |
| 521 | - * @param withMe The character to replace all occurrences of (replaceMe) with. | |
| 522 | - * @returns The number of characters that were successfully replaced. | |
| 523 | - */ | |
| 524 | - uint32 Replace(char replaceMe, char withMe); | |
| 525 | - | |
| 526 | - /** Replaces all instances of (replaceMe) in this string with (withMe). | |
| 527 | - * @param replaceMe The substring to search for. | |
| 528 | - * @param withMe The substring to replace all occurrences of (replaceMe) with. | |
| 529 | - * @returns The number of substrings that were successfully replaced, or -1 | |
| 530 | - * if the operation failed (out of memory, or (replaceMe) was of length zero) | |
| 531 | - */ | |
| 532 | - int32 Replace(const String & replaceMe, const String & withMe); | |
| 533 | - | |
| 534 | - /** Reverses the order of all characters in the string, so that e.g. "string" becomes "gnirts" */ | |
| 535 | - void Reverse(); | |
| 536 | - | |
| 537 | - /** Part of the Flattenable interface. | |
| 538 | - * @return false | |
| 539 | - */ | |
| 540 | - virtual bool IsFixedSize() const {return false;} | |
| 541 | - | |
| 542 | - /** Part of the Flattenable interface. | |
| 543 | - * @return B_STRING_TYPE | |
| 544 | - */ | |
| 545 | - status_t Prealloc(uint32 numChars) {return EnsureBufferSize(numChars+1, true);} | |
| 546 | - | |
| 547 | - /** Returns a string like this string, but with the appropriate %# tokens | |
| 548 | - * replaced with a textual representation of the values passed in as (value). | |
| 549 | - * For example, String("%1 is a %2").Arg(13).Arg("bakers dozen") would return | |
| 550 | - * the string "13 is a bakers dozen". | |
| 551 | - * @param value The value to replace in the string. | |
| 552 | - * @param fmt The format parameter to pass to sprintf(). Note that if you supply your | |
| 553 | - * own format string, you'll need to be careful since a badly chosen format | |
| 554 | - * string could lead to a crash or out-of-bounds memory overwrite. In particular, | |
| 555 | - * the format string should match the type specified, and should not lead to | |
| 556 | - * more than 256 bytes being written. | |
| 557 | - * @returns a new String with the appropriate tokens replaced. | |
| 558 | - */ | |
| 559 | - String Arg(int8 value, const char * fmt = "%i") const; | |
| 560 | - | |
| 561 | - /** As above, but for uint8 values. */ | |
| 562 | - String Arg(uint8 value, const char * fmt = "%u") const; | |
| 563 | - | |
| 564 | - /** As above, but for int16 values. */ | |
| 565 | - String Arg(int16 value, const char * fmt = "%i") const; | |
| 566 | - | |
| 567 | - /** As above, but for uint16 values. */ | |
| 568 | - String Arg(uint16 value, const char * fmt = "%u") const; | |
| 569 | - | |
| 570 | - /** As above, but for int32 values. */ | |
| 571 | - String Arg(int32 value, const char * fmt = "%li") const; | |
| 572 | - | |
| 573 | - /** As above, but for uint32 values. */ | |
| 574 | - String Arg(uint32 value, const char * fmt = "%lu") const; | |
| 575 | - | |
| 576 | - /** As above, but for int64 values. */ | |
| 577 | - String Arg(int64 value, const char * fmt = INT64_FORMAT_SPEC) const; | |
| 578 | - | |
| 579 | - /** As above, but for uint64 values. */ | |
| 580 | - String Arg(uint64 value, const char * fmt = UINT64_FORMAT_SPEC) const; | |
| 581 | - | |
| 582 | - /** As above, but for double values. */ | |
| 583 | - String Arg(double value, const char * fmt = "%f") const; | |
| 584 | - | |
| 585 | - /** As above, but for string values. */ | |
| 586 | - String Arg(const String & value) const; | |
| 587 | - String Arg(const char * value) const; | |
| 588 | - | |
| 589 | -private: | |
| 590 | - bool IsSpaceChar(char c) const {return ((c==' ')||(c=='\t')||(c=='\r')||(c=='\n'));} | |
| 591 | - status_t EnsureBufferSize(uint32 newBufLen, bool retainValue); | |
| 592 | - String ArgAux(const char * buf) const; | |
| 593 | - | |
| 594 | - char _smallBuffer[SMALL_MUSCLE_STRING_LENGTH+1]; // store very small strings in-line, to avoid dynamic allocation | |
| 595 | - char * _buffer; // Stores the chars. May point at (_smallBuffer), or a dynamically allocated buffer | |
| 596 | - uint32 _bufferLen; // Number of bytes pointed to by (_buffer) | |
| 597 | - uint32 _length; // cached strlen(_buffer) | |
| 598 | - | |
| 599 | - void VerifyIndex(uint32 index) const | |
| 600 | - { | |
| 601 | -#ifdef ANUBIS_AVOID_ASSERTIONS | |
| 602 | - (void) index; // avoid compiler warnings | |
| 603 | -#else | |
| 604 | - MASSERT(index < _length, "Index Out Of Bounds Exception"); | |
| 605 | -#endif | |
| 606 | - } | |
| 607 | -}; | |
| 608 | - | |
| 609 | -/** Convenience method: returns a string with no characters in it (a.k.a. "") */ | |
| 610 | -const String & GetEmptyString(); | |
| 611 | - | |
| 612 | -template <class T> class HashFunctor; | |
| 613 | - | |
| 614 | -template <> | |
| 615 | -class HashFunctor<String> | |
| 616 | -{ | |
| 617 | -public: | |
| 618 | - uint32 operator () (const String & x) const {return CStringHashFunc(x());} | |
| 619 | -}; | |
| 620 | - | |
| 621 | -template <> | |
| 622 | -class HashFunctor<const char *> | |
| 623 | -{ | |
| 624 | -public: | |
| 625 | - uint32 operator () (const char * x) const {return CStringHashFunc(x);} | |
| 626 | -}; | |
| 627 | - | |
| 628 | -/** A function for comparing (const String &)'s -- calls the String operators */ | |
| 629 | -int StringCompareFunc(const String &, const String &, void *); | |
| 630 | - | |
| 631 | -/** A function for comparing (const char *)'s -- calls strcmp() */ | |
| 632 | -int CStringCompareFunc(const char * const &, const char * const &, void *); | |
| 633 | - | |
| 634 | -inline String operator+(const String & lhs, const String &rhs) {String ret(lhs); ret += rhs; return ret;} | |
| 635 | -inline String operator+(const String & lhs, const char *rhs) {String ret(lhs); ret += rhs; return ret;} | |
| 636 | -inline String operator+(const char * lhs, const String & rhs) {String ret(lhs); ret += rhs; return ret;} | |
| 637 | -inline String operator+(const String & lhs, char rhs) {String ret(lhs); ret += rhs; return ret;} | |
| 638 | -inline String operator-(const String & lhs, const String &rhs) {String ret(lhs); ret -= rhs; return ret;} | |
| 639 | -inline String operator-(const String & lhs, const char *rhs) {String ret(lhs); ret -= rhs; return ret;} | |
| 640 | -inline String operator-(const char *lhs, const String &rhs) {String ret(lhs); ret -= rhs; return ret;} | |
| 641 | -inline String operator-(const String & lhs, char rhs) {String ret(lhs); ret -= rhs; return ret;} | |
| 642 | - | |
| 643 | -END_NAMESPACE(muscle); | |
| 644 | - | |
| 645 | -#endif |
anubis_dev/vm/src/anbexec.cpp
| ... | ... | @@ -120,11 +120,11 @@ void handle_sigsegv(int sig) |
| 120 | 120 | |
| 121 | 121 | int security_zone_size = 0; |
| 122 | 122 | |
| 123 | -char *home_directory = NULL; | |
| 123 | +String home_directory; | |
| 124 | 124 | String anubis_directory; |
| 125 | 125 | String my_anubis_directory; |
| 126 | 126 | String anubisVMRootDirectory; |
| 127 | -char *trusted_certs_directory = NULL; | |
| 127 | +String trusted_certs_directory; | |
| 128 | 128 | |
| 129 | 129 | fd_set rfds; |
| 130 | 130 | fd_set wfds; |
| ... | ... | @@ -998,7 +998,7 @@ int main(int argc, char **argv) |
| 998 | 998 | |
| 999 | 999 | //here we make all host OS dependencies init, before entering in kernel land |
| 1000 | 1000 | kernelInit(); |
| 1001 | - anubisVMRootDirectory = GetVMRootDir(argv[0]); | |
| 1001 | + anubisVMRootDirectory = GetProgramRootDir(argv[0]); | |
| 1002 | 1002 | LOGINFO("ROOT DIR %s\n",anubisVMRootDirectory.Cstr()); |
| 1003 | 1003 | #ifdef WIN32_OLD |
| 1004 | 1004 | lpCmdLine = GetCommandLine(); |
| ... | ... | @@ -1203,7 +1203,7 @@ int main(int argc, char **argv) |
| 1203 | 1203 | LOGERROR("Not enough memory.\n"); |
| 1204 | 1204 | my_exit(1); |
| 1205 | 1205 | } |
| 1206 | - sprintf(trusted_certs_directory,"%s/trusted_certs",my_anubis_directory.Cstr()); | |
| 1206 | + trusted_certs_directory = my_anubis_directory + "/trusted_certs"; | |
| 1207 | 1207 | } |
| 1208 | 1208 | |
| 1209 | 1209 | if ((random_file = (char *)malloc(my_anubis_directory.Length()+30)) == NULL) | ... | ... |
anubis_dev/vm/src/debug_log.cpp deleted
| 1 | -/* | |
| 2 | -* Gestion de la console du terminal | |
| 3 | -* | |
| 4 | -* (c)2004 Alain Prouté | |
| 5 | -* Auteur : David RENE david.rene@free.fr | |
| 6 | -* | |
| 7 | -*/ | |
| 8 | - | |
| 9 | -#include <stdarg.h> | |
| 10 | -#include <stdio.h> | |
| 11 | -#include <string.h> | |
| 12 | -#include <stdlib.h> | |
| 13 | -#include "debug_log.h" | |
| 14 | -#include <time.h> | |
| 15 | - | |
| 16 | -#ifdef WIN32 | |
| 17 | -#include <windows.h> | |
| 18 | -#endif | |
| 19 | - | |
| 20 | - | |
| 21 | - | |
| 22 | -void LogInfo(const char *format, ...) | |
| 23 | -{ | |
| 24 | - char buf[2048]; | |
| 25 | - va_list ap; | |
| 26 | - va_start (ap, format); | |
| 27 | - vsprintf (buf, format, ap); | |
| 28 | - va_end (ap); | |
| 29 | - Log(stdout, buf); | |
| 30 | -} | |
| 31 | - | |
| 32 | -void LogError(const char *format, ...) | |
| 33 | -{ | |
| 34 | - char buf[2048]; | |
| 35 | - va_list ap; | |
| 36 | - va_start (ap, format); | |
| 37 | - vsprintf (buf, format, ap); | |
| 38 | - va_end (ap); | |
| 39 | - Log(stderr, buf); | |
| 40 | -} | |
| 41 | - | |
| 42 | -void Log(FILE * output, char *logString) | |
| 43 | -{ | |
| 44 | - //Ecris sur la console fenetre | |
| 45 | -#if defined (__BEOS__) || (_LINUX_) || (WIN32) | |
| 46 | - fprintf(output, "%s", logString); | |
| 47 | - if(output == stderr) | |
| 48 | - fflush(output); | |
| 49 | -#endif | |
| 50 | -} |
anubis_dev/vm/src/debug_log.h deleted
| 1 | -#ifndef __DEBUGLOG_H__ | |
| 2 | -#define __DEBUGLOG_H__ | |
| 3 | - | |
| 4 | -#include <stdio.h> | |
| 5 | -//if(DebugLog::Db) | |
| 6 | -//{ | |
| 7 | -// char prompt[32]; | |
| 8 | -// GetPrompt(prompt); | |
| 9 | -// DebugLog::Db->SetPrompt(prompt); | |
| 10 | -//} | |
| 11 | -//if(DebugLog::Db) | |
| 12 | -// DebugLog::Db->LogCriticalError### | |
| 13 | - | |
| 14 | -//#define LOGCRITICALERROR if(DebugLog::Db) DebugLog::Db->LogCriticalError | |
| 15 | -#define LOGERROR LogError | |
| 16 | -//#define PRINTF if(DebugLog::Db) DebugLog::Db->LogWarning | |
| 17 | -#define LOGINFO LogInfo | |
| 18 | -//#define LOGDEBUG if(DebugLog::Db) DebugLog::Db->LogDebug | |
| 19 | -//#define LOGTRACE if(DebugLog::Db) DebugLog::Db->LogTrace | |
| 20 | - | |
| 21 | - | |
| 22 | - | |
| 23 | -// void LogCriticalError(const char *format, ...); | |
| 24 | - void LogError(const char *format, ...); | |
| 25 | -// void LogWarning(const char *format, ...); | |
| 26 | - void LogInfo(const char *format, ...); | |
| 27 | -// void LogDebug(const char *format, ...); | |
| 28 | -// void LogTrace(const char *format, ...); | |
| 29 | - | |
| 30 | - void Log(FILE *, char *format); | |
| 31 | - | |
| 32 | -#endif | |
| 33 | - | |
| 34 | - |
anubis_dev/vm/src/sslstuff.cpp
| ... | ... | @@ -57,6 +57,7 @@ int sslnofree = 0; |
| 57 | 57 | |
| 58 | 58 | extern String anubis_directory; |
| 59 | 59 | extern String my_anubis_directory; |
| 60 | +extern String trusted_certs_directory; | |
| 60 | 61 | |
| 61 | 62 | static char *tmp_file_name = "tmp_17dee617b56ed2e6ce54b4042ed87ad4db57d68c"; |
| 62 | 63 | |
| ... | ... | @@ -371,9 +372,9 @@ void init_openssl(void) |
| 371 | 372 | my_exit(1); |
| 372 | 373 | } |
| 373 | 374 | |
| 374 | - if (!(SSL_CTX_load_verify_locations(ssl_ctx,NULL,trusted_certs_directory))) | |
| 375 | + if (!(SSL_CTX_load_verify_locations(ssl_ctx,NULL,trusted_certs_directory.Cstr()))) | |
| 375 | 376 | { |
| 376 | - LOGERROR("Cannot load trusted certificates from '%s'.\n",trusted_certs_directory); | |
| 377 | + LOGERROR("Cannot load trusted certificates from '%s'.\n",trusted_certs_directory.Cstr()); | |
| 377 | 378 | my_exit(1); |
| 378 | 379 | } |
| 379 | 380 | SSL_CTX_set_verify_depth(ssl_ctx,1); | ... | ... |
anubis_dev/vm/src/vm.cpp
| ... | ... | @@ -89,7 +89,7 @@ U32 end_debug = 0; |
| 89 | 89 | |
| 90 | 90 | extern String anubis_directory; |
| 91 | 91 | extern String my_anubis_directory; |
| 92 | - | |
| 92 | +extern String trusted_certs_directory; | |
| 93 | 93 | |
| 94 | 94 | #define vmbuf_size (2000) |
| 95 | 95 | char vmbuf[vmbuf_size]; |
| ... | ... | @@ -8559,7 +8559,7 @@ if (debugging || |
| 8559 | 8559 | NID_commonName,peer_CN,256), |
| 8560 | 8560 | do { m_R = 3; m_IP += 1+2; goto do_computing; } while(0)); |
| 8561 | 8561 | |
| 8562 | - if (trusted_certs_directory == NULL) | |
| 8562 | + if (trusted_certs_directory.Length() == NULL) | |
| 8563 | 8563 | { |
| 8564 | 8564 | m_R = 0; // ca_directory_not_found |
| 8565 | 8565 | } | ... | ... |
anubis_dev/vm/src/vm.h
anubis_dev/vm/src/win32/host_graph.c deleted
| 1 | - | |
| 2 | - | |
| 3 | -/* Windows dedicated file */ | |
| 4 | - | |
| 5 | - | |
| 6 | -#include "../host_graph.h" | |
| 7 | -#include "../vm.h" | |
| 8 | -#include <assert.h> | |
| 9 | -#include <zmouse.h> | |
| 10 | - | |
| 11 | -HWND hDesktop; | |
| 12 | -HDC domain_area; | |
| 13 | - | |
| 14 | -int host_translate_key(int k); | |
| 15 | -int host_translate_exkey(int k); | |
| 16 | -U8 host_translate_keyboard_state(unsigned int state); | |
| 17 | - | |
| 18 | - | |
| 19 | -void host_init_graphism(void) | |
| 20 | -{ | |
| 21 | - RECT rect; | |
| 22 | - hDesktop = GetDesktopWindow(); | |
| 23 | - domain_area = GetDC(NULL); | |
| 24 | - | |
| 25 | - rect.bottom; | |
| 26 | -} | |
| 27 | - | |
| 28 | -BOOL host_sc_open_host_window(U32 xPos, U32 yPos, U32 wSize, U32 hSize, char * winTitle, BOOL transcientFlag, U32 * the_window, U32 * the_gc) | |
| 29 | -{ | |
| 30 | -WNDCLASSEX wc; | |
| 31 | -HWND lHWND; | |
| 32 | -HostWindow the_window_ptr; | |
| 33 | - | |
| 34 | - wc.cbSize = sizeof(WNDCLASSEX); | |
| 35 | - wc.style = CS_HREDRAW | CS_VREDRAW; | |
| 36 | - wc.lpfnWndProc = AnubisWindowProc; | |
| 37 | - wc.cbClsExtra = 0; | |
| 38 | - wc.cbWndExtra = 0; | |
| 39 | - wc.hInstance = (HINSTANCE)0x00400000; | |
| 40 | - wc.hIcon = NULL; | |
| 41 | - wc.hCursor = LoadCursor(NULL, IDC_ARROW); | |
| 42 | - wc.hbrBackground = NULL; | |
| 43 | - wc.lpszMenuName = NULL; | |
| 44 | - wc.lpszClassName = winTitle; | |
| 45 | - wc.hIconSm = NULL; | |
| 46 | - | |
| 47 | - RegisterClassEx(&wc); | |
| 48 | - | |
| 49 | -/* Create the window */ | |
| 50 | - lHWND = CreateWindow(winTitle, | |
| 51 | - winTitle, | |
| 52 | - WS_OVERLAPPEDWINDOW, //WS_POPUP, //, | |
| 53 | - xPos, | |
| 54 | - yPos, | |
| 55 | - wSize, | |
| 56 | - hSize, | |
| 57 | - NULL, | |
| 58 | - NULL, | |
| 59 | - NULL, | |
| 60 | - NULL); | |
| 61 | - | |
| 62 | - | |
| 63 | - the_window_ptr = (HostWindow)malloc(sizeof(HostWindow_type)); | |
| 64 | - | |
| 65 | - if(the_window_ptr) | |
| 66 | - { | |
| 67 | - memset(the_window_ptr,0,sizeof(HostWindow_type)); | |
| 68 | - | |
| 69 | - the_window_ptr->handle = lHWND; | |
| 70 | - | |
| 71 | - *the_window = (U32)the_window_ptr; | |
| 72 | - *the_gc = (U32)NULL; | |
| 73 | - | |
| 74 | - //always return TRUE at this time, maybe we manage error in the future | |
| 75 | - return TRUE; | |
| 76 | - } | |
| 77 | - | |
| 78 | - return FALSE; | |
| 79 | -} | |
| 80 | - | |
| 81 | -void host_sc_show_host_window(U32 theWindow) | |
| 82 | -{ | |
| 83 | - HostWindow win = (HostWindow)theWindow; | |
| 84 | - | |
| 85 | - ShowWindow(win->handle, SW_SHOW); | |
| 86 | - UpdateWindow(win->handle); | |
| 87 | - SetCursor(LoadCursor(NULL, IDC_ARROW)); | |
| 88 | -} | |
| 89 | - | |
| 90 | -void host_sc_hide_host_window(U32 theWindow) | |
| 91 | -{ | |
| 92 | - HostWindow win = (HostWindow)theWindow; | |
| 93 | - | |
| 94 | - ShowWindow(win->handle, SW_HIDE); | |
| 95 | - UpdateWindow(win->handle); | |
| 96 | - | |
| 97 | -} | |
| 98 | - | |
| 99 | -void host_free_host_window(U32 winPointer) | |
| 100 | -{ | |
| 101 | - assert(winPointer); | |
| 102 | - | |
| 103 | - free((HostWindow) winPointer); | |
| 104 | - | |
| 105 | -} | |
| 106 | - | |
| 107 | -void host_free_host_pixmap(U32 pixPointer) | |
| 108 | -{ | |
| 109 | - assert(pixPointer); | |
| 110 | - free((HostImage) pixPointer); | |
| 111 | -} | |
| 112 | - | |
| 113 | -void host_free_host_gc(U32 winPointer) | |
| 114 | -{ | |
| 115 | - //nothing to do at this time | |
| 116 | - return; | |
| 117 | -} | |
| 118 | - | |
| 119 | -void host_sc_host_window_size(U32 theWindow, int * wSize, int * hSize) | |
| 120 | -{ | |
| 121 | - RECT rect; | |
| 122 | - HostWindow win = (HostWindow)theWindow; | |
| 123 | - | |
| 124 | - GetWindowRect(win->handle, &rect); | |
| 125 | - *wSize = rect.right - rect.left; | |
| 126 | - *hSize = rect.bottom - rect.top; | |
| 127 | -} | |
| 128 | - | |
| 129 | -void host_sc_resize_host_window(U32 theWindow, int width, int height) | |
| 130 | -{ | |
| 131 | - Salut David ! | |
| 132 | - noublie pas de mettre quelque chose comme sup(30,width) et sup(30,height) pour éviter les tailles | |
| 133 | - négatives ou trop petites. | |
| 134 | -} | |
| 135 | - | |
| 136 | -void host_pixmap_size(U32 thePixmap, int * wSize, int * hSize) | |
| 137 | -{ | |
| 138 | - HostImage pixmap = (HostImage)thePixmap; | |
| 139 | - BITMAP retBitmap; | |
| 140 | - | |
| 141 | - GetObject(pixmap->hBitmap, sizeof(BITMAP), &retBitmap); | |
| 142 | - | |
| 143 | - * wSize = retBitmap.bmWidth; | |
| 144 | - * hSize = retBitmap.bmHeight; | |
| 145 | -} | |
| 146 | - | |
| 147 | -void host_free_graphical_resources(void) | |
| 148 | -{ | |
| 149 | -} | |
| 150 | - | |
| 151 | -void host_change_host_window_title(U32 theWindow, char *title) | |
| 152 | -{ | |
| 153 | - HostWindow win = (HostWindow)theWindow; | |
| 154 | - | |
| 155 | - SetWindowText(win->handle, title); | |
| 156 | -} | |
| 157 | - | |
| 158 | -void host_begin_paint(U32 theWindow) | |
| 159 | -{ | |
| 160 | - HostWindow win = (HostWindow)theWindow; | |
| 161 | - | |
| 162 | - BeginPaint(win->handle, &(win->pnt)); | |
| 163 | -} | |
| 164 | - | |
| 165 | -void host_end_paint(U32 theWindow) | |
| 166 | -{ | |
| 167 | - HostWindow win = (HostWindow)theWindow; | |
| 168 | - EndPaint(win->handle, &(win->pnt)); | |
| 169 | -} | |
| 170 | - | |
| 171 | -U32 host_create_pixmap(U32 target_width, U32 target_height) | |
| 172 | -{ | |
| 173 | - HostImage pixmap; | |
| 174 | - HDC tempDC; | |
| 175 | - HBITMAP tempBitmap; | |
| 176 | - | |
| 177 | - pixmap = (HostImage)malloc(sizeof(HostImage_type)); | |
| 178 | - tempDC = CreateCompatibleDC(domain_area); | |
| 179 | - tempBitmap = CreateCompatibleBitmap(domain_area,target_width ,target_height); | |
| 180 | - | |
| 181 | - pixmap->hBitmap = tempBitmap; | |
| 182 | - pixmap->hDC = tempDC; | |
| 183 | - | |
| 184 | - return (U32)pixmap; | |
| 185 | -} | |
| 186 | - | |
| 187 | -void host_paint_pixel_to(U32 dest, U32 x, U32 y, U32 color) | |
| 188 | -{ | |
| 189 | - COLORREF result; | |
| 190 | - HostImage pixmap = (HostImage)dest; | |
| 191 | - | |
| 192 | - SelectObject(pixmap->hDC, pixmap->hBitmap); | |
| 193 | - result = SetPixel(pixmap->hDC, x,y, (COLORREF) RGB_to_X(color)); | |
| 194 | -} | |
| 195 | - | |
| 196 | -void host_sc_paint_rectangle(U32 theWindow, U32 gc, int xX, int y, int w, int h, U32 color) | |
| 197 | -{ | |
| 198 | - RECT tmpRect; | |
| 199 | - HBRUSH tmpBrush; | |
| 200 | - unsigned long xcolor = RGB_to_X(color); | |
| 201 | - HostWindow win = (HostWindow)theWindow; | |
| 202 | - | |
| 203 | - HDC lgc = GetDC(win->handle); | |
| 204 | - | |
| 205 | - tmpRect.left = xX; | |
| 206 | - tmpRect.top = y; | |
| 207 | - tmpRect.right = xX + w; | |
| 208 | - tmpRect.bottom = y+h; | |
| 209 | - tmpBrush = CreateSolidBrush(color); | |
| 210 | - FillRect(lgc ,&tmpRect, tmpBrush); | |
| 211 | - DeleteObject(tmpBrush); | |
| 212 | - | |
| 213 | - ReleaseDC(win->handle, lgc); | |
| 214 | -} | |
| 215 | -void host_sc_paint_rectangle_to_buffer(U32 pixMap, int xX, int y, int w, int h, U32 color) | |
| 216 | -{ | |
| 217 | - RECT tmpRect; | |
| 218 | - HBRUSH tmpBrush; | |
| 219 | - unsigned long xcolor = RGB_to_X(color); | |
| 220 | - HostImage pix = (HostImage)pixMap; | |
| 221 | - | |
| 222 | - tmpRect.left = xX; | |
| 223 | - tmpRect.top = y; | |
| 224 | - tmpRect.right = xX + w; | |
| 225 | - tmpRect.bottom = y+h; | |
| 226 | - tmpBrush = CreateSolidBrush(color); | |
| 227 | - FillRect(pix->hDC ,&tmpRect, tmpBrush); | |
| 228 | - DeleteObject(tmpBrush); | |
| 229 | -} | |
| 230 | - | |
| 231 | -void host_draw_pixel_to_buffer(U32 buffer, int x, int y, U32 color) | |
| 232 | -{ | |
| 233 | - COLORREF result; | |
| 234 | - COLORREF xcolor = RGB_to_X(color); | |
| 235 | - HostImage pix = (HostImage)buffer; | |
| 236 | - | |
| 237 | - SelectObject(pix->hDC, pix->hBitmap); | |
| 238 | - result = SetPixel(pix->hDC, x,y, xcolor); | |
| 239 | -} | |
| 240 | - | |
| 241 | -void host_paint_host_image(U32 theWindow, U32 thePixmap, int a, int b, int u, int v, int x, int y) | |
| 242 | -{ | |
| 243 | - BOOL result; | |
| 244 | - HostWindow win = (HostWindow)theWindow; | |
| 245 | - HostImage pix = (HostImage)thePixmap; | |
| 246 | - HDC winHDC = GetDC(win->handle); | |
| 247 | - | |
| 248 | - result = BitBlt(winHDC, x, y, u, v, pix->hDC, a, b, SRCCOPY); | |
| 249 | - | |
| 250 | - ReleaseDC(win->handle, winHDC); | |
| 251 | -} | |
| 252 | - | |
| 253 | - | |
| 254 | -void host_paint_host_image_to_buffer(U32 src, U32 dest, int src_x, int src_y, | |
| 255 | - int src_w, int src_h, | |
| 256 | - int dest_x, int dest_y) | |
| 257 | -{ | |
| 258 | - BOOL result; | |
| 259 | - HostImage lSrc = (HostImage)src; | |
| 260 | - HostImage lDest = (HostImage)dest; | |
| 261 | - | |
| 262 | - result = BitBlt(lDest->hDC, dest_x, dest_y, src_w, src_h, lSrc->hDC,src_x, src_y, SRCCOPY); | |
| 263 | - | |
| 264 | -} | |
| 265 | - | |
| 266 | -/* Internal use only */ | |
| 267 | - | |
| 268 | -void host_sc_screen_size(int *witdh, int *height) | |
| 269 | -{ | |
| 270 | - RECT tmpRect; | |
| 271 | - GetClientRect(hDesktop, &tmpRect); | |
| 272 | - | |
| 273 | - *witdh = tmpRect.right; | |
| 274 | - *height = tmpRect.bottom; | |
| 275 | -} | |
| 276 | - | |
| 277 | -void host_queue_host_window_events(void) | |
| 278 | -{ | |
| 279 | - MSG msg; | |
| 280 | - LRESULT lResult; | |
| 281 | - | |
| 282 | -begin: /* we jump here when we have processed a message ourself, i.e. | |
| 283 | - without returning to run_thot. */ | |
| 284 | - | |
| 285 | - /* test for a pending message from Windows without waiting for it */ | |
| 286 | - if (!PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) | |
| 287 | - { | |
| 288 | - /* no message is pending. return 0 so that run_thot will know that | |
| 289 | - no message is pending. This will let it work on another stuff, like | |
| 290 | - updating the clocks... Don't worry about other processes. After all, | |
| 291 | - Windows is now a preemptive multitasking system. */ | |
| 292 | - return; | |
| 293 | - } | |
| 294 | - | |
| 295 | - /* if there is one, get the pending message */ | |
| 296 | - if (!GetMessage(&msg,NULL,0,0)) | |
| 297 | - { | |
| 298 | - /* Here the message is WM_QUIT: we must quit. */ | |
| 299 | - return ; /* after having dumped the current state */ | |
| 300 | - } | |
| 301 | - else | |
| 302 | - { | |
| 303 | - /* Due to Windows' method of message transmission, we must return | |
| 304 | - the translated message to Windows, which will call us again through | |
| 305 | - WindowProc... Thanks Bill ! */ | |
| 306 | - TranslateMessage(&msg); | |
| 307 | - lResult = DispatchMessage(&msg); | |
| 308 | - /* see you again in WindowProc (below) */ | |
| 309 | - | |
| 310 | - if (lResult == -1) | |
| 311 | - goto begin; | |
| 312 | - else | |
| 313 | - return /*(int)lResult*/; | |
| 314 | - } | |
| 315 | -} | |
| 316 | - | |
| 317 | - | |
| 318 | -//#define show_events | |
| 319 | - | |
| 320 | -/* Here is the actual processing of host (Windows) messages */ | |
| 321 | -LRESULT CALLBACK AnubisWindowProc(HWND hWnd, | |
| 322 | - UINT message, | |
| 323 | - WPARAM wParam, | |
| 324 | - LPARAM lParam) | |
| 325 | -{ | |
| 326 | - U32 R = 0; | |
| 327 | - U32 handle; | |
| 328 | - int retValue = 1; | |
| 329 | - /* | |
| 330 | - 'HostWindowEvent' (see 'predefined.anubis') is a large type: | |
| 331 | - | |
| 332 | - offsets: 0 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
| 333 | - ----------------------------------------------------------------------------------------------------- | |
| 334 | - quit * ---> | cnt | 0 | | |
| 335 | - expose * ---> | cnt | 1 | | |
| 336 | - pointer_entering * ---> | cnt | 2 | | |
| 337 | - pointer_leaving * ---> | cnt | 3 | | |
| 338 | - key_down * ---> | cnt | 4 | s | k | | |
| 339 | - mouse_move * ---> | cnt | 5 | s | x | y | | |
| 340 | - mouse_click * ---> | cnt | 6 | s | c | x | y | | |
| 341 | - mouse_wheel * ---> | cnt | 7 | s | delta | x | y | | |
| 342 | - other events are never generated here | |
| 343 | - | |
| 344 | - We allocate a segment big enough for any alternative. | |
| 345 | - */ | |
| 346 | - | |
| 347 | - if ((R = allocate_data_segment(5,the_allocator)) == 0) | |
| 348 | - /* Since the introduction of 'mouse_wheel' we need 5 words, not only 4. */ | |
| 349 | - return 0; /* try next time */ | |
| 350 | - | |
| 351 | - handle = (U32)hWnd; | |
| 352 | - | |
| 353 | - switch(message) | |
| 354 | - { | |
| 355 | - case WM_DESTROY: | |
| 356 | - { | |
| 357 | -#ifdef show_events | |
| 358 | - LOGINFO("WM_DESTROY\n"); | |
| 359 | -#endif | |
| 360 | - ((U8 *)R)[4] = 0; /* quit */ | |
| 361 | - break; | |
| 362 | - } | |
| 363 | - case WM_PAINT: | |
| 364 | - { | |
| 365 | -#ifdef show_events | |
| 366 | - LOGINFO("WM_PAINT\n"); | |
| 367 | -#endif | |
| 368 | - ((U8 *)R)[4] = 1; /* expose */ | |
| 369 | - retValue = 0; | |
| 370 | - break; | |
| 371 | - } | |
| 372 | - | |
| 373 | - case WM_SETCURSOR: | |
| 374 | - { | |
| 375 | -#ifdef show_events | |
| 376 | - LOGINFO("WM_SETCURSOR\n"); | |
| 377 | -#endif | |
| 378 | - ((U8 *)R)[4] = 2; /* pointer_entering */ | |
| 379 | - break; | |
| 380 | - } | |
| 381 | - | |
| 382 | - case WM_NCMOUSEMOVE: | |
| 383 | - { | |
| 384 | -#ifdef show_events | |
| 385 | - LOGINFO("WM_NCMOUSEMOUVE\n"); | |
| 386 | -#endif | |
| 387 | - ((U8 *)R)[4] = 3; /* pointer_leaving */ | |
| 388 | - //retValue = 0; | |
| 389 | - break; | |
| 390 | - } | |
| 391 | - | |
| 392 | - case WM_KEYDOWN: | |
| 393 | - { | |
| 394 | -#ifdef show_events | |
| 395 | - LOGINFO("WM_KEYDOWN\n"); | |
| 396 | -#endif | |
| 397 | - ((U8 *)R)[4] = 4; /* key_down */ | |
| 398 | - ((U8 *)R)[5] = 0; | |
| 399 | - ((U8 *)R)[6] = host_translate_exkey(wParam); | |
| 400 | - //retValue = 0; | |
| 401 | - break; | |
| 402 | - } | |
| 403 | - | |
| 404 | - case WM_CHAR: | |
| 405 | - { | |
| 406 | -#ifdef show_events | |
| 407 | - LOGINFO("WM_CHAR\n"); | |
| 408 | -#endif | |
| 409 | - ((U8 *)R)[4] = 4; /* key_down */ | |
| 410 | - ((U8 *)R)[5] = 0; | |
| 411 | - ((U8 *)R)[6] = host_translate_key(wParam); | |
| 412 | - //retValue = 0; | |
| 413 | - break; | |
| 414 | - } | |
| 415 | - | |
| 416 | - case WM_MOUSEMOVE: | |
| 417 | - { | |
| 418 | -#ifdef show_events | |
| 419 | - LOGINFO("WM_MOUSEMOUVE\n"); | |
| 420 | -#endif | |
| 421 | - ((U8 *)R)[4] = 5; /* mouse_move */ | |
| 422 | - ((U8 *)R)[5] = host_translate_keyboard_state(wParam) ; | |
| 423 | - *((U32 *)(((U8 *)R)+6)) = (U32)LOWORD(lParam); | |
| 424 | - *((U32 *)(((U8 *)R)+10)) = (U32)HIWORD(lParam); | |
| 425 | - //LOGINFO("MOUSE_MOVE x=%d y=%d\n", LOWORD(lParam), HIWORD(lParam) ); | |
| 426 | - //retValue = 0; | |
| 427 | - break; | |
| 428 | - } | |
| 429 | - | |
| 430 | - case WM_LBUTTONDOWN: | |
| 431 | - { | |
| 432 | -#ifdef show_events | |
| 433 | - LOGINFO("WM_LBUTTONDOWN\n"); | |
| 434 | -#endif | |
| 435 | - ((U8 *)R)[4] = 6; /* mouse_click */ | |
| 436 | - ((U8 *)R)[5] = host_translate_keyboard_state(wParam) ; | |
| 437 | - ((U8 *)R)[6] = MC_left_down; /* left_down */ | |
| 438 | - *((U32 *)(((U8 *)R)+7)) = (U32)LOWORD(lParam); | |
| 439 | - *((U32 *)(((U8 *)R)+11)) = (U32)HIWORD(lParam); | |
| 440 | - //LOGINFO("MC LEFT DOWN x=%d y=%d\n", LOWORD(lParam), HIWORD(lParam) ); | |
| 441 | - //retValue = 0; | |
| 442 | - break; | |
| 443 | - } | |
| 444 | - | |
| 445 | - case WM_LBUTTONUP: | |
| 446 | - { | |
| 447 | -#ifdef show_events | |
| 448 | - LOGINFO("WM_LBUTTONUP\n"); | |
| 449 | -#endif | |
| 450 | - ((U8 *)R)[4] = 6; /* mouse_click */ | |
| 451 | - ((U8 *)R)[5] = host_translate_keyboard_state(wParam) ; | |
| 452 | - ((U8 *)R)[6] = MC_left_up; /* left_up */ | |
| 453 | - *((U32 *)(((U8 *)R)+7)) = (U32)LOWORD(lParam); | |
| 454 | - *((U32 *)(((U8 *)R)+11)) = (U32)HIWORD(lParam); | |
| 455 | - //LOGINFO("MC LEFT UP x=%d y=%d\n", LOWORD(lParam), HIWORD(lParam) ); | |
| 456 | - //retValue = 0; | |
| 457 | - | |
| 458 | - break; | |
| 459 | - } | |
| 460 | - | |
| 461 | - case WM_MBUTTONDOWN: | |
| 462 | - { | |
| 463 | -#ifdef show_events | |
| 464 | - LOGINFO("WM_MBUTTONDOWN\n"); | |
| 465 | -#endif | |
| 466 | - ((U8 *)R)[4] = 6; /* mouse_click */ | |
| 467 | - ((U8 *)R)[5] = host_translate_keyboard_state(wParam) ; | |
| 468 | - ((U8 *)R)[6] = MC_middle_down; /* left_down */ | |
| 469 | - *((U32 *)(((U8 *)R)+7)) = (U32)LOWORD(lParam); | |
| 470 | - *((U32 *)(((U8 *)R)+11)) = (U32)HIWORD(lParam); | |
| 471 | - //LOGINFO("MC MIDDLE DOWN x=%d y=%d\n", LOWORD(lParam), HIWORD(lParam) ); | |
| 472 | - //retValue = 0; | |
| 473 | - | |
| 474 | - break; | |
| 475 | - } | |
| 476 | - | |
| 477 | - case WM_MBUTTONUP: | |
| 478 | - { | |
| 479 | -#ifdef show_events | |
| 480 | - LOGINFO("WM_MBUTTONUP\n"); | |
| 481 | -#endif | |
| 482 | - ((U8 *)R)[4] = 6; /* mouse_click */ | |
| 483 | - ((U8 *)R)[5] = host_translate_keyboard_state(wParam) ; | |
| 484 | - ((U8 *)R)[6] = MC_middle_up; /* left_up */ | |
| 485 | - *((U32 *)(((U8 *)R)+7)) = (U32)LOWORD(lParam); | |
| 486 | - *((U32 *)(((U8 *)R)+11)) = (U32)HIWORD(lParam); | |
| 487 | - //LOGINFO("MC MIDDLE UP x=%d y=%d\n", LOWORD(lParam), HIWORD(lParam) ); | |
| 488 | - //retValue = 0; | |
| 489 | - | |
| 490 | - break; | |
| 491 | - } | |
| 492 | - | |
| 493 | - case WM_RBUTTONDOWN: | |
| 494 | - { | |
| 495 | -#ifdef show_events | |
| 496 | - LOGINFO("WM_RBUTTONDOWN\n"); | |
| 497 | -#endif | |
| 498 | - ((U8 *)R)[4] = 6; /* mouse_click */ | |
| 499 | - ((U8 *)R)[5] = host_translate_keyboard_state(wParam) ; | |
| 500 | - ((U8 *)R)[6] = MC_right_down; /* right_down */ | |
| 501 | - *((U32 *)(((U8 *)R)+7)) = (U32)LOWORD(lParam); | |
| 502 | - *((U32 *)(((U8 *)R)+11)) = (U32)HIWORD(lParam); | |
| 503 | - //LOGINFO("MC RIGHT DOWN x=%d y=%d\n", LOWORD(lParam), HIWORD(lParam) ); | |
| 504 | - //retValue = 0; | |
| 505 | - | |
| 506 | - break; | |
| 507 | - } | |
| 508 | - | |
| 509 | - case WM_RBUTTONUP: | |
| 510 | - { | |
| 511 | -#ifdef show_events | |
| 512 | - LOGINFO("WM_RBUTTONUP\n"); | |
| 513 | -#endif | |
| 514 | - ((U8 *)R)[4] = 6; /* mouse_click */ | |
| 515 | - ((U8 *)R)[5] = host_translate_keyboard_state(wParam) ; | |
| 516 | - ((U8 *)R)[6] = MC_right_up; /* right_up */ | |
| 517 | - *((U32 *)(((U8 *)R)+7)) = (U32)LOWORD(lParam); | |
| 518 | - *((U32 *)(((U8 *)R)+11)) = (U32)HIWORD(lParam); | |
| 519 | - //LOGINFO("MC RIGHT UP x=%d y=%d\n", LOWORD(lParam), HIWORD(lParam) ); | |
| 520 | - //retValue = 0; | |
| 521 | - | |
| 522 | - break; | |
| 523 | - } | |
| 524 | - | |
| 525 | - case WM_MOUSEWHEEL: | |
| 526 | - { | |
| 527 | -#ifdef show_events | |
| 528 | - LOGINFO("WM_MOUSEWHEEL\n"); | |
| 529 | -#endif | |
| 530 | - ((U8 *)R)[4] = 7; /* mouse wheel */ | |
| 531 | - ((U8 *)R)[5] = host_translate_keyboard_state(wParam); | |
| 532 | - *((U32 *)(((U8 *)R)+6)) = (U32)HIWORD(wParam); | |
| 533 | - *((U32 *)(((U8 *)R)+10)) = (U32)LOWORD(lParam); | |
| 534 | - *((U32 *)(((U8 *)R)+14)) = (U32)HIWORD(lParam); | |
| 535 | - //LOGINFO("MC RIGHT UP x=%d y=%d\n", LOWORD(lParam), HIWORD(lParam) ); | |
| 536 | - break; | |
| 537 | - } | |
| 538 | - | |
| 539 | - default: | |
| 540 | -#ifdef show_events | |
| 541 | - LOGINFO("other event\n"); | |
| 542 | -#endif | |
| 543 | - return DefWindowProc(hWnd,message,wParam,lParam); | |
| 544 | - } | |
| 545 | - | |
| 546 | - //add event to Anubis Land | |
| 547 | - queue_event(R, handle); | |
| 548 | - | |
| 549 | - return retValue; | |
| 550 | - | |
| 551 | -} | |
| 552 | - | |
| 553 | -U8 host_translate_keyboard_state(unsigned int state) | |
| 554 | -{ | |
| 555 | - U8 result; | |
| 556 | - | |
| 557 | - result = state & MK_SHIFT; | |
| 558 | - result |= (state & MK_CONTROL) << 1; | |
| 559 | - result |= (state & MK_ALT) << 2; | |
| 560 | - result |= (state & MK_LBUTTON) << 3; | |
| 561 | - result |= (state & MK_MBUTTON) << 4; | |
| 562 | - result |= (state & MK_RBUTTON) << 4; | |
| 563 | - | |
| 564 | - return result; | |
| 565 | - | |
| 566 | -} | |
| 567 | - | |
| 568 | -int host_translate_key(int k) | |
| 569 | -{ | |
| 570 | - switch (k) | |
| 571 | - { | |
| 572 | - /* decimal digits */ | |
| 573 | - /* | |
| 574 | - case VK_0: return '0'; | |
| 575 | - case VK_1: return '1'; | |
| 576 | - case VK_2: return '2'; | |
| 577 | - case VK_3: return '3'; | |
| 578 | - case VK_4: return '4'; | |
| 579 | - case VK_5: return '5'; | |
| 580 | - case VK_6: return '6'; | |
| 581 | - case VK_7: return '7'; | |
| 582 | - case VK_8: return '8'; | |
| 583 | - case VK_9: return '9'; | |
| 584 | - */ | |
| 585 | - /* not defined in winuser.h !!! but same as '0', '1, etc... */ | |
| 586 | - | |
| 587 | - /* accented letters */ | |
| 588 | - case 0xFC: return 129; /* u trema */ | |
| 589 | - case 0xE9: return 130; /* e accute */ | |
| 590 | - case 0xE2: return 131; /* a circumflex */ | |
| 591 | - case 0xE4: return 132; /* a trema */ | |
| 592 | - case 0xE0: return 133; /* a grave */ | |
| 593 | - case 0xE7: return 135; /* c cedilla */ | |
| 594 | - case 0xEA: return 136; /* e circumflex */ | |
| 595 | - case 0xEB: return 137; /* e trema */ | |
| 596 | - case 0xE8: return 138; /* e grave */ | |
| 597 | - case 0xEF: return 139; /* i trema */ | |
| 598 | - case 0xEE: return 140; /* i circumflex */ | |
| 599 | - case 0xF4: return 147; /* o circumflex */ | |
| 600 | - case 0xF6: return 148; /* o trema */ | |
| 601 | - case 0xFB: return 150; /* u circumflex */ | |
| 602 | - case 0xF9: return 151; /* u grave */ | |
| 603 | - | |
| 604 | - /* special keys */ | |
| 605 | - /* | |
| 606 | - case VK_LEFT: return left_arrow_key; | |
| 607 | - case VK_RIGHT: return right_arrow_key; | |
| 608 | - case VK_UP: return up_arrow_key; | |
| 609 | - case VK_DOWN: return down_arrow_key; | |
| 610 | - case VK_BACK: return backspace_key; | |
| 611 | - case VK_TAB: return tab_key; | |
| 612 | - case VK_RETURN: return enter_key; | |
| 613 | - case VK_ESCAPE: return escape_key; | |
| 614 | - case VK_DELETE: return del_key; | |
| 615 | - case VK_INSERT: return ins_key; | |
| 616 | - case VK_HOME: return home_key; | |
| 617 | - case VK_END: return end_key; | |
| 618 | - case VK_PRIOR: return page_up_key; | |
| 619 | - case VK_NEXT: return page_down_key; | |
| 620 | - case VK_F1: return F1_key; | |
| 621 | - case VK_F2: return F2_key; | |
| 622 | - case VK_F3: return F3_key; | |
| 623 | - case VK_F4: return F4_key; | |
| 624 | - case VK_F5: return F5_key; | |
| 625 | - case VK_F6: return F6_key; | |
| 626 | - case VK_F7: return F7_key; | |
| 627 | - case VK_F8: return F8_key; | |
| 628 | - case VK_F9: return F9_key; | |
| 629 | - case VK_F10: return F10_key; | |
| 630 | - */ | |
| 631 | - | |
| 632 | - /* ordinary character keys */ | |
| 633 | - default: return k; | |
| 634 | - } | |
| 635 | -} | |
| 636 | - | |
| 637 | -int host_translate_exkey(int k) | |
| 638 | -{ | |
| 639 | - switch (k) | |
| 640 | - { | |
| 641 | - /* special keys */ | |
| 642 | - case VK_LEFT: return KSK_left; | |
| 643 | - case VK_RIGHT: return KSK_right; | |
| 644 | - case VK_UP: return KSK_up; | |
| 645 | - case VK_DOWN: return KSK_down; | |
| 646 | - case VK_BACK: return KSK_backspace; | |
| 647 | - case VK_TAB: return KSK_tab; | |
| 648 | - case VK_RETURN: return KSK_enter; | |
| 649 | - case VK_ESCAPE: return KSK_escape; | |
| 650 | - case VK_DELETE: return KSK_del; | |
| 651 | - case VK_INSERT: return KSK_insert; | |
| 652 | - case VK_HOME: return KSK_home; | |
| 653 | - case VK_END: return KSK_end; | |
| 654 | - case VK_PRIOR: return KSK_page_up; | |
| 655 | - case VK_NEXT: return KSK_page_down; | |
| 656 | - case VK_F1: return KSK_f1; | |
| 657 | - case VK_F2: return KSK_f2; | |
| 658 | - case VK_F3: return KSK_f3; | |
| 659 | - case VK_F4: return KSK_f4; | |
| 660 | - case VK_F5: return KSK_f5; | |
| 661 | - case VK_F6: return KSK_f6; | |
| 662 | - case VK_F7: return KSK_f7; | |
| 663 | - case VK_F8: return KSK_f8; | |
| 664 | - case VK_F9: return KSK_f9; | |
| 665 | - case VK_F10: return KSK_f10; | |
| 666 | - | |
| 667 | - /* ordinary character keys */ | |
| 668 | - default: return k; | |
| 669 | - } | |
| 670 | -} | |
| 671 | - |
anubis_dev/vm/src/win32stuff.cpp deleted
| 1 | -#include "dependencies.h" | |
| 2 | -#include "debug_log.h" | |
| 3 | -#include <assert.h> | |
| 4 | - | |
| 5 | -int kernelInit(void) | |
| 6 | -{ | |
| 7 | - | |
| 8 | - WSADATA wsadata; | |
| 9 | - | |
| 10 | - /* Open console for output */ | |
| 11 | -// AllocConsole(); | |
| 12 | - | |
| 13 | - assert(sizeof(long) == 4); | |
| 14 | - | |
| 15 | - if (WSAStartup(MAKEWORD(2,2),&wsadata) != 0) | |
| 16 | - { | |
| 17 | - LOGERROR("Winsock.dll not found."); | |
| 18 | - my_exit(1); | |
| 19 | - } | |
| 20 | - return 1; | |
| 21 | -} | |
| 22 | - | |
| 23 | -char * getpass(char * in) | |
| 24 | -{ | |
| 25 | - return ""; | |
| 26 | -} | |
| 27 | - | |
| 28 | - | |
| 29 | -void my_exit(int returnCode) | |
| 30 | -{ | |
| 31 | -// WaitForKeyboard(); | |
| 32 | - exit(returnCode); | |
| 33 | -} | |
| 34 | - | |
| 35 | -void WaitForKeyboard(void) | |
| 36 | -{ | |
| 37 | - INPUT_RECORD inpRecord; | |
| 38 | - DWORD readRecord = 0; | |
| 39 | - BOOL quit = FALSE; | |
| 40 | - | |
| 41 | - while ( quit == FALSE) | |
| 42 | - { | |
| 43 | - ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &inpRecord, 1, &readRecord); | |
| 44 | - if(inpRecord.EventType == KEY_EVENT ) | |
| 45 | - quit = TRUE; | |
| 46 | - } | |
| 47 | - | |
| 48 | -} | |
| 49 | - | |
| 50 | - |