// function to reload an index from a file, re-create the index structure for it, and output it to a new file INVERTED_INDEX* reloadIndex(char* dirname, char* filename, INVERTED_INDEX* index) { index = InitIndex(); // strings to build up the full path char* path; path = (char*)malloc(sizeof(char)*1000); BZERO(path, 1000); strcpy(path, dirname); strcat(path, "/"); strcat(path, filename); FILE* indexFile; indexFile = fopen(path, "r"); if (indexFile == NULL) { printf("Error opening [%s] to RELOAD.\n", path); } char* buffer; buffer = (char*)malloc(sizeof(char)*20000); BZERO(buffer, 20000); // get each line from the index file and process it while(1) { if(fgets(buffer, 20000, indexFile) == NULL) { break; } else { removeNewLines(buffer); // variables to store parsed elements: word, # of docs, pairs of docID's and frequency char temp[20000]; strcpy(temp, buffer); // free(buffer); char* word; word = strtok(temp, " "); char* docs; docs = strtok(NULL, " "); char* doc; // while the end of the line hasn't been reached, keep str'toking while((doc = strtok(NULL, " ")) != NULL) { char* freq; // parse the frequency freq = strtok(NULL, " "); // insert word, along with docID and frequency into the index insertElementIntoIndex(word, atoi(doc), atoi(freq), index); } } } fclose(indexFile); free(path); free(buffer); return(index); }
TEST_P(TokenizerTest, Tokenize) { const char* testName = GetParam(); char* inputFile = (char*) malloc(sizeof(char) * (strlen(DATA_PATH) + strlen(testName) + strlen(".in") + 1)); strcpy(inputFile, DATA_PATH); strcat(inputFile, testName); strcat(inputFile, ".in"); std::fstream inputStream(inputFile, std::fstream::in); ASSERT_TRUE(inputStream.good()); char* resultFile = (char*) malloc(sizeof(char) * (strlen(DATA_PATH) + strlen(testName) + strlen(".out") + 1)); strcpy(resultFile, DATA_PATH); strcat(resultFile, testName); strcat(resultFile, ".out"); std::fstream resultStream(resultFile, std::fstream::in); ASSERT_TRUE(resultStream.good()); Tokenizer tokenizer(&inputStream); Tokenizer::Token token; std::string output; do { tokenizer.nextToken(token); ASSERT_NE(token.type, Tokenizer::Token::None); std::string line; line.append(tokenTypeName(token.type)); if (shouldPrintContents(token.type)) { line.push_back(' '); line.push_back('"'); line.append(removeNewLines(token.contents)); line.push_back('"'); } output.append(line); output.push_back('\n'); } while (token.type != Tokenizer::Token::EndOfInput); std::istringstream outputStream(output); int line = 1; while (resultStream.good() || outputStream.good()) { std::string outputLine, resultLine; getline(outputStream, outputLine); getline(resultStream, resultLine); ASSERT_EQ(resultLine, outputLine) << "Line " << line; ++line; } }
QList<QHash<QString, QString> > GitRunner::log() { QList<QHash<QString, QString> > data; QHash<QString, QString> temp; QStringList command; command << "log"; //we will use $ in the format in order to //split the string author, date, subject //and we will use ^^ in order to identify every commit command << "--pretty=format:%an$%s$%ad$%H^^"; QString result = execSynchronously(command); QStringList commitList = result.split("^^", QString::SkipEmptyParts); foreach(QString commit, commitList) { QStringList l = commit.split('$', QString::SkipEmptyParts); temp["author"] = removeNewLines(l.at(0)); temp["subject"] = removeNewLines(l.at(1)); temp["date"] = removeNewLines(l.at(2)); temp["sha1hash"] = removeNewLines(l.at(3)); data.append(temp); }
/*First convert the map into a 2D array*/ Maze * convertMaze (FILE * mazeFile) { char input[255]; char (*maze)[255] = malloc(255*255); /*Repeat for each line in the file*/ int height = 0; int width = 0; while (fgets(input,254,mazeFile) != NULL) { if (height == 0) { width = strlen(input) -1; } removeNewLines(input); for (int i = 0; i < width; i++) { maze[height][i] = input[i]; } height++; } Maze * mazeStruct = malloc(sizeof(Maze)); mazeStruct->height = height; mazeStruct->width = width; mazeStruct->mazeMap = maze; return mazeStruct; }
stringstream& StringBuilder::operator<<(const string& str) { mStringing<<str; Log(lDebug5)<<"Appended :"<<removeNewLines(str, 1); return mStringing; }
/* * Obfuscate the Lua's code in the files in list <fileList> * <excludeFunctions> contatins name of global function, for which don't make obfuscating * <obf> contains the some options */ int LuaObfuscator::obfuscate(const stObfuscatorSetting &settings) { FILE *fileNew, *fileOld; char szFileNameNew[FILENAME_MAX]; ptrdiff_t commentSize, spaceSize, newLineSize, duplicateCharsSize; ptrdiff_t localVarsSize; size_t constIntSize, constFloatSize, constStringSize; if (m_luaFiles.empty()) return 0; StringListConstIter iter = m_luaFiles.begin(); while (iter != m_luaFiles.end()) { const char *szFileName = getFileName(iter); fileOld = fopen(szFileName, "rt"); if (!fileOld) { print("ERROR: Open file fail\n"); ++iter; continue; } fseek(fileOld, 0, SEEK_END); long size = ftell(fileOld); rewind(fileOld); char *pDataInSource = new char[size + 5]; char *pDataIn = pDataInSource + 2; // for delete/clear data if (!pDataInSource) { fclose(fileOld); print("Try to alloc memory failed, size %d\n", size + 5); return 0; } size_t realSize = fread(pDataIn, 1, size, fileOld); pDataInSource[0] = 0; // \0\0...............\0\0 pDataInSource[1] = 0; pDataIn[realSize] = 0; pDataIn[realSize + 1] = 0; char szFileBakup[FILENAME_MAX]; if (settings.bCreateBakFile) { strcpy(szFileBakup, szFileName); strcat(szFileBakup, ".bak"); FILE *fileBakup = fopen(szFileBakup, "wt"); if (fileBakup) { fwrite(pDataIn, 1, strlen(pDataIn), fileBakup); fclose(fileBakup); } } commentSize = 0; duplicateCharsSize = 0; spaceSize = 0; newLineSize = 0; constIntSize = 0; constFloatSize = 0; constStringSize = 0; localVarsSize = 0; commentSize = removeComments(pDataIn); #ifdef _DEBUG SaveToFile(pDataIn, szFileName, "1_comment"); #endif duplicateCharsSize = removeDumplicatedChars(pDataIn); #ifdef _DEBUG SaveToFile(pDataIn, szFileName, "2_dublicate"); #endif spaceSize = removeExtraWhitespace(pDataIn); #ifdef _DEBUG SaveToFile(pDataIn, szFileName, "3_spaces"); #endif newLineSize = removeNewLines(pDataIn); #ifdef _DEBUG SaveToFile(pDataIn, szFileName, "4_endline"); #endif StringStream strIn, strOut; if (settings.ObfuscateConstInt || settings.ObfuscateConstFloat || settings.ObfuscateConstString) { obfuscateConst(pDataIn, strOut, settings.ObfuscateConstInt, settings.ObfuscateConstFloat, settings.ObfuscateConstString); #ifdef _DEBUG SaveToFile(strOut.str().c_str(), szFileName, "5_const"); #endif } else { strOut.str(""); strOut << pDataIn; } strIn.str(""); strIn << strOut.str(); if (settings.ObfuscateLocalVasAndParam) { try { localVarsSize = obfuscateLocalVarsAndParameters(strIn.str().c_str(), strOut); } catch (std::exception e) { printf(strOut.str().c_str()); printf(e.what()); } #ifdef _DEBUG SaveToFile(strOut.str().c_str(), szFileName, "6_local_vars"); #endif } printObfuscateResilt("Remove the comments", commentSize); printObfuscateResilt("Remove the duplicate chars", duplicateCharsSize); printObfuscateResilt("Remove the spaces", spaceSize); printObfuscateResilt("Remove the \"New line\"", newLineSize); printObfuscateResilt("Obfuscate constant integer numbers", constIntSize); printObfuscateResilt("Obfuscate constant float numbers", constFloatSize); printObfuscateResilt("Obfuscate constant strings", constStringSize); printObfuscateResilt("Obfuscate local vars", localVarsSize); generateNewFileName(szFileNameNew, szFileName); fileNew = fopen(szFileNameNew, "wt"); if (!fileNew) { print("Couldn't create file %s\n", szFileNameNew); fclose(fileOld); return -1; } const std::string &sTmp = strOut.str(); const char *p = sTmp.c_str(); // TODO: correct code size_t lenCstr = strlen(p); fwrite(p, 1, lenCstr, fileNew); delete[] pDataInSource; fclose(fileOld); fclose(fileNew); /* if (settings.bCreateBakFile) { unlink(szFileName); rename(szFileNameNew, szFileName); }*/ ++iter; } if (settings.ObfuscateGlobalFunctionName) { print("\nObfuscate global functions\n"); ptrdiff_t globalFunctionSize = obfuscateGlobalFunctionNames(); print("size: %+d\n", globalFunctionSize); } int addCommentSize = 0; // TODO: may be replace this code in loop of each file (see above, local) if (settings.ObfuscateAddFalseComment) { print("\nAdd comment\n"); addCommentSize = addFalseComment(); print("Total size: %d\n", addCommentSize); } char szAddonFilename[MAX_PATH]; strcpy(szAddonFilename, m_sAddonDir.c_str()); strcat(szAddonFilename, "addon.lua"); FILE *addonFile = fopen(szAddonFilename, "wb"); if (!addonFile) { printf("Couldn't create addon.lua file\n"); return 1; } iter = m_luaFiles.begin(); while (iter != m_luaFiles.end()) { const char *szFileName = getFileName(iter); generateNewFileName(szFileNameNew, szFileName); fileNew = fopen(szFileNameNew, "rb"); char buf[4096]; char cLast = 0; while (size_t size = fread(buf, 1, 4096, fileNew)) { fwrite(buf, size, 1, addonFile); cLast = buf[size - 1]; } fclose(fileNew); // add ; to divide files if (!settings.linesBetweenFiles) { if (cLast != ';') { fwrite(";", 1, 1, addonFile); } } else { for (size_t i = 0; i < settings.linesBetweenFiles; ++i) { fwrite("\n", 1, 1, addonFile); } } ++iter; } fclose(addonFile); return 0; }