bool set_new_style_variable(const char *variable, const char* value) { INT_VARIABLE_C_IT int_it = &INT_VARIABLE::head; BOOL_VARIABLE_C_IT BOOL_it = &BOOL_VARIABLE::head; STRING_VARIABLE_C_IT STRING_it = &STRING_VARIABLE::head; double_VARIABLE_C_IT double_it = &double_VARIABLE::head; bool foundit = false; // find name for (STRING_it.mark_cycle_pt(); !STRING_it.cycled_list() && strcmp(variable, STRING_it.data()->name); STRING_it.forward()); if (!STRING_it.cycled_list()) { foundit = true; // found the varaible if (*value == '\0') STRING_it.data()->set_value((char *) NULL); // No value. else STRING_it.data()->set_value(value); // set its value } if (*value) { // find name for (int_it.mark_cycle_pt(); !int_it.cycled_list() && strcmp(variable, int_it.data()->name); int_it.forward()); int intval; if (!int_it.cycled_list() && sscanf(value, INT32FORMAT, &intval) == 1) { foundit = true; // found the varaible int_it.data()->set_value(intval); // set its value. } for (BOOL_it.mark_cycle_pt(); !BOOL_it.cycled_list () && strcmp(variable, BOOL_it.data()->name); BOOL_it.forward()); if (!BOOL_it.cycled_list()) { if (*value == 'T' || *value == 't' || *value == 'Y' || *value == 'y' || *value == '1') { foundit = true; BOOL_it.data()->set_value(TRUE); } else if (*value == 'F' || *value == 'f' || *value == 'N' || *value == 'n' || *value == '0') { foundit = true; BOOL_it.data()->set_value(FALSE); } } for (double_it.mark_cycle_pt(); !double_it.cycled_list() && strcmp(variable, double_it.data ()->name); double_it.forward()); double doubleval; #ifdef EMBEDDED if (!double_it.cycled_list ()) { doubleval = strtofloat(value); #else if (!double_it.cycled_list() && sscanf(value, "%lf", &doubleval) == 1) { #endif foundit = true; // found the varaible double_it.data()->set_value(doubleval); } } return foundit; } /********************************************************************** * print_variables * * Print all variable types to the given file **********************************************************************/ DLLSYM void print_variables( //print all vars FILE *fp //file to print on ) { INT_VARIABLE::print(fp); //print INTs BOOL_VARIABLE::print(fp); //print BOOLs STRING_VARIABLE::print(fp); //print STRINGs double_VARIABLE::print(fp); //print doubles }
bool TAccount::loadAccount(const CString& pAccount, bool ignoreNickname) { // Just in case this account was loaded offline through RC. accountName = pAccount; bool loadedFromDefault = false; CFileSystem* accfs = server->getAccountsFileSystem(); std::vector<CString> fileData; CString accountText = server->getPluginManager().LoadAccount(pAccount); if (!accountText.isEmpty()) fileData = accountText.tokenize("\n"); else { // Find the account in the file system. CString accpath(accfs->findi(CString() << pAccount << ".txt")); if (accpath.length() == 0) { accpath = CString() << server->getServerPath() << "accounts/defaultaccount.txt"; CFileSystem::fixPathSeparators(&accpath); loadedFromDefault = true; } // Load file. fileData = CString::loadToken(accpath, "\n"); if (fileData.size() == 0 || fileData[0].trim() != "GRACC001") return false; } // Clear Lists for (int i = 0; i < 30; ++i) attrList[i].clear(); chestList.clear(); mFlagList.clear(); folderList.clear(); weaponList.clear(); // Parse File for (unsigned int i = 0; i < fileData.size(); i++) { // Trim Line fileData[i].trimI(); // Declare Variables; CString section, val; int sep; // Seperate Section & Value sep = fileData[i].find(' '); section = fileData[i].subString(0, sep); if (sep != -1) val = fileData[i].subString(sep + 1); if (section == "NAME") continue; else if (section == "NICK") { if (!ignoreNickname) nickName = val; } else if (section == "COMMUNITYNAME") communityName = val; else if (section == "LEVEL") levelName = val; else if (section == "X") { x = (float)strtofloat(val); x2 = (int)(x * 16); } else if (section == "Y") { y = (float)strtofloat(val); y2 = (int)(y * 16); } else if (section == "Z") { z = (float)strtofloat(val); z2 = (int)(z * 16); } else if (section == "MAXHP") maxPower = (int)strtoint(val); else if (section == "HP") power = (float)strtofloat(val); else if (section == "RUPEES") gralatc = strtoint(val); else if (section == "ANI") gani = val; else if (section == "ARROWS") arrowc = strtoint(val); else if (section == "BOMBS") bombc = strtoint(val); else if (section == "GLOVEP") glovePower = strtoint(val); else if (section == "SHIELDP") shieldPower = strtoint(val); else if (section == "SWORDP") swordPower = strtoint(val); else if (section == "BOWP") bowPower = strtoint(val); else if (section == "BOW") bowImage = val; else if (section == "HEAD") headImg = val; else if (section == "BODY") bodyImg = val; else if (section == "SWORD") swordImg = val; else if (section == "SHIELD") shieldImg = val; else if (section == "COLORS") { std::vector<CString> t = val.tokenize(","); for (int i = 0; i < (int)t.size() && i < 5; i++) colors[i] = (unsigned char)strtoint(t[i]); } else if (section == "SPRITE") sprite = strtoint(val); else if (section == "STATUS") status = strtoint(val); else if (section == "MP") mp = strtoint(val); else if (section == "AP") ap = strtoint(val); else if (section == "APCOUNTER") apCounter = strtoint(val); else if (section == "ONSECS") onlineTime = strtoint(val); else if (section == "IP") { if (accountIp == 0) accountIp = strtolong(val); } else if (section == "LANGUAGE") { language = val; if (language.isEmpty()) language = "English"; } else if (section == "KILLS") kills = strtoint(val); else if (section == "DEATHS") deaths = strtoint(val); else if (section == "RATING") rating = (float)strtofloat(val); else if (section == "DEVIATION") deviation = (float)strtofloat(val); else if (section == "OLDDEVIATION") oldDeviation = (float)strtofloat(val); else if (section == "LASTSPARTIME") lastSparTime = strtolong(val); else if (section == "FLAG") setFlag(val); else if (section == "ATTR1") attrList[0] = val; else if (section == "ATTR2") attrList[1] = val; else if (section == "ATTR3") attrList[2] = val; else if (section == "ATTR4") attrList[3] = val; else if (section == "ATTR5") attrList[4] = val; else if (section == "ATTR6") attrList[5] = val; else if (section == "ATTR7") attrList[6] = val; else if (section == "ATTR8") attrList[7] = val; else if (section == "ATTR9") attrList[8] = val; else if (section == "ATTR10") attrList[9] = val; else if (section == "ATTR11") attrList[10] = val; else if (section == "ATTR12") attrList[11] = val; else if (section == "ATTR13") attrList[12] = val; else if (section == "ATTR14") attrList[13] = val; else if (section == "ATTR15") attrList[14] = val; else if (section == "ATTR16") attrList[15] = val; else if (section == "ATTR17") attrList[16] = val; else if (section == "ATTR18") attrList[17] = val; else if (section == "ATTR19") attrList[18] = val; else if (section == "ATTR20") attrList[19] = val; else if (section == "ATTR21") attrList[20] = val; else if (section == "ATTR22") attrList[21] = val; else if (section == "ATTR23") attrList[22] = val; else if (section == "ATTR24") attrList[23] = val; else if (section == "ATTR25") attrList[24] = val; else if (section == "ATTR26") attrList[25] = val; else if (section == "ATTR27") attrList[26] = val; else if (section == "ATTR28") attrList[27] = val; else if (section == "ATTR29") attrList[28] = val; else if (section == "ATTR30") attrList[29] = val; else if (section == "WEAPON") weaponList.push_back(val); else if (section == "CHEST") chestList.push_back(val); else if (section == "BANNED") isBanned = (strtoint(val) == 0 ? false : true); else if (section == "BANREASON") banReason = val; else if (section == "BANLENGTH") banLength = val; else if (section == "COMMENTS") accountComments = val; else if (section == "EMAIL") email = val; else if (section == "LOCALRIGHTS") adminRights = strtoint(val); else if (section == "IPRANGE") adminIp = val; else if (section == "FOLDERRIGHT") folderList.push_back(val); else if (section == "LASTFOLDER") lastFolder = val; } // Comment out this line if you are actually going to use community names. communityName = accountName; // If we loaded from the default account, save our account now and add it to the file system. if (loadedFromDefault) { saveAccount(); accfs->addFile(CString() << "accounts/" << pAccount << ".txt"); } return true; }
DLLSYM BOOL8 read_variables_file( //read the file const char *file //name to read ) { BOOL8 anyerr; //true if any error char flag; //file flag BOOL8 foundit; //found variable INT16 length; //length of line INT16 nameoffset; //offset for real name char *valptr; //value field char *stringend; //end of string value FILE *fp; //file pointer INT32 intval; //value from file double doubleval; //value form file //iterators INT_VARIABLE_C_IT int_it = &INT_VARIABLE::head; BOOL_VARIABLE_C_IT BOOL_it = &BOOL_VARIABLE::head; STRING_VARIABLE_C_IT STRING_it = &STRING_VARIABLE::head; double_VARIABLE_C_IT double_it = &double_VARIABLE::head; char line[MAX_PATH]; //input line anyerr = FALSE; if (*file == PLUS) { flag = PLUS; //file has flag nameoffset = 1; } else if (*file == MINUS) { flag = MINUS; nameoffset = 1; } else { flag = EQUAL; nameoffset = 0; } fp = fopen (file + nameoffset, "r"); if (fp == NULL) { tprintf ("read_variables_file:Can't open %s", file + nameoffset); return TRUE; //can't open it } while (fgets (line, MAX_PATH, fp)) { if (line[0] != '\n' && line[0] != '#') { length = strlen (line); if (line[length - 1] == '\n') line[length - 1] = '\0'; //cut newline for (valptr = line; *valptr && *valptr != ' ' && *valptr != '\t'; valptr++); if (*valptr) { //found blank *valptr = '\0'; //make name a string do valptr++; //find end of blanks while (*valptr == ' ' || *valptr == '\t'); if (*valptr && *valptr != '#') { //last char in string stringend = valptr + strlen (valptr) - 1; while (stringend != valptr) { while (stringend != valptr && (*stringend == ' ' || *stringend == '\t')) //cut trailing blanks stringend--; stringend[1] = '\0'; //terminate string while (stringend != valptr && (*stringend != ' ' && *stringend != '\t' || stringend[1] != '#')) stringend--; //find word start } } } foundit = FALSE; //find name for (STRING_it.mark_cycle_pt (); !STRING_it.cycled_list () && strcmp (line, STRING_it.data ()->name); STRING_it.forward ()); //found it if (!STRING_it.cycled_list ()) { foundit = TRUE; //found the varaible if (*valptr == '\0' || *valptr == '#') STRING_it.data ()->set_value ((char *) NULL); //no value else //set its value STRING_it.data ()->set_value (valptr); } if (*valptr) { //find name for (int_it.mark_cycle_pt (); !int_it.cycled_list () && strcmp (line, int_it.data ()->name); int_it.forward ()); //found it if (!int_it.cycled_list () && sscanf (valptr, INT32FORMAT, &intval) == 1) { foundit = TRUE; //found the varaible //set its value int_it.data ()->set_value (intval); } //find name for (BOOL_it.mark_cycle_pt (); !BOOL_it.cycled_list () && strcmp (line, BOOL_it.data ()->name); BOOL_it.forward ()); //found it if (!BOOL_it.cycled_list ()) { if (*valptr == 'T' || *valptr == 't' || *valptr == 'Y' || *valptr == 'y' || *valptr == '1') { foundit = TRUE; if (flag == MINUS) BOOL_it.data ()->set_value (FALSE); //set to false else BOOL_it.data ()->set_value (TRUE); //set to true } else if (*valptr == 'F' || *valptr == 'f' || *valptr == 'N' || *valptr == 'n' || *valptr == '0') { foundit = TRUE; if (flag == EQUAL) BOOL_it.data ()->set_value (FALSE); //set to false } } //find name for (double_it.mark_cycle_pt (); !double_it.cycled_list () && strcmp (line, double_it.data ()->name); double_it.forward ()); //found it #ifdef EMBEDDED if (!double_it.cycled_list ()) { doubleval = strtofloat(valptr); #else if (!double_it.cycled_list () && sscanf (valptr, "%lf", &doubleval) == 1) { #endif foundit = TRUE; //found the varaible double_it.data ()->set_value (doubleval); //set its value } if (!foundit) { anyerr = TRUE; //had an error tprintf ("read_variables_file:variable not found: %s", line); } } else if (!foundit) { anyerr = TRUE; //had an error tprintf ("read_variables_file:No value for variable %s", line); } } } fclose(fp); //close file return anyerr; } /********************************************************************** * print_variables * * Print all variable types to the given file **********************************************************************/ DLLSYM void print_variables( //print all vars FILE *fp //file to print on ) { INT_VARIABLE::print(fp); //print INTs BOOL_VARIABLE::print(fp); //print BOOLs STRING_VARIABLE::print(fp); //print STRINGs double_VARIABLE::print(fp); //print doubles }
//---------------------------------------------------------------------------// // AttrAsFloat // //---------------------------------------------------------------------------// float CNodeFile::CNode::AttrAsFloat(const string &sName, float fDefault) { const char *pData = ToElement()->Attribute(sName.c_str()); return (pData ? (float)strtofloat(pData) : fDefault); }
bool strtobool(const string &sStr) { if (!Stricmp(sStr, "true" )) return true; if (!Stricmp(sStr, "false")) return true; return (strtofloat(sStr) > 0); }
float SafeFloatAttr(TiXmlElement *pElem, const char *pAttr, float fDefault) { const char *pData = pElem->Attribute(pAttr); return (pData ? (float)strtofloat(pData) : fDefault); }
float SafeFloat(TiXmlElement *pElem, const char *pChild, float fDefault) { TiXmlNode *pData = pElem->FirstChild(pChild); return ((pData && pData->FirstChild()) ? (float)strtofloat(pData->FirstChild()->Value()) : fDefault); }
float CSettings::getFloat(const CString& pStr, float pDefault) const { boost::recursive_mutex::scoped_lock lock(*m_preventChange); const CKey *key = getKey(pStr); return (key == 0 ? pDefault : (float)strtofloat(key->value)); }