int main() { assert(stringContains("abcd", "bad")); assert(!stringContains("abcd", "bce")); assert(stringContains("abcd", "aa")); assert(!stringContains("", "a")); assert(stringContains("a", "")); return 0; }
/* * student_submission_time: 13/Oct/2014 10:31:15 * assignment_due_time: 13/Oct/2014 23:59:00 * calendar_days_late: 0 */ void showLateDays(const std::string& filename) { Map<std::string, std::string> lineMap; std::string text; if (fileExists(filename)) { text = readEntireFile(filename); } else { text = std::string("student_submission_time: unknown\n") + "assignment_due_time: unknown\n" + "calendar_days_late: unknown\n" + "details: " + filename + " not found!"; } if (STATIC_VARIABLE(FLAGS).graphicalUI) { for (std::string line : stringSplit(text, "\n")) { if (stringContains(line, ": ")) { std::string key = line.substr(0, line.find(": ")); std::string value = line.substr(line.find(": ") + 2); lineMap.put(key, value); } } std::string message; std::string dueTime = lineMap["assignment_due_time"]; std::string submitTime = lineMap["student_submission_time"]; std::string daysLate = lineMap["calendar_days_late"]; std::string details = lineMap["details"]; if (dueTime != "unknown") { dueTime = formatDate(dueTime); } if (submitTime != "unknown") { submitTime = formatDate(submitTime); } message += "<html><table>"; if (!details.empty()) { message += "<tr><td><b>NOTE:</b></td><td>" + details + "</td></tr>"; } message += "<tr><td><b>due</b></td><td>" + dueTime + "</td></tr>"; message += "<tr><td><b>submitted</b></td><td>" + submitTime + "</td></tr>"; message += "<tr><td><b>cal.days late</b></td><td>" + daysLate + "</td></tr>"; message += "</table></html>"; GOptionPane::showMessageDialog(message, filename, GOptionPane::PLAIN); } else { std::cout << AUTOGRADER_OUTPUT_SEPARATOR << std::endl; std::cout << "Contents of " << filename << ":" << std::endl; std::cout << text; if (!endsWith(text, '\n')) { std::cout << std::endl; } std::cout << AUTOGRADER_OUTPUT_SEPARATOR << std::endl; } }
int main(void) { if(stringContains("hello world", "hello") == true) { printf("%s\n", "matched"); } else { printf("%s\n", "failed to match"); } //char *str = stripLettersFromFloat("asdaksjhdk0.567810273123asdjkhas"); //printf("%s\n", str); //free(str); return 0; }
void HTTPHeader::consumeLine(char* line) { int nTokens; char** tokens = stringSplit(line, " :", &nTokens); if (strcmp(tokens[0], "GET")==0 || strcmp(tokens[0], "PUT")==0 || strcmp(tokens[0], "POST")==0 || strcmp(tokens[0], "DELETE")==0) { if (strcmp(tokens[0], "GET")==0) this->verb = GET; else if (strcmp(tokens[0], "PUT")==0) this->verb = PUT; else if (strcmp(tokens[0], "POST")==0) this->verb = POST; else if(strcmp(tokens[0], "DELETE")==0) this->verb = DELETE; if (stringContains(tokens[1], '?')) { int nQTokens; char** qTokens = stringSplit(tokens[1], "?&", &nQTokens); for (int i = 0; i < nQTokens; i++) { int nElements; char** qElements = stringSplit(qTokens[i], "=", &nElements); if (nElements == 2 && nPairs < MAX_PAIRS) { strcpy(this->pairs[nPairs].key, qElements[0]); strcpy(this->pairs[nPairs++].value, qElements[1]); } free(qElements); } free(qTokens); } strcpy(this->resource, tokens[1]); } else if (strcmp(tokens[0], "Content-Length") == 0) { this->contentLength = atoi(tokens[1]); } free(tokens); }
int autograderGraphicalMain(int argc, char** argv) { GWindow gui(500, 300, /* visible */ false); gui.setTitle(STATIC_VARIABLE(FLAGS).assignmentName + " Autograder"); gui.setCanvasSize(0, 0); gui.setExitOnClose(true); GLabel startLabel(""); if (!STATIC_VARIABLE(FLAGS).startMessage.empty()) { std::string startMessage = STATIC_VARIABLE(FLAGS).startMessage; if (!stringContains(startMessage, "<html>")) { startMessage = stringReplace(startMessage, "Note:", "<b>Note:</b>"); startMessage = stringReplace(startMessage, "NOTE:", "<b>NOTE:</b>"); startMessage = "<html><body style='width: 500px; max-width: 500px;'>" + startMessage + "</body></html>"; } startLabel.setLabel(startMessage); gui.addToRegion(&startLabel, "NORTH"); } std::string autogradeText = addAutograderButton(gui, "Automated\ntests", "check.gif"); std::string manualText = addAutograderButton(gui, "Run\nmanually", "play.gif"); std::string styleCheckText = addAutograderButton(gui, "Style\nchecker", "magnifier.gif"); for (int i = 0; i < STATIC_VARIABLE(FLAGS).callbackButtons.size(); i++) { STATIC_VARIABLE(FLAGS).callbackButtons[i].text = addAutograderButton(gui, STATIC_VARIABLE(FLAGS).callbackButtons[i].text, STATIC_VARIABLE(FLAGS).callbackButtons[i].icon); } std::string lateDayText = addAutograderButton(gui, "Late days\ninfo", "calendar.gif"); std::string aboutText = addAutograderButton(gui, "About\nGrader", "help.gif"); std::string exitText = addAutograderButton(gui, "Exit\nGrader", "stop.gif"); gui.pack(); gui.setVisible(true); int result = 0; while (true) { GEvent event = waitForEvent(ACTION_EVENT); if (event.getEventClass() == ACTION_EVENT) { GActionEvent actionEvent(event); std::string cmd = actionEvent.getActionCommand(); if (cmd == autogradeText) { if (STATIC_VARIABLE(FLAGS).callbackStart) { STATIC_VARIABLE(FLAGS).callbackStart(); } // stanfordcpplib::getPlatform()->autograderunittest_clearTests(); stanfordcpplib::getPlatform()->autograderunittest_clearTestResults(); stanfordcpplib::getPlatform()->autograderunittest_setTestingCompleted(false); stanfordcpplib::getPlatform()->autograderunittest_setVisible(true); result = mainRunAutograderTestCases(argc, argv); stanfordcpplib::getPlatform()->autograderunittest_setTestingCompleted(true); // if style checker is merged, also run it now if (stylecheck::isStyleCheckMergedWithUnitTests()) { mainRunStyleChecker(); } if (STATIC_VARIABLE(FLAGS).callbackEnd) { STATIC_VARIABLE(FLAGS).callbackEnd(); } } else if (cmd == manualText) { // set up buttons to automatically enter user input if (STATIC_VARIABLE(FLAGS).showInputPanel) { inputpanel::load(STATIC_VARIABLE(FLAGS).inputPanelFilename); } // actually run the student's program // (While program is running, if we close console, exit entire // autograder program because we might be blocked on console I/O. // But after it's done running, set behavior to just hide the // console, since the grader will probably try to close it and then // proceed with more grading and tests afterward. // A little wonky, but it avoids most of the surprise cases of // "I closed the student's console and it killed the autograder". stanfordcpplib::getPlatform()->jbeconsole_clear(); stanfordcpplib::getPlatform()->jbeconsole_setVisible(true); stanfordcpplib::getPlatform()->jbeconsole_toFront(); // setConsoleCloseOperation(ConsoleCloseOperation::CONSOLE_EXIT_ON_CLOSE); autograder::setExitEnabled(false); // block exit() call setConsoleCloseOperation(ConsoleCloseOperation::CONSOLE_HIDE_ON_CLOSE); studentMain(); // gwindowSetExitGraphicsEnabled(true); } else if (cmd == styleCheckText) { mainRunStyleChecker(); } else if (cmd == lateDayText) { showLateDays(); } else if (cmd == aboutText) { GOptionPane::showMessageDialog(STATIC_VARIABLE(FLAGS).aboutText, "About Autograder", GOptionPane::MessageType::INFORMATION); } else if (cmd == exitText) { autograder::setExitEnabled(true); // don't block exit() call // free up memory used by graphical interactors for (GButton* button : STATIC_VARIABLE(AUTOGRADER_BUTTONS)) { delete button; } STATIC_VARIABLE(AUTOGRADER_BUTTONS).clear(); gui.close(); // exits program; will not return break; } else { for (CallbackButtonInfo buttonInfo : STATIC_VARIABLE(FLAGS).callbackButtons) { if (cmd == buttonInfo.text) { buttonInfo.func(); break; } } } } } // free up memory used by graphical interactors for (GButton* button : STATIC_VARIABLE(AUTOGRADER_BUTTONS)) { delete button; } STATIC_VARIABLE(AUTOGRADER_BUTTONS).clear(); return result; }
void printStackTrace(std::ostream& out) { // constructing the following object jumps into fancy code in call_stack_gcc/windows.cpp // to rebuild the stack trace; implementation differs for each operating system stacktrace::call_stack trace; std::vector<stacktrace::entry> entries = trace.stack; // get longest line string length to line up stack traces void* fakeStackPtr = stacktrace::getFakeCallStackPointer(); int entriesToShowCount = 0; int funcNameLength = 0; int lineStrLength = 0; for (size_t i = 0; i < entries.size(); ++i) { // remove references to std:: namespace stringReplaceInPlace(entries[i].function, "std::", ""); // remove template arguments while (stringContains(entries[i].function, "<") && stringContains(entries[i].function, ">")) { int lessThan = stringIndexOf(entries[i].function, "<"); int greaterThan = stringIndexOf(entries[i].function, ">", lessThan); if (lessThan >= 0 && greaterThan > lessThan) { entries[i].function.erase(lessThan, greaterThan - lessThan + 1); } } if (!STACK_TRACE_SHOULD_FILTER || !shouldFilterOutFromStackTrace(entries[i].function)) { lineStrLength = std::max(lineStrLength, (int) entries[i].lineStr.length()); funcNameLength = std::max(funcNameLength, (int) entries[i].function.length()); entriesToShowCount++; } } if (entries.empty() || entriesToShowCount == 0) { return; // couldn't get a stack trace, or had no useful data :-( } if (lineStrLength > 0) { out << " *** Stack trace (line numbers are approximate):" << std::endl; if (STACK_TRACE_SHOW_TOP_BOTTOM_BARS) { out << " *** " << std::setw(lineStrLength) << std::left << "file:line" << " " << "function" << std::endl; out << " *** " << std::string(lineStrLength + 2 + funcNameLength, '=') << std::endl; } } else { out << " *** Stack trace:" << std::endl; } for (size_t i = 0; i < entries.size(); ++i) { stacktrace::entry entry = entries[i]; entry.file = getTail(entry.file); // skip certain entries for clarity if (STACK_TRACE_SHOULD_FILTER && shouldFilterOutFromStackTrace(entry.function)) { continue; } // show Main() as main() to hide hidden case-change by Stanford C++ lib internals if (startsWith(entry.function, "Main(")) { entry.function.replace(0, 5, "main("); } std::string lineStr = ""; if (!entry.lineStr.empty()) { lineStr = entry.lineStr; } else if (entry.line > 0) { lineStr = "line " + integerToString(entry.line); } out << " *** " << std::left << std::setw(lineStrLength) << lineStr << " " << entry.function << std::endl; // don't show entries beneath the student's main() function, for simplicity if (entry.function == "main()") { break; } } if (entries.size() == 1 && entries[0].address == fakeStackPtr) { out << " *** (partial stack due to crash)" << std::endl; } if (STACK_TRACE_SHOW_TOP_BOTTOM_BARS && lineStrLength > 0) { out << " *** " << std::string(lineStrLength + 2 + funcNameLength, '=') << std::endl; } // out << " ***" << std::endl; // out << " *** NOTE:" << std::endl; // out << " *** Any line numbers listed above are approximate." << std::endl; // out << " *** To learn more about why the program crashed, we" << std::endl; // out << " *** suggest running your program under the debugger." << std::endl; out << " ***" << std::endl; }
int main(int argc, char** argv) { NPCORE_initialize(); NPCVTests testChoosed = ALL; char *input = strmakeN("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); while (stringCompare(input, "quit") != 0) { // // MAIN LOOP printMainMessage(); //get input scanf("%s", input); //TODO: move this to function //choose example if (stringContains(input, "all") != NULL) testChoosed = ALL; else if (stringContains(input, "list") != NULL) testChoosed = LIST; else if (stringContains(input, "time") != NULL) testChoosed = TIME; else if (stringContains(input, "filewrite") != NULL) testChoosed = FILE_W; else if (stringContains(input, "imagereadwrite") != NULL) testChoosed = IMAGE_RW; else if (stringContains(input, "filter") != NULL) testChoosed = FILTER; else if (stringContains(input, "gray") != NULL) testChoosed = GRAY; else if (stringContains(input, "subimage") != NULL) testChoosed = SUBIMAGE; else if (stringContains(input, "edges") != NULL) testChoosed = EDGES; else if (stringContains(input, "csfocr") != NULL) testChoosed = CLASSIFYOCR; else { NConsolePrint("\nWrong command.."); continue; } //process if (testChoosed == ALL) { List_Example(); List_Example(); Time_Example(); Time_Example(); FileWrite_Example(); FileWrite_Example(); ImageReadWrite_Example(); ImageReadWrite_Example(); ImageGray_Example(); ImageGray_Example(); Filter_Example(); Filter_Example(); Subarea_Example(); Subarea_Example(); EdgeDetection_Examples(); EdgeDetection_Examples(); ClassifyOcrSamples_Example(); ClassifyOcrSamples_Example(); } else if (testChoosed == LIST) { List_Example(); } else if (testChoosed == TIME) { Time_Example(); } else if (testChoosed == FILE_W) { FileWrite_Example(); } else if (testChoosed == IMAGE_RW) { ImageReadWrite_Example(); } else if (testChoosed == GRAY) { ImageGray_Example(); } else if (testChoosed == FILTER) { Filter_Example(); } else if (testChoosed == SUBIMAGE) { Subarea_Example(); } else if (testChoosed == EDGES) { EdgeDetection_Examples(); } else if (testChoosed == CLASSIFYOCR) { ClassifyOcrSamples_Example(); } } freeN(input); //system("pause"); NPCORE_exit(); return 1; }