void data_base::TrimEndOfFile() { /*The generated files receive an excess chunk after it is opened in write mode *multiple times. This makes the file progressively larger with crap. */ size_t lastMarker = 0; for(size_t i = 0; i < buffer.size(); i++) { if(buffer[i] == ';' || buffer[i] == '\n') { lastMarker = i; } if(buffer[i] == 26) { lastMarker = i; break;//Break a leg! We just reached EOF } } if(findString(":EOF:", buffer.c_str()) > 0) { //Optional way to end the file. Takes priority over the other markers. lastMarker = (size_t)findString(":EOF:", buffer.c_str()) - 1; } buffer = sliceStr(buffer, 0, lastMarker + 1); }
PyObject *Pywrap::exec_FullFile(const char file[], PyObject *argsI) { /*This method will take a filepath and a tuple containing the arguments, and will execute a main function within the script. I am assuming here that the module contains a main function that I can use as the entrypoint. I am not using the standard if __namespace__ == Python entrypoint!*/ if(findString(".py",file)>= 0)//Check and delete the .py extension as it is not necessary and makes the Python API crash. { file = sliceStr(file,0,std::string(file).size() - 3).c_str(); } PyObject *nameI = PyString_FromString(file);//Create Python object from our C string PyObject *moduleI = PyImport_Import(nameI);//Import the module. PyErr_Print();//get error output if import fails :). Py_DECREF(nameI);//Small cleanup. PyObject *funcI = PyObject_GetAttrString(moduleI, "main"); PyErr_Print();//get error output if import fails :). if (funcI && PyCallable_Check(funcI)) { if(PyTuple_GET_SIZE(args) < 1) { std::cout<<"Argument list is 0 items in size. Do not panic if you get an empty return!\n\r"; } result = PyObject_CallObject(funcI, argsI);//save the reference inside this class' object so we don't lose it for when we have to decrease its reference count. :) //Always remember to do some cleanup. Py_CLEAR(moduleI); Py_CLEAR(funcI); Py_CLEAR(argsI); //return our internal reference. The final cleanup has to be done elsewhere. I assume in this function that //I can overwrite our internal copy of the pointer because the cleanup will occur at the caller side. return result; } return 0; }
arangodb::LoggerStream& operator<< (arangodb::LoggerStream& logger, VPackSlice const& slice) { size_t const cutoff = 100; std::string sliceStr(slice.toJson()); bool longer = sliceStr.size() > cutoff; if (longer) { logger << sliceStr.substr(cutoff) << "..."; } else { logger << sliceStr; } return logger; }
double data_base::GetValueFromData(const std::string& search) const { /*This function takes a search string (aka. keyword) and looks for the index of the beginning of the keyword. Then, it slices the internal buffer and cleans it of non-numeric characters that are known to be in the internal string buffer. Finally, it sends the number string into a conversion function that returns the number equivalent of the string.*/ int start = findString((char*)search.c_str(), buffer.c_str())+ search.length(); int end = searchCharIndex(';', buffer, start); if(end < 0) { end = searchCharIndex('\n', buffer.c_str(), start); } std::string numberStr = sliceStr(buffer,start, end);//Get slice numberStr = removeCharFromStr('=', numberStr.c_str()); return cStrToNum(numberStr.c_str()); }
double data_base::GetValueFromDataWithLine(const std::string& search, int instanceIndex) const { /*Note: instanceIndex is the number of times a search word can be found in the target file. i.e. value = 1; value = 3; has 2 instances, so if inctanceIndex is 1 the result will be 1 and if instanceIndex is 2 the result will be 3. This method gets the value from an specific instance of a keyword.*/ int start = GetLineIndex(search, instanceIndex); int end = searchCharIndex(';', buffer, start); if(end == ENDOFFILE) { end = searchCharIndex('\n', buffer, start); } std::string numberStr = sliceStr(buffer,start, end);//Get slice numberStr = removeCharFromStr('=', numberStr.c_str()); return cStrToNum(numberStr.c_str()); }
std::string data_base::GetStrFromData(const std::string& search) const { /* Extracts the string in much the same way the GetValueFromData does.*/ int start = findString((char*)search.c_str(), buffer.c_str())+ search.length(); int end = searchCharIndex(';', buffer, start);//Get's end of line if(end < 0) { end = searchCharIndex('\n', buffer.c_str(), start); } std::string Str = sliceStr(buffer,start, end); Str = removeCharFromStr('=', Str.c_str());//Cleans value string from the = sign while(Str[0] == ' ')//remover initial spaces left after removing '='. { Str = removeCharFromStr(' ', Str.c_str()); } return Str; }
std::string data_base::GetStrFromDataWithLine(const std::string& search, int instanceIndex) const { /*Note: instanceIndex is the number of times a search word can be found in the target file. i.e. value = 1; value = 3; has 2 instances, so if inctanceIndex is 1 the result will be 1 and if instanceIndex is 2 the result will be 3. This method gets the value from an specific instance of a keyword.*/ int start = GetLineIndex(search, instanceIndex); int end = searchCharIndex(';', buffer, start); if(end == ENDOFFILE) { end = searchCharIndex('\n', buffer, start); } std::string Str = sliceStr(buffer,start, end); Str = removeCharFromStr('=', Str.c_str());//Cleans value string from the = sign while(Str[0] == ' ')//remover initial spaces left after removing '='. { Str = removeCharFromStr(' ', Str.c_str()); } return Str; }
Pywrap::Pywrap(const char* file, unsigned int arg_size) { if(file != "") { if(findString(".py",file)>= 0) { file = sliceStr(file,0,std::string(file).size() - 3).c_str(); } SetFilePath(file); name_space = PyString_FromString(file); module = PyImport_Import(name_space); PyErr_Print();//get error output if import fails :). Py_DECREF(name_space); fileLoaded = true; } else { fileLoaded = false; } args = PyTuple_New(arg_size); sizeT = arg_size; index = 0; }