void Packed::poll(bool can_read) { if (!can_read) return; m_socket.peek(); std::streamsize count; while ((count = m_socket.rdbuf()->in_avail()) > 0) { for (int i = 0; i < count; ++i) { int next = m_socket.rdbuf()->sbumpc(); switch (m_state.top()) { case PARSE_STREAM: parseStream(next); break; case PARSE_MAP: parseMap(next); break; case PARSE_LIST: parseList(next); break; case PARSE_MAP_BEGIN: parseMapBegin(next); break; case PARSE_LIST_BEGIN: parseListBegin(next); break; case PARSE_INT: parseInt(next); break; case PARSE_FLOAT: parseFloat(next); break; case PARSE_STRING: parseString(next); break; case PARSE_NAME: parseName(next); break; } } } }
static boolean parseVimLine (const unsigned char *line) { boolean readNextLine = TRUE; if ( (!strncmp ((const char*) line, "comp", (size_t) 4) == 0) && (!strncmp ((const char*) line, "comc", (size_t) 4) == 0) && (strncmp ((const char*) line, "com", (size_t) 3) == 0) ) { readNextLine = parseCommand(line); /* TODO - Handle parseCommand returning FALSE */ } if (isMap(line)) { parseMap(line); } if (strncmp ((const char*) line, "fu", (size_t) 2) == 0) { parseFunction(line); } if (strncmp ((const char*) line, "aug", (size_t) 3) == 0) { parseAutogroup(line); } if ( strncmp ((const char*) line, "let", (size_t) 3) == 0 ) { parseLet(line); } return readNextLine; }
dictionary::dictionary(const std::shared_ptr<std::map<std::string, std::string>> &fromMap) : _pimpl(std::make_shared<pimpl_dictionary>()) { if (fromMap != nullptr){ parseMap(*fromMap); } }
static boolean parseVimLine (const unsigned char *line, int infunction) { boolean readNextLine = TRUE; if (wordMatchLen (line, "command", 3)) { readNextLine = parseCommand(line); /* TODO - Handle parseCommand returning FALSE */ } else if (isMap(line)) { parseMap(skipWord(line)); } else if (wordMatchLen (line, "function", 2)) { parseFunction(skipWord(line)); } else if (wordMatchLen (line, "augroup", 3)) { parseAutogroup(skipWord(line)); } else if (wordMatchLen (line, "let", 3)) { parseLet(skipWord(line), infunction); } return readNextLine; }
void CMetalHandler::loadState() { cb->SendTextMsg("loadstate()L",1); FILE *fp=NULL; //Can't get saving working.. bah. //fp=fopen(hashname(),"r"); metalpatch.clear(); //hotspot.clear(); if (fp!=NULL) { int s=0; fread(&s,sizeof(int),1,fp); if (s!=SAVEGAME_VER) { cb->SendTextMsg("Wrong Savegame Magic",0); fclose(fp); fp=NULL; } } if (fp==NULL) { //cb->SendTextMsg("fp==NULL",1); std::vector<float3> *vi=parseMap(); for (std::vector<float3>::iterator it=vi->begin();it!=vi->end();++it) { metalpatch.push_back(*it); } delete(vi); float sr=cb->GetExtractorRadius()*HOT_SPOT_RADIUS_MULTIPLYER; if (sr<256) sr=256; sr=sr*sr; for (unsigned int i=0;i<this->metalpatch.size();i++){ float3 candidate=metalpatch[i]; bool used=false; for (unsigned int j=0;j<hotspot.size();j++) { float3 t=hotspot[j]; if (((candidate.x-t.x)*(candidate.x-t.x)+(candidate.z-t.z)*(candidate.z-t.z))<sr){ used=true; hotspot[j]=float3((t.x*t.y+candidate.x*candidate.y)/(t.y+candidate.y),t.y+candidate.y,(t.z*t.y+candidate.z*candidate.y)/(t.y+candidate.y)); } } if (used==false) { if (candidate.y>MIN_HOTSPOT_METAL) hotspot.push_back(candidate); } } saveState(); } else { int s; fread(&s,sizeof(int),1,fp); for(int i=0;i<s;i++) { float3 tmp; fread(&tmp,sizeof(float3),1,fp); metalpatch.push_back(tmp); } fread(&s,sizeof(int),1,fp); for(int i=0;i<s;i++) { float3 tmp; fread(&tmp,sizeof(float3),1,fp); hotspot.push_back(tmp); } fclose(fp); } }
bool SCRAMSHA1ClientAuthenticator::setChallenge(const boost::optional<ByteArray>& challenge) { if (step == Initial) { if (!challenge) { return false; } initialServerMessage = *challenge; std::map<char, std::string> keys = parseMap(initialServerMessage.toString()); // Extract the salt ByteArray salt = Base64::decode(keys['s']); // Extract the server nonce std::string clientServerNonce = keys['r']; if (clientServerNonce.size() <= clientnonce.size()) { return false; } std::string receivedClientNonce = clientServerNonce.substr(0, clientnonce.size()); if (receivedClientNonce != clientnonce) { return false; } serverNonce = clientServerNonce.substr(clientnonce.size(), clientServerNonce.npos); // Extract the number of iterations int iterations = 0; try { iterations = boost::lexical_cast<int>(keys['i']); } catch (const boost::bad_lexical_cast&) { return false; } if (iterations <= 0) { return false; } ByteArray channelBindData; if (useChannelBinding && tlsChannelBindingData) { channelBindData = *tlsChannelBindingData; } // Compute all the values needed for the server signature saltedPassword = PBKDF2::encode(StringPrep::getPrepared(getPassword(), StringPrep::SASLPrep), salt, iterations); authMessage = getInitialBareClientMessage() + "," + initialServerMessage + "," + getFinalMessageWithoutProof(); ByteArray serverKey = HMACSHA1::getResult(saltedPassword, "Server Key"); serverSignature = HMACSHA1::getResult(serverKey, authMessage); step = Proof; return true; } else if (step == Proof) { ByteArray result = ByteArray("v=") + ByteArray(Base64::encode(serverSignature)); step = Final; return challenge && challenge == result; } else { return true; } }
err_code loadSystemCallMap() { FILE *mapfile; err_code err = NO_ERROR; mapfile = fopen(MAP_FILE, "r"); if(mapfile == NULL) return ERROR_OPENING_FILE; err = parseMap(mapfile); fclose(mapfile); return err; }
map *openMap(char *filePath) { FILE *file = fopen(filePath, "r"); /* get width and height of map */ int width, height; if(fscanf(file, "P3 %d %d 255", &width, &height) != 2) { printf("error reading frome file\n"); }; /* create the empty map */ map *m = newMap(width, height); /* fill the map */ parseMap(file, m); fclose(file); return m; }
// virtual S32 LLSDBinaryParser::doParse(std::istream& istr, LLSD& data) const { /** * Undefined: '!'<br> * Boolean: 't' for true 'f' for false<br> * Integer: 'i' + 4 bytes network byte order<br> * Real: 'r' + 8 bytes IEEE double<br> * UUID: 'u' + 16 byte unsigned integer<br> * String: 's' + 4 byte integer size + string<br> * strings also secretly support the notation format * Date: 'd' + 8 byte IEEE double for seconds since epoch<br> * URI: 'l' + 4 byte integer size + string uri<br> * Binary: 'b' + 4 byte integer size + binary data<br> * Array: '[' + 4 byte integer size + all values + ']'<br> * Map: '{' + 4 byte integer size every(key + value) + '}'<br> * map keys are serialized as s + 4 byte integer size + string or in the * notation format. */ char c; c = get(istr); if(!istr.good()) { return 0; } S32 parse_count = 1; switch(c) { case '{': { S32 child_count = parseMap(istr, data); if((child_count == PARSE_FAILURE) || data.isUndefined()) { parse_count = PARSE_FAILURE; } else { parse_count += child_count; } if(istr.fail()) { llinfos << "STREAM FAILURE reading binary map." << llendl; parse_count = PARSE_FAILURE; } break; } case '[': { S32 child_count = parseArray(istr, data); if((child_count == PARSE_FAILURE) || data.isUndefined()) { parse_count = PARSE_FAILURE; } else { parse_count += child_count; } if(istr.fail()) { llinfos << "STREAM FAILURE reading binary array." << llendl; parse_count = PARSE_FAILURE; } break; } case '!': data.clear(); break; case '0': data = false; break; case '1': data = true; break; case 'i': { U32 value_nbo = 0; read(istr, (char*)&value_nbo, sizeof(U32)); /*Flawfinder: ignore*/ data = (S32)ntohl(value_nbo); if(istr.fail()) { llinfos << "STREAM FAILURE reading binary integer." << llendl; } break; } case 'r': { F64 real_nbo = 0.0; read(istr, (char*)&real_nbo, sizeof(F64)); /*Flawfinder: ignore*/ data = ll_ntohd(real_nbo); if(istr.fail()) { llinfos << "STREAM FAILURE reading binary real." << llendl; } break; } case 'u': { LLUUID id; read(istr, (char*)(&id.mData), UUID_BYTES); /*Flawfinder: ignore*/ data = id; if(istr.fail()) { llinfos << "STREAM FAILURE reading binary uuid." << llendl; } break; } case '\'': case '"': { std::string value; int cnt = deserialize_string_delim(istr, value, c); if(PARSE_FAILURE == cnt) { parse_count = PARSE_FAILURE; } else { data = value; account(cnt); } if(istr.fail()) { llinfos << "STREAM FAILURE reading binary (notation-style) string." << llendl; parse_count = PARSE_FAILURE; } break; } case 's': { std::string value; if(parseString(istr, value)) { data = value; } else { parse_count = PARSE_FAILURE; } if(istr.fail()) { llinfos << "STREAM FAILURE reading binary string." << llendl; parse_count = PARSE_FAILURE; } break; } case 'l': { std::string value; if(parseString(istr, value)) { data = LLURI(value); } else { parse_count = PARSE_FAILURE; } if(istr.fail()) { llinfos << "STREAM FAILURE reading binary link." << llendl; parse_count = PARSE_FAILURE; } break; } case 'd': { F64 real = 0.0; read(istr, (char*)&real, sizeof(F64)); /*Flawfinder: ignore*/ data = LLDate(real); if(istr.fail()) { llinfos << "STREAM FAILURE reading binary date." << llendl; parse_count = PARSE_FAILURE; } break; } case 'b': { // We probably have a valid raw binary stream. determine // the size, and read it. U32 size_nbo = 0; read(istr, (char*)&size_nbo, sizeof(U32)); /*Flawfinder: ignore*/ S32 size = (S32)ntohl(size_nbo); if(mCheckLimits && (size > mMaxBytesLeft)) { parse_count = PARSE_FAILURE; } else { std::vector<U8> value; if(size > 0) { value.resize(size); account(fullread(istr, (char*)&value[0], size)); } data = value; } if(istr.fail()) { llinfos << "STREAM FAILURE reading binary." << llendl; parse_count = PARSE_FAILURE; } break; } default: parse_count = PARSE_FAILURE; llinfos << "Unrecognized character while parsing: int(" << (int)c << ")" << llendl; break; } if(PARSE_FAILURE == parse_count) { data.clear(); } return parse_count; }
// virtual S32 LLSDNotationParser::doParse(std::istream& istr, LLSD& data) const { // map: { string:object, string:object } // array: [ object, object, object ] // undef: ! // boolean: true | false | 1 | 0 | T | F | t | f | TRUE | FALSE // integer: i#### // real: r#### // uuid: u#### // string: "g'day" | 'have a "nice" day' | s(size)"raw data" // uri: l"escaped" // date: d"YYYY-MM-DDTHH:MM:SS.FFZ" // binary: b##"ff3120ab1" | b(size)"raw data" char c; c = istr.peek(); while(isspace(c)) { // pop the whitespace. c = get(istr); c = istr.peek(); continue; } if(!istr.good()) { return 0; } S32 parse_count = 1; switch(c) { case '{': { S32 child_count = parseMap(istr, data); if((child_count == PARSE_FAILURE) || data.isUndefined()) { parse_count = PARSE_FAILURE; } else { parse_count += child_count; } if(istr.fail()) { llinfos << "STREAM FAILURE reading map." << llendl; parse_count = PARSE_FAILURE; } break; } case '[': { S32 child_count = parseArray(istr, data); if((child_count == PARSE_FAILURE) || data.isUndefined()) { parse_count = PARSE_FAILURE; } else { parse_count += child_count; } if(istr.fail()) { llinfos << "STREAM FAILURE reading array." << llendl; parse_count = PARSE_FAILURE; } break; } case '!': c = get(istr); data.clear(); break; case '0': c = get(istr); data = false; break; case 'F': case 'f': ignore(istr); c = istr.peek(); if(isalpha(c)) { int cnt = deserialize_boolean( istr, data, NOTATION_FALSE_SERIAL, false); if(PARSE_FAILURE == cnt) parse_count = cnt; else account(cnt); } else { data = false; } if(istr.fail()) { llinfos << "STREAM FAILURE reading boolean." << llendl; parse_count = PARSE_FAILURE; } break; case '1': c = get(istr); data = true; break; case 'T': case 't': ignore(istr); c = istr.peek(); if(isalpha(c)) { int cnt = deserialize_boolean(istr,data,NOTATION_TRUE_SERIAL,true); if(PARSE_FAILURE == cnt) parse_count = cnt; else account(cnt); } else { data = true; } if(istr.fail()) { llinfos << "STREAM FAILURE reading boolean." << llendl; parse_count = PARSE_FAILURE; } break; case 'i': { c = get(istr); S32 integer = 0; istr >> integer; data = integer; if(istr.fail()) { llinfos << "STREAM FAILURE reading integer." << llendl; parse_count = PARSE_FAILURE; } break; } case 'r': { c = get(istr); F64 real = 0.0; istr >> real; data = real; if(istr.fail()) { llinfos << "STREAM FAILURE reading real." << llendl; parse_count = PARSE_FAILURE; } break; } case 'u': { c = get(istr); LLUUID id; istr >> id; data = id; if(istr.fail()) { llinfos << "STREAM FAILURE reading uuid." << llendl; parse_count = PARSE_FAILURE; } break; } case '\"': case '\'': case 's': if(!parseString(istr, data)) { parse_count = PARSE_FAILURE; } if(istr.fail()) { llinfos << "STREAM FAILURE reading string." << llendl; parse_count = PARSE_FAILURE; } break; case 'l': { c = get(istr); // pop the 'l' c = get(istr); // pop the delimiter std::string str; int cnt = deserialize_string_delim(istr, str, c); if(PARSE_FAILURE == cnt) { parse_count = PARSE_FAILURE; } else { data = LLURI(str); account(cnt); } if(istr.fail()) { llinfos << "STREAM FAILURE reading link." << llendl; parse_count = PARSE_FAILURE; } break; } case 'd': { c = get(istr); // pop the 'd' c = get(istr); // pop the delimiter std::string str; int cnt = deserialize_string_delim(istr, str, c); if(PARSE_FAILURE == cnt) { parse_count = PARSE_FAILURE; } else { data = LLDate(str); account(cnt); } if(istr.fail()) { llinfos << "STREAM FAILURE reading date." << llendl; parse_count = PARSE_FAILURE; } break; } case 'b': if(!parseBinary(istr, data)) { parse_count = PARSE_FAILURE; } if(istr.fail()) { llinfos << "STREAM FAILURE reading data." << llendl; parse_count = PARSE_FAILURE; } break; default: parse_count = PARSE_FAILURE; llinfos << "Unrecognized character while parsing: int(" << (int)c << ")" << llendl; break; } if(PARSE_FAILURE == parse_count) { data.clear(); } return parse_count; }
dictionary::dictionary(const std::map<std::string, std::string> &fromMap) : _pimpl(std::make_shared<pimpl_dictionary>()) { parseMap(fromMap); }
com::typesafe::config::Config* com::typesafe::config::ConfigFactory::parseMap(::java::util::Map* values) { clinit(); return parseMap(values, nullptr); }
int main(int argc, char **argv){ tspsMap_t map; tspsConfig_t config; tspsPopulation_t population; unsigned long int numGenerations = 0; int mpiNumProcs = 0; double start, end; //starting MPI directives MPI_Init(NULL,NULL); MPI_Comm_size(MPI_COMM_WORLD,&mpiNumProcs); MPI_Comm_rank(MPI_COMM_WORLD,&mpiId); logg("* Starting tspgen...\n"); // parse the command line args if(readConfig(&config, argc, argv) != TSPS_RC_SUCCESS){ return TSPS_RC_FAILURE; } // parse the map logg("* Parsing map...\n"); if(parseMap(&map) != TSPS_RC_SUCCESS){ logg("Error! Unable to read map 'maps/brazil58.tsp'!\n"); return TSPS_RC_FAILURE; } // initialize random seed: srand ( time(NULL)*mpiId ); logg("* Generating population...\n"); if(generatePopulation(&population, &config) != TSPS_RC_SUCCESS){ logg("Error! Failed to create a new random population!"); return TSPS_RC_FAILURE; } // start a timer (mpi_barrier + mpi_wtime) MPI_Barrier(MPI_COMM_WORLD); start = MPI_Wtime(); logg("* Initializing reproduction loop...\n"); while(1){ numGenerations++; calculateFitnessPopulation(&population, &map); if(numGenerations % 500 == 0){ logg("- Generation %d...\n", numGenerations); printIndividual(&population.individuals[0], "Current Top Individual"); } sortPopulation(&population); if(config.numGenerations > 0 && numGenerations == config.numGenerations){ logg("* Max number of generations [%d] reached!\n", config.numGenerations); break; } crossoverPopulation(&population, &config); mutatePopulation(&population, &config); // migrate population at every n generation if(numGenerations % config.migrationRate == 0){ migrateIndividuals(&population, mpiId, mpiNumProcs, &config); } } // join all the populations joinPopulations(&population, mpiId, mpiNumProcs); sortPopulation(&population); // get the best inidividual and print it printIndividual(&population.individuals[0], "Top Individual"); printIndividual(&population.individuals[config.populationSize-1], "Worst (of the top ones) Individual"); // stop the timer MPI_Barrier(MPI_COMM_WORLD); /* IMPORTANT */ end = MPI_Wtime(); if(mpiId == 0) { /* use time on master node */ printf("* Runtime = %f\n", end-start); } logg("* tspgen finished!\n"); free(population.individuals); free(map.nodes); MPI_Finalize(); return TSPS_RC_SUCCESS; }
int main(int argc, char **argv) { enum GameMode gameMode; struct Windows windows; struct MapList mapList; struct Map *map; struct Dialog dialog[MAX_LEVELS]; struct UnitList inmates, guards; struct UnitNode *unitNode; struct Path path; bool progress[MAX_LEVELS]; int level=0; /* Parse map files */ parseMap(argv[1], &mapList, dialog); /* Create nCurses WINDOWs */ uiInit(&windows); /* Present user with main menu */ gameMode = menuMain(&windows); do{ if(gameMode==EXIT){ break; } else if (gameMode!=NEW){ level = levelSelect(&windows, &mapList, progress); } /* Select current map */ map = &(mapList).level[level]; /* Display intro text */ drawText(&windows, dialog[level], gameMode); /* Initialize game elements */ getGuards(&guards, *map); getPath(&path, *map); inmates.count = 0; inmates.head = NULL; inmates.tail = NULL; /* Draw level */ drawLevel(&windows, map, &guards); /* Prompt user for unit selection */ drawInmateSelection(&windows, map, &inmates, &guards); unitNode = getHead(&inmates); for (int i = 0; i < inmates.count; i++) { ((struct Inmate *) unitNode->unit)->position = path.first->location; unitNode = unitNode->next; } /* Simulate unit interactions */ progress[level] = simulate(&windows, &guards, &inmates, &path); if(progress[level]) gameMode=WIN; else gameMode=LOSE; /* Display outro text */ drawText(&windows, dialog[level], gameMode); } while (level!=EXIT); uiFree(&windows); quit("Thanks for playing.\n"); return 0; }