// Initializes the board by getting the puzzle from a csv file. // 0 means empty (an example of a row: 0,0,1,0,0,2,0,0,3) void Board::read(string filename) { std::ifstream inFile(filename.c_str(), std::ios::in); // If the file does not exist, throw an error. if (!inFile) throw NoInputFile(); // Temporary strings to read input. string temp; vector<string> lines; // Read all lines first and store them in an array, while (getline(inFile, temp)) lines.push_back(temp); if (lines.size() != size) throw BadInput(); // Wrong row counts. // For each line, extract values using string streams and set values. for (short i = 0; i < size; i++) { std::istringstream ss(lines[i]); vector<unsigned char> values; while (getline(ss, temp, ',')) values.push_back(std::atoi(temp.c_str())); // Check for wrong col counts. if (values.size() != size) throw BadInput(); for (short j = 0; j < size; j++) { if (values[j] < 0 || values[j] > size) throw InvalidPuzzle(); // Bad value is given. set(i, j, values[j]); }; }; inFile.close(); // Check for the validity of the given puzzle. if (!ifValid()) throw InvalidPuzzle(); };
Map* MapLoader::loadMap(const string& mapFileName, const string& constrFileName) { ifstream mapFile(mapFileName); LoadableLocationSet locations; LoadableReaderSet readers; NameIdMap locationsMap; string line; if (mapFile.is_open()) { unsigned int lId = 0, rId = 0; while (getline(mapFile, line)) { Util::trim(line); // Jump if blank line or comment (starts with '#') if (line.length() == 0 || line[0] == '#') continue; vector<string> v = Util::split(line, SEP); if (v.size() == 5) { // Location line string name = v[0]; double x = stod(v[1]), y = stod(v[2]), w = stod(v[3]), h = stod(v[4]); locationsMap.insert(NameIdPair(name, lId)); locations.push_back(Location(lId++, name, x, y, w, h)); } else if (v.size() == 3) { // Reader line string name = v[0]; double x = stod(v[1]), y = stod(v[2]); readers.push_back(Reader(rId++, name, x, y)); } else throw BadInput(); } } else throw BadInput(); Map* map = Map::createMap<LoadableLocationSet,LoadableReaderSet>(locations, readers); ifstream constrFile(constrFileName); if (constrFile.is_open()) { while (getline(constrFile, line)) { Util::trim(line); // Jump if blank line or comment (starts with '#') if (line.length() == 0 || line[0] == '#') continue; vector<string> v = Util::split(line, CSEP); if (v[0] == "DR") { // Direct Reachability Constraint unsigned int id1 = locationsMap.at(v[1]), id2 = locationsMap.at(v[2]); map->setDR(id1, id2); } else if (v[0] == "DU") { // Direct Unreachability Constraint unsigned int id1 = locationsMap.at(v[1]), id2 = locationsMap.at(v[2]); map->setDU(id1, id2); } else if (v[0] == "LT") { // Latency Constraint unsigned int id = locationsMap.at(v[1]), latency = stoi(v[2]); map->setLT(id, latency); } else if (v[0] == "TT") { // Traveling Time Constraint unsigned int id1 = locationsMap.at(v[1]), id2 = locationsMap.at(v[2]), travelingTime = stoi(v[3]); map->setTT(id1, id2, travelingTime); } else throw BadInput(); } } return map; }
size_t IconvWrapper::RequiredBufferSize(const char* src, size_t srcLen) { char buff[512]; size_t charsWritten = 0; size_t res; do { char* dst = buff; size_t dstSize = sizeof(buff); res = conv->Convert(&src, &srcLen, &dst, &dstSize); conv->Convert(nullptr, nullptr, &dst, &dstSize); charsWritten += dst - buff; } while (res == iconv_failed && errno == E2BIG); if (res == iconv_failed) { switch (errno) { case EINVAL: throw BadInput( "One or more characters in the input string were not valid " "characters in the given input encoding"); case EILSEQ: throw BadOutput( "One or more characters could not be converted to the " "selected target encoding and the version of iconv " "Aegisub was built with does not have useful fallbacks. " "For best results, please build Aegisub using a recent " "version of GNU iconv."); default: throw ConversionFailure("An unknown conversion failure occured"); } } return charsWritten; }
size_t IconvWrapper::Convert(const char* source, size_t sourceSize, char *dest, size_t destSize) { if (sourceSize == (size_t)-1) sourceSize = SrcStrLen(source); size_t res = conv->Convert(&source, &sourceSize, &dest, &destSize); if (res == 0) res = conv->Convert(nullptr, nullptr, &dest, &destSize); if (res == iconv_failed) { switch (errno) { case E2BIG: throw BufferTooSmall( "Destination buffer was not large enough to fit converted " "string."); case EINVAL: throw BadInput( "One or more characters in the input string were not valid " "characters in the given input encoding"); case EILSEQ: throw BadOutput( "One or more characters could not be converted to the " "selected target encoding and the version of iconv " "Aegisub was built with does not have useful fallbacks. " "For best results, please build Aegisub using a recent " "version of GNU iconv."); default: throw ConversionFailure("An unknown conversion failure occurred"); } } return res; }
void IconvWrapper::Convert(std::string const& source, std::string &dest) { char buff[512]; const char *src = source.data(); size_t srcLen = source.size(); size_t res; do { char *dst = buff; size_t dstLen = sizeof(buff); res = conv->Convert(&src, &srcLen, &dst, &dstLen); if (res == 0) conv->Convert(nullptr, nullptr, &dst, &dstLen); dest.append(buff, sizeof(buff) - dstLen); } while (res == iconv_failed && errno == E2BIG); if (res == iconv_failed) { switch (errno) { case EINVAL: throw BadInput( "One or more characters in the input string were not valid " "characters in the given input encoding"); case EILSEQ: throw BadOutput( "One or more characters could not be converted to the " "selected target encoding and the version of iconv " "Aegisub was built with does not have useful fallbacks. " "For best results, please build Aegisub using a recent " "version of GNU iconv."); default: throw ConversionFailure("An unknown conversion failure occurred"); } } }
size_t IconvWrapper::RequiredBufferSize(const char* src, size_t srcLen) { char buff[512]; size_t charsWritten = 0; size_t res; do { char* dst = buff; size_t dstSize = sizeof(buff); res = conv->Convert(&src, &srcLen, &dst, &dstSize); conv->Convert(nullptr, nullptr, &dst, &dstSize); charsWritten += dst - buff; } while (res == iconv_failed && errno == E2BIG); if (res == iconv_failed) { switch (errno) { case EINVAL: case EILSEQ: throw BadInput( "One or more characters in the input string were not valid " "characters in the given input encoding"); default: throw ConversionFailure("An unknown conversion failure occurred"); } } return charsWritten; }
void IconvWrapper::Convert(const char *src, size_t srcLen, std::string &dest) { char buff[512]; size_t res; do { char *dst = buff; size_t dstLen = sizeof(buff); res = conv->Convert(&src, &srcLen, &dst, &dstLen); if (res == 0) conv->Convert(nullptr, nullptr, &dst, &dstLen); dest.append(buff, sizeof(buff) - dstLen); } while (res == iconv_failed && errno == E2BIG); if (res == iconv_failed) { switch (errno) { case EILSEQ: case EINVAL: throw BadInput( "One or more characters in the input string were not valid " "characters in the given input encoding"); default: throw ConversionFailure("An unknown conversion failure occurred"); } } }
char * foo_i::foo_op (const char * inarg) { CORBA::String_var retval = CORBA::string_dup ("bad"); if (ACE_OS::strcmp (inarg, "foo_op") == 0) { retval = CORBA::string_dup ("good"); } else { throw BadInput ("expected \"foo_op\"\n"); } return retval._retn (); }
size_t IconvWrapper::Convert(const char* source, size_t sourceSize, char *dest, size_t destSize) { if (sourceSize == (size_t)-1) sourceSize = SrcStrLen(source); size_t res = conv->Convert(&source, &sourceSize, &dest, &destSize); if (res == 0) res = conv->Convert(nullptr, nullptr, &dest, &destSize); if (res == iconv_failed) { switch (errno) { case E2BIG: throw BufferTooSmall( "Destination buffer was not large enough to fit converted " "string."); case EINVAL: case EILSEQ: throw BadInput( "One or more characters in the input string were not valid " "characters in the given input encoding"); default: throw ConversionFailure("An unknown conversion failure occurred"); } } return res; }
NS_IMETHODIMP ValidityState::GetBadInput(bool* aBadInput) { *aBadInput = BadInput(); return NS_OK; }
void CEDScriptSelectorDlg::EventHandler(CGUIEvent *event) { if(!event) return; CGUIEventProgram *ep; CGUIButton *bt; CGUIEditWindow *eb; ep = nameServer->ResolveID(event->sender); //----------------------------------------------------------------------------------- eb = dynamic_cast<CGUIEditWindow*>(ep); //editboxy if(eb && event->eventID == EOk){ if(eb == edScript) { int ret=0; if(eb->Sync(1)==0) { char buf[5]; FS->ChangeDir("$GAME$"); ret = FS->ReadFile(scriptName,buf,4); if(ret) { // kontrola typu souboru dle pripony } } if(!ret) { eb->BadInput(); BadInput(event->sender); } } else if(eb == edLevel) { int ret=0; if(eb->Sync(1)==0) { ret = FS->IsValidFilename(levelName); } if(!ret) { eb->BadInput(); BadInput(event->sender); } } //else if(eb == edLevelPath) //{ // int ret=0; // if(eb->Sync(1)==0) // { // FS->ChangeDir("$GAME$"); // if(levelPath[0]==0) // ret=1; // else // ret = FS->ChangeDir(levelPath); // } // if(!ret) // { // eb->BadInput(); // BadInput(event->sender); // } //} } //----------------------------------------------------------------------------------- bt = dynamic_cast<CGUIButton*>(ep); //buttony if(bt) { if(event->eventID == EClicked) { if(bt == bScriptSelector) //vyber skriptu { CEDFileSelector *fs = dynamic_cast<CEDFileSelector*>(nameServer->ResolveID(fsScriptSelector)); if(fs) { fs->FocusMe(); //uz existuje FileSelector se skripty -> jen ho nafocusuju } else { float bx=0,by=0; desktop->GetDesktopPos(bScriptSelector,bx,by); //zjistim souradnice tlacitka "..." v desktopu fs = new CEDFileSelector(bx,by,300,500); fs->title->SetText("en{{Select Script:}}cs{{Vyber script}}"); fs->title->SetIcon(new CGUIRectHost(0,0,styleSet->Get("FileBrowser")->GetTexture(0))); fs->compareFunction=&CEDFileSelector::CompareLevelDIR; fs->SetCompareFunctionSubTree(&CEDFileSelector::CompareLevelDIR); fs->title->SetButtons(true,false,false); fs->filterExt = eEXTscript; fs->AcceptEvent(GetID(),ETree); desktop->AddBackElem(fs); fs->SetModal(1); //fs->FocusMe(); fsScriptSelector = fs->GetID(); } } //else if(bt == bLevelSelector) //vyber levelu - cesty + eventualne i jmena //{ // CEDFileSelector *fs = dynamic_cast<CEDFileSelector*>(nameServer->ResolveID(fsLevelSelector)); // if(fs) // { // fs->FocusMe(); //uz existuje FileSelector se skripty -> jen ho nafocusuju // } // else // { // float bx=0,by=0; // desktop->GetDesktopPos(bLevelSelector,bx,by); //zjistim souradnice tlacitka "..." v desktopu // // fs = new CEDFileSelector(bx,by,300,500); // fs->title->SetText("Select LevelPath (name):"); // fs->title->SetIcon(new CGUIRectHost(0,0,styleSet->Get("FileBrowser")->GetTexture(0))); // fs->title->SetButtons(true,false,false); // fs->filterExt = eEXTlevel; // fs->AcceptEvent(GetID(),ETree); // CGUIButton* ok = new CGUIButton(75,10,50,25,"GUI.But.Std","OK"); // ok->AcceptEvent(GetID(),EClicked); // fsLevelSelectorButton=ok->GetID(); // fs->treeItemStartY = 50; // fs->AddBackElem(ok); // ok->Center(true,false); // desktop->AddBackElem(fs); // fs->SetModal(1); // //fs->FocusMe(); // fsLevelSelector = fs->GetID(); // } //} //else if(event->sender == fsLevelSelectorButton) //{ // zmacknul se OK button v LevelSelectoru // CEDFileSelector *fs = dynamic_cast<CEDFileSelector*>(nameServer->ResolveID(fsLevelSelector)); // if(fs) // { // fs->CloseWindow(); // fsLevelSelectorButton=0; // FocusMe(); // SetFocusEl(FindNextTABElement(edLevelPath->GetTabOrder(),false),0); // } //} } else if(event->eventID == EStateChanged) { CGUIEditWindow *ew = GetObjFromID<CGUIEditWindow>(IDbadElem); if((ew==edScript && bt==bScriptSelector)) { int cs = bt->GetState(); if(cs == 2) canceling = 1;//cancel je zamacklej else { if(IDbadElem){ CGUIElement *el = dynamic_cast<CGUIElement*>(nameServer->ResolveID(IDbadElem)); if(el) SetFocusEl(el,0); IDbadElem = 0; //badelfoc=0; } canceling = 0; } } } } //------------------------------------------------------------------------------------ if(event->eventID == ETree) { if(event->sender == fsScriptSelector && event->pInt2 == 0) //obsluha vyberu jmena scriptu { CEDFileSelector *fs = dynamic_cast<CEDFileSelector*>(nameServer->ResolveID(fsScriptSelector)); if(fs) { char *dir=0,*file=0,*root=0; int sf; sf = fs->GetSelectedFileRelativ(event->pID,&root,&dir,&file); if(sf==0) //vybral se soubor { if(dir[0]!=0) _snprintf(scriptName,STRINGLEN,"%s\\%s",dir,file); else _snprintf(scriptName,STRINGLEN,"%s",file); edScript->Sync(false); IDbadElem = 0; fs->CloseWindow(); FocusMe(); SetFocusEl(FindNextTABElement(bScriptSelector->GetTabOrder(),false),0); } SAFE_DELETE_ARRAY(root); SAFE_DELETE_ARRAY(dir); SAFE_DELETE_ARRAY(file); } } //else if(event->sender == fsLevelSelector && event->pInt2 == 0) //obsluha vyberu jmena Levelu / cesty k Levelu //{ // CEDFileSelector *fs = dynamic_cast<CEDFileSelector*>(nameServer->ResolveID(fsLevelSelector)); // if(fs) // { // char *dir=0,*file=0,*root=0; // int sf; // sf = fs->GetSelectedFileRelativ(event->pID,&root,&dir,&file); // if(!sf) // assert(false); // neni to adresar - chyba, ve vyberu maji byt pouze adresare // // _snprintf(levelPath,STRINGLEN,"%s",dir); // int ext; // KerServices.ConvertFileNameToShort(file,ext,0,0); // if(ext==eEXTlevel) // { // _snprintf(levelName,STRINGLEN,"%s",file); // edLevel->Sync(false); // //IDbadElem = 0; // fs->CloseWindow(); // FocusMe(); // SetFocusEl(FindNextTABElement(edLevelPath->GetTabOrder(),false),0); // } // else // { // if(dir[0]==0) // _snprintf(levelPath,STRINGLEN,"%s",file); // else // _snprintf(levelPath,STRINGLEN,"%s\\%s",dir,file); // } // edLevelPath->Sync(false); // IDbadElem = 0; // SAFE_DELETE_ARRAY(root); // SAFE_DELETE_ARRAY(dir); // SAFE_DELETE_ARRAY(file); // } //} } //------------------------------------------------------------------------------------ CGUIDlgOkCancel::EventHandler(event); //zavolam eventhander predka }
void CEDAutoSelectFileDlg::EventHandler(CGUIEvent *event) { if(!event) return; CGUIEventProgram *ep; CGUIButton *bt; CGUIEditWindow *eb; ep = nameServer->ResolveID(event->sender); //----------------------------------------------------------------------------------- eb = dynamic_cast<CGUIEditWindow*>(ep); //editboxy if(eb && event->eventID == EOk){ if(eb == edA) { int ret=0; if(eb->Sync(1)==0) { ret = FS->IsValidFilename(AName); } if(!ret) { eb->BadInput(); BadInput(event->sender); } } else if(eb == edAPath) { int ret=0; if(eb->Sync(1)==0) { FS->ChangeDir("$GAME$"); if(APath[0]==0) ret=1; else ret = FS->ChangeDir(APath); } if(!ret) { eb->BadInput(); BadInput(event->sender); } } } //----------------------------------------------------------------------------------- bt = dynamic_cast<CGUIButton*>(ep); //buttony if(bt) { if(event->eventID == EClicked) { if(bt == bASelector) //vyber levelu - cesty + eventualne i jmena { CEDFileSelector *fs = dynamic_cast<CEDFileSelector*>(nameServer->ResolveID(fsASelector)); if(fs) { fs->FocusMe(); //uz existuje FileSelector se skripty -> jen ho nafocusuju } else { float bx=0,by=0; desktop->GetDesktopPos(bASelector,bx,by); //zjistim souradnice tlacitka "..." v desktopu fs = new CEDFileSelector(bx,by,300,500); fs->title->SetText("Select A file (dir):"); fs->title->SetIcon(new CGUIRectHost(0,0,styleSet->Get("BUT_Save_Up")->GetTexture(0))); fs->title->SetButtons(true,false,false); fs->compareFunction=&(CEDFileSelector::CompareLevelDIR); fs->SetCompareFunctionSubTree(&(CEDFileSelector::CompareLevelDIR)); fs->filterExt = eEXTauto; fs->AcceptEvent(GetID(),ETree); desktop->AddBackElem(fs); fs->SetModal(1); fsASelector = fs->GetID(); } } } else if(event->eventID == EStateChanged) { CGUIEditWindow *ew = GetObjFromID<CGUIEditWindow>(IDbadElem); if(ew==edA && bt==bASelector) { int cs = bt->GetState(); if(cs == 2) canceling = 1;//cancel je zamacklej else { if(IDbadElem){ CGUIElement *el = dynamic_cast<CGUIElement*>(nameServer->ResolveID(IDbadElem)); if(el) SetFocusEl(el,0); IDbadElem = 0; } canceling = 0; } } } } //------------------------------------------------------------------------------------ if(event->eventID == ETree) { if(event->sender == fsASelector && event->pInt2 == 0) //obsluha vyberu jmena Levelu / cesty k Levelu { CEDFileSelector *fs = dynamic_cast<CEDFileSelector*>(nameServer->ResolveID(fsASelector)); if(fs) { char *dir=0,*file=0,*root=0; int sf; sf = fs->GetSelectedFileRelativ(event->pID,&root,&dir,&file); assert(sf!=-1); _snprintf(APath,STRINGLEN,"%s",dir); int ext; KerServices.ConvertFileNameToShort(file,ext,0,0); if(sf==1) // jde o adresar { if(dir[0]==0) _snprintf(APath,STRINGLEN,"%s",file); else _snprintf(APath,STRINGLEN,"%s\\%s",dir,file); } if(ext==eEXTauto) { _snprintf(AName,STRINGLEN,"%s",file); edA->Sync(false); fs->CloseWindow(); FocusMe(); SetFocusEl(FindNextTABElement(edAPath->GetTabOrder(),false),0); } edAPath->Sync(false); IDbadElem = 0; SAFE_DELETE_ARRAY(root); SAFE_DELETE_ARRAY(dir); SAFE_DELETE_ARRAY(file); } } } //------------------------------------------------------------------------------------ CGUIDlgOkCancel::EventHandler(event); //zavolam eventhander predka }
int main(int argc, char *argv[]) { // Process args std::string output; std::vector<std::string> list_inputs; bool capture_inputs=false; bool capture_output=false; for(int i=1; i < argc; i++) { std::string const arg(argv[i]); if(arg == "-i") { capture_inputs = true; capture_output = false; continue; } else if(arg == "-o") { capture_inputs = false; capture_output = true; continue; } if(capture_inputs) { list_inputs.push_back(arg); } else if(capture_output) { output = arg; } } if(output.empty() || list_inputs.empty()) { BadInput(); return -1; } // Populate the LUT to convert ubytes to string rep for(size_t i=0; i < g_lut_ubyte_to_hex.size(); i++) { std::stringstream ss_to_hex; ss_to_hex << std::hex << i; g_lut_ubyte_to_hex[i] = std::string("0x"+ss_to_hex.str()); } std::string cpp_source; cpp_source.append(g_respk_file_header); for(auto const &input : list_inputs) { if(!GenerateSourceFromResourceDesc(input,cpp_source)) { return -1; } } // Create the source file std::ofstream cpp_file; cpp_file.open(output); if(!cpp_file) { std::cout << "respk: ERROR: failed to open output file: " << std::endl; std::cout << output << std::endl; return -1; } cpp_file << cpp_source; cpp_file.close(); return 0; }