unsigned int ShaderManager::createProgram(const std::string &vertexShaderFilename, const std::string &geometryShaderFilename, const std::string &fragmentShaderFilename, const std::map<std::string, std::string> &tokens) { unsigned int programID = glCreateProgram(); programs.push_back(programID); //vertex shader const std::string &vertexShaderFileSource = readEntireFile(shadersParentDirectory + shadersDirectoryName + vertexShaderFilename); const std::string &vertexShaderSourceStep1 = tokenReplacerShader.replaceTokens(vertexShaderFileSource, tokens); const std::string &vertexShaderSource = loopUnrollerShader.unrollLoops(vertexShaderSourceStep1); const char *vertexShaderSourceChar = vertexShaderSource.c_str(); unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSourceChar, nullptr); glCompileShader(vertexShader); checkShader(vertexShader, vertexShaderFilename); glAttachShader(programID, vertexShader); //geometry shader if(!geometryShaderFilename.empty()) { const std::string &geometryShaderFileSource = readEntireFile(shadersParentDirectory + shadersDirectoryName + geometryShaderFilename); const std::string &geometryShaderSourceStep1 = tokenReplacerShader.replaceTokens(geometryShaderFileSource, tokens); const std::string &geometryShaderSource = loopUnrollerShader.unrollLoops(geometryShaderSourceStep1); const char *geometryShaderSourceChar = geometryShaderSource.c_str(); unsigned int geometryShader = glCreateShader(GL_GEOMETRY_SHADER); glShaderSource(geometryShader, 1, &geometryShaderSourceChar, nullptr); glCompileShader(geometryShader); checkShader(geometryShader, geometryShaderFilename); glAttachShader(programID, geometryShader); } //fragment shader const std::string &fragmentShaderFileSource = readEntireFile(shadersParentDirectory + shadersDirectoryName + fragmentShaderFilename); const std::string &fragmentShaderSourceStep1 = tokenReplacerShader.replaceTokens(fragmentShaderFileSource, tokens); const std::string &fragmentShaderSource = loopUnrollerShader.unrollLoops(fragmentShaderSourceStep1); const char *fragmentShaderSourceChar = fragmentShaderSource.c_str(); unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSourceChar, nullptr); glCompileShader(fragmentShader); checkShader(fragmentShader, fragmentShaderFilename); glAttachShader(programID, fragmentShader); //link glLinkProgram(programID); checkProgram(programID, vertexShaderFilename + ", " + geometryShaderFilename + ", " + fragmentShaderFilename); return programID; }
Board * Board::fromFile (const char * filename) { size_t boardStringLength; const char * boardString = readEntireFile(filename, boardStringLength); Board * result = Board::fromString(boardString, boardStringLength); delete[] boardString; return result; }
void showStudentTextFile(const std::string& filename, int maxWidth, int maxHeight) { std::string myText = readEntireFile(filename); int height = stringutils::height(myText); if (maxWidth > 0) { myText = stringutils::trimToWidth(myText, maxWidth); } if (maxWidth > 0) { myText = stringutils::trimToHeight(myText, maxHeight); } if (!endsWith(myText, "\n")) { myText += "\n"; } if (STATIC_VARIABLE(FLAGS).graphicalUI) { GOptionPane::showTextFileDialog( myText, ///* title */ "Contents of " + filename + " (" + integerToString(height) + " lines)", /* title */ "Contents of " + filename, maxHeight, maxWidth ); } else { // these begin/end markers are kept to the same width // as AUTOGRADER_OUTPUT_SEPARATOR from autograder.h std::cout << "Here are the contents of the student's " << filename << " file" << " (" << height << " lines):" << std::endl << "============================== BEGIN ===============================" << std::endl << myText << "=============================== END ================================" << std::endl; } }
TextReader::TextReader(const FileProperties& props) :textReader(props.reader) { textReader->setOffset(props.offset); textReader->setSize(props.size); readEntireFile(props.size); }
std::string readEntireFile(const std::string& filename) { std::string out; if (readEntireFile(filename, out)) { return out; } else { error(std::string("input file not found or cannot be opened: ") + filename); return ""; } }
int main(void) { readEntireFile(); printf("\n--------------------------------\n"); readUntilMarker('m'); writeToFile(); return EXIT_SUCCESS; }
GstSDPMessage * SdpEndpointImpl::getSdpPattern () { GstSDPMessage *sdp; GstSDPResult result; boost::filesystem::path sdp_pattern_file; std::unique_lock<std::mutex> lock (sdpMutex); if (pattern) { return pattern.get(); } try { sdp_pattern_file = boost::filesystem::path ( getConfigValue<std::string, SdpEndpoint> ("sdpPattern") ); } catch (boost::property_tree::ptree_error &e) { throw kurento::KurentoException (SDP_CONFIGURATION_ERROR, "Error reading SDP pattern from configuration, please contact the administrator: " + std::string (e.what() ) ); } if (!sdp_pattern_file.is_absolute() ) { try { sdp_pattern_file = boost::filesystem::path ( getConfigValue<std::string, SdpEndpoint> ("configPath") ) / sdp_pattern_file; } catch (boost::property_tree::ptree_error &e) { } } result = gst_sdp_message_new (&sdp); if (result != GST_SDP_OK) { GST_ERROR ("Error creating sdp message"); throw kurento::KurentoException (SDP_CREATE_ERROR, "Error creating SDP pattern"); } pattern = std::shared_ptr<GstSDPMessage> (sdp, gst_sdp_message_free); result = gst_sdp_message_parse_buffer ( (const guint8 *) readEntireFile ( sdp_pattern_file.string() ).c_str(), -1, sdp); if (result != GST_SDP_OK) { GST_ERROR ("Error parsing SDP config pattern"); pattern.reset(); throw kurento::KurentoException (SDP_CONFIGURATION_ERROR, "Error reading SDP pattern from configuration, please contact the administrator"); } return pattern.get(); }
/* * 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; } }
Dictionary::Dictionary (const char * dictionaryPath) : wordCount(0) { // Allocate node 0 to be the root. nodes.reserve(DEFAULT_DICTIONARY_SIZE); // The root node does not actually contain character information, just children. nodes.push_back(Node('\0', false)); const char * dictionaryBuffer; size_t dictionaryLength; dictionaryBuffer = readEntireFile(dictionaryPath, dictionaryLength); try { // Scan through the dictionary file for words and add them to the dictionary. unsigned currentWordStart = 0; for (unsigned i = 0; i < dictionaryLength; i++) { char ch = dictionaryBuffer[i]; if ((ch == '\n') || (ch == '\r') || (ch == '\0')) { size_t currentWordLength = i - currentWordStart; if (currentWordLength) addWord(dictionaryBuffer + currentWordStart, currentWordLength); currentWordStart = i + 1; } } size_t currentWordLength = dictionaryLength - currentWordStart; if ((currentWordLength + currentWordStart <= dictionaryLength) && (currentWordLength)) addWord(dictionaryBuffer + currentWordStart, currentWordLength); delete[] dictionaryBuffer; } catch (...) { delete[] dictionaryBuffer; throw; } }
bool SearchContext::searchFile(int id, const std::string &filename, RegexList &filespecRegexes, pcre *matchRegex) { bool matchesOneFilespec = false; for(RegexList::iterator it = filespecRegexes.begin(); it != filespecRegexes.end(); ++it) { pcre *regex = *it; if(pcre_exec(regex, NULL, filename.c_str(), filename.length(), 0, 0, NULL, 0) >= 0) { matchesOneFilespec = true; break; } } if(!matchesOneFilespec) return false; std::string contents; if(!readEntireFile(filename, contents, params_.maxFileSize)) return false; std::string workBuffer = contents; std::string updatedContents; bool atLeastOneMatch = false; int lineNumber = 1; char *p = &workBuffer[0]; char *line; while((line = nextToken(&p, '\n')) != NULL) { char *originalLine = line; std::string replacedLine; SearchEntry entry; int ovector[100]; do { bool matches = false; int matchPos; int matchLen; if(matchRegex) { int rc; if(rc = pcre_exec(matchRegex, 0, line, strlen(line), 0, 0, ovector, sizeof(ovector)) >= 0) { matches = true; matchPos = ovector[0]; matchLen = ovector[1] - ovector[0]; } } else { char *match; if(params_.flags & SF_MATCH_CASE_SENSITIVE) match = strstr(line, params_.match.c_str()); else match = strstri(line, params_.match.c_str()); if(match != NULL) { matches = true; matchPos = match - line; matchLen = params_.match.length(); } } if(params_.flags & SF_REPLACE) { if(matches) { replacedLine.append(line, matchPos); entry.highlights_.push_back(Highlight(replacedLine.length(), params_.replace.length())); replacedLine.append(params_.replace); line += matchPos + matchLen; } else { break; } } else { if(matches) { entry.filename_ = filename; entry.match_ = originalLine; entry.line_ = lineNumber; entry.highlights_.push_back(Highlight(matchPos + (line - originalLine), matchLen)); line += matchPos + matchLen; } else { break; } } if(matches) hits_++; } while(*line); if(!entry.filename_.empty()) { linesWithHits_++; atLeastOneMatch = true; } if(params_.flags & SF_REPLACE) { if(line && *line) replacedLine += line; if(replacedLine != originalLine) { entry.filename_ = filename; entry.match_ = replacedLine; entry.line_ = lineNumber; append(id, entry); } replacedLine += "\n"; updatedContents += replacedLine; } else { if(!entry.filename_.empty()) append(id, entry); } lineNumber++; } if(atLeastOneMatch) filesWithHits_++; if(params_.flags & SF_REPLACE) { if((contents != updatedContents)) { bool overwriteFile = true; if(params_.flags & SF_BACKUP) { std::string backupFilename = filename; backupFilename += "."; backupFilename += params_.backupExtension; if(!writeEntireFile(backupFilename, contents)) { std::string err = "WARNING: Couldn't write backup file (skipping replacement): "; err += backupFilename; err += "\n"; poke(id, err.c_str(), HighlightList(), 0, false); overwriteFile = false; } } if(overwriteFile) { if(writeEntireFile(filename, updatedContents)) { return true; } else { std::string err = "WARNING: Couldn't write to file: "; err += filename; err += "\n"; poke(id, err.c_str(), HighlightList(), 0, false); } } } return false; } return true; }
bool SearchContext::searchFile(int id, const std::string &filename, RegexList &filespecRegexes, pcre *matchRegex) { bool matchesOneFilespec = false; for(RegexList::iterator it = filespecRegexes.begin(); it != filespecRegexes.end(); ++it) { pcre *regex = *it; if(pcre_exec(regex, NULL, filename.c_str(), filename.length(), 0, 0, NULL, 0) >= 0) { matchesOneFilespec = true; break; } } if(!matchesOneFilespec) return false; std::string contents; if(!readEntireFile(filename, contents, params_.maxFileSize)) return false; std::string workBuffer = contents; std::string updatedContents; std::deque<char *> contextLines; bool atLeastOneMatch = false; int trailingContextLines = 0; int lineNumber = 1; char *p = &workBuffer[0]; char *line; while((line = nextToken(&p, '\n')) != NULL) { char *originalLine = line; std::string replacedLine; SearchEntry entry; int ovector[100]; // Strip newline, but remember exactly what kind it was bool hasCarriageReturn = false; int lineLen = strlen(line); if(lineLen && (line[lineLen - 1] == '\r')) { line[lineLen - 1] = 0; hasCarriageReturn = true; } // Matching loop (we might find our string a few times on a single line) bool lineMatched = false; do { bool matches = false; int matchPos; int matchLen; // The actual match. Either invoke PCRE or do a boring strstr if(matchRegex) { int rc; if(rc = pcre_exec(matchRegex, 0, line, strlen(line), 0, 0, ovector, sizeof(ovector)) >= 0) { matches = true; matchPos = ovector[0]; matchLen = ovector[1] - ovector[0]; } } else { char *match; if(params_.flags & SF_MATCH_CASE_SENSITIVE) match = strstr(line, params_.match.c_str()); else match = strstri(line, params_.match.c_str()); if(match != NULL) { matches = true; matchPos = match - line; matchLen = params_.match.length(); } } if(matches) lineMatched = true; // Handle the match. For replace or find, we: // * Add output explaining the match // * Advance the line pointer for another match attempt // ... or ... // * "break", which leaves the matching loop if(params_.flags & SF_REPLACE) { if(matches) { std::string temp(line, matchPos); replacedLine.append(temp); replacedLine.append(params_.replace); entry.textBlocks.addBlock(temp, config_.textColor_); entry.textBlocks.addBlock(params_.replace, config_.highlightColor_, true); line += matchPos + matchLen; } else { break; } } else { if(matches) { entry.textBlocks.addHighlightedBlock(originalLine, matchPos + (line - originalLine), matchLen, config_.textColor_, config_.highlightColor_, true); line += matchPos + matchLen; } else { break; } } if(matches) hits_++; } while(*line); // end of matching loop // If we're doing a replace, finish the line and append to the final updated contents if(params_.flags & SF_REPLACE) { if(line && *line) { replacedLine += line; entry.textBlocks.addBlock(line, config_.textColor_); } if(hasCarriageReturn) { replacedLine += "\r"; } replacedLine += "\n"; updatedContents += replacedLine; } bool outputMatch = false; if(lineMatched) { // keep stats linesWithHits_++; atLeastOneMatch = true; // If we matched, consider notifying the user. We'll always say something // unless the replaced text doesn't actually change the line. outputMatch = ( !(params_.flags & SF_REPLACE) ) || (replacedLine != originalLine); if(outputMatch) { entry.filename_ = filename; entry.line_ = lineNumber; // output all existing context lines if(contextLines.size()) { SearchEntry contextEntry; contextEntry.filename_ = entry.filename_; contextEntry.contextOnly_ = true; int currLine = entry.line_ - contextLines.size(); for(std::deque<char *>::iterator it = contextLines.begin(); it != contextLines.end(); ++it) { TextBlockList textBlocks; textBlocks.addBlock(*it, config_.textColor_); contextEntry.textBlocks.swap(textBlocks); contextEntry.line_ = currLine++; append(id, contextEntry); } contextLines.clear(); } append(id, entry); } // Remember that we'd like the next few lines, even if they don't match trailingContextLines = config_.contextLines_; } if(!outputMatch) { // Didn't output a match. Keep track or output the line anyway for contextual reasons. if(trailingContextLines > 0) { // A recent match wants to see this line in the output anyway SearchEntry trailingEntry; trailingEntry.filename_ = filename; trailingEntry.line_ = lineNumber; trailingEntry.contextOnly_ = true; trailingEntry.textBlocks.addBlock(originalLine, config_.textColor_); append(id, trailingEntry); trailingContextLines--; } else { // didn't output a match, and wasn't output as context. stash it in contextLines contextLines.push_back(originalLine); if((int)contextLines.size() > config_.contextLines_) contextLines.pop_front(); } } lineNumber++; } // end of line loop (done reading file) if(atLeastOneMatch) filesWithHits_++; if(params_.flags & SF_REPLACE) { if((contents != updatedContents)) { bool overwriteFile = true; if(params_.flags & SF_BACKUP) { std::string backupFilename = filename; backupFilename += "."; backupFilename += params_.backupExtension; if(!writeEntireFile(backupFilename, contents)) { std::string err = "WARNING: Couldn't write backup file (skipping replacement): "; err += backupFilename; err += "\n"; sendError(id, err); overwriteFile = false; } } if(overwriteFile) { if(writeEntireFile(filename, updatedContents)) { return true; } else { std::string err = "WARNING: Couldn't write to file: "; err += filename; err += "\n"; sendError(id, err); } } } return false; } return true; }