void Graphics::PrintPicture() { std::fstream a_file( "./screenshots/count.txt" ); if( !a_file.is_open() ) { printf("Could not find the file. Creating count.txt.\n"); std::ostringstream convert; std::ofstream a_file( "./screenshots/count.txt" ); FileNum = 100; convert << FileNum; a_file << convert.str(); a_file.close(); } else { a_file.close(); std::string Result; printf("Found count.txt. Updating the number.\n"); a_file >> Result; std::istringstream convert( Result ); convert >> FileNum; printf("Found file num: %d.\n", FileNum); FileNum += 1; a_file.close(); std::ofstream a_file( "./screenshots/count.txt", std::ios::trunc ); a_file << FileNum; a_file.close(); } std::string saveFile; std::ostringstream fileNumber; fileNumber << FileNum; saveFile = "./screenshots/island" + fileNumber.str() + ".bmp"; char *NameOfFile = (char*)saveFile.c_str(); SDL_SaveBMP( Window, NameOfFile ); printf("New FileNum = %d.\n", FileNum ); printf("Printed out file %s.\n", NameOfFile ); }
JNIEXPORT jboolean JNICALL Java_com_devadvance_rootinspector_Root_checkifstream(JNIEnv * env, jobject obj, jstring filepath) { jboolean fileExists = 0; jboolean isCopy; const char * path = env->GetStringUTFChars(filepath, &isCopy); __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: ifstream with path: [%s]", path); std::ifstream a_file( path ); if ( !a_file.is_open() ) { // The file could not be opened } else { // Safely use the file stream __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: File exists. ifstream.is_open() is true."); fileExists = 1; a_file.close(); } return fileExists; }
bool HexFile::loadHex(char filepath[], bool isMC18) { // Populate buffer unsigned char lineDataCount = 0; unsigned long lineAddrHigh = 0; unsigned long lineAddr = 0; unsigned char lineCode = 0; unsigned long fullAddr = 0; strcpy_s(HexFile::filepath,FILE_PATH_SIZE,filepath); HexFile::length = 0; HexFile::addrLower = 0x7FFFFFFF; HexFile::addrUpper = 0; // Prime the buffer for (int i = 0; i < BUFFER_SIZE; i++) buff[i] = 0xFF; // Try opening file ifstream a_file (filepath); if ( !a_file.is_open() ) { // The file could not be opened strcpy_s(lastError,ERROR_BUFFER_SIZE,"Err! Unable to open file."); return false; } else { // Safely use the file stream char oneLine[LINE_MAX_SIZE]; // For each line, read until eof. while (!a_file.eof()) { // read one line a_file.getline(&oneLine[0],LINE_MAX_SIZE); // If valid line if (strcmp(oneLine,"")!=0 && oneLine[0]==':') { lineDataCount = (unsigned char)HexFile::hexToLong(oneLine,1,2); lineAddr = (unsigned long)HexFile::hexToLong(oneLine,3,4) & 65535; lineCode = (unsigned char)HexFile::hexToLong(oneLine,7,2); fullAddr = (lineAddrHigh * 65536) + lineAddr; switch(lineCode) { case 0: HexFile::length +=lineDataCount; if ((fullAddr + lineDataCount - 1) > HexFile::addrUpper) HexFile::addrUpper = fullAddr + lineDataCount - 1; if (lineCode == 0 && fullAddr < HexFile::addrLower) HexFile::addrLower = fullAddr; for (int j = 0; j < lineDataCount; j++) { buff[fullAddr] = (unsigned char)HexFile::hexToLong(oneLine,j * 2 + 9, 2); //byte.Parse(oneLine.Substring(j * 2 + 9, 2), System.Globalization.NumberStyles.HexNumber); buff[fullAddr] &= 255; //(byte)255; fullAddr++; } break; case 4: lineAddrHigh = (unsigned long)HexFile::hexToLong(oneLine,9, 4) & 65535; //long.Parse(oneLine.Substring(9, 4), System.Globalization.NumberStyles.HexNumber) & 65535; break; case 1: break; break; } } } } // Force lower addr to start HexFile::addrLower = USER_RESET_ADDR; return true; }
void ModNeatExperiment7::processGroup(shared_ptr<NEAT::GeneticGeneration> generation) { // FORK a number of processes equal to the number of individuals we are going to process // getGroupSize is the size of ::getGroupCapacity const int groupSize = getGroupSize(); pid_t* pids = new pid_t[groupSize]; for (int i = 0; i < groupSize; ++i) { // increment individual count if the current individual number is smaller than the population size. // if it is not, then it is the first individual of the next population. // this is an ugly workaround since it is not easily possible to get the current individual or generation // from HyperNEAT itself, thus it needs to be managed by ourselves. if(currIndividual == popSize) { // reset individual and increase generation number currIndividual = 1; currGeneration++; } else { // this is just a next individual in the current generation currIndividual++; } screen << "processing generation: " << currGeneration << ", individual: " << currIndividual << ", on thread: " << i << endl; shared_ptr<NEAT::GeneticIndividual> individual = group[i]; // avoid 0 fitness if (individual->getFitness() <= 0) individual->setFitness(0.000001); // spawn child process after 1 sec (webotsrc test) usleep(1000 * 1000); pids[i] = fork(); // child code if (pids[i] == 0) { // code only executed by child process // evaluate individual and get fitness double fitness = processEvaluation(individual, NULL, i); // write fitness to a named textfile stringstream ss; ss << i; string temp_str = "simulation" + ss.str() + ".txt"; // write fitness to textfile ofstream a_file (temp_str.c_str()); a_file << fitness; a_file.close(); exit(0); // exit child process successfully } } // WAIT for each individual process and only when all have exited, continue main process for (int i = 0; i < groupSize; ++i) { int status; while (-1 == waitpid(pids[i], &status, 0)) { if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { screen << "Process " << i << " (pid " << pids[i] << ") failed" << endl; exit(1); } } } // READ back the fitness values and for (int i = 0; i < groupSize; ++i) { // get individual to reward fitness too shared_ptr<NEAT::GeneticIndividual> individual = group[i]; // Opens for reading the file stringstream ss; ss << i; string temp_str = "simulation" + ss.str() + ".txt"; ifstream b_file (temp_str.c_str()); // Reads one string from the file string fitness; b_file >> fitness; b_file.close(); // reward this individual its fitness individual->reward(abs(::atof(fitness.c_str())) + 0.000001); } }
void cAnimationManager::SaveAnimation(std::string _filename, uint animationId){ cAnimation *tmpAni = GetAnimation(animationId); if(_filename == ""){ StormPrintLog(STORM_LOG_ERROR, "cAnimation", "Could not save animation. Filename name not specified"); return; } if(tmpAni->GetFramesCount() == 0){ StormPrintLog(STORM_LOG_ERROR, "cAnimation", "Could not save animation. There are not any frames in this animation"); return; } tmpAni->SetFilename(_filename); std::string fullFile = (char*)STORM_DIR_ANIMS + _filename; cBinaryFile tmpFile; std::ofstream a_file(fullFile); a_file.clear(); tmpFile.Open(fullFile); if(!tmpFile.IsOpen()){ StormPrintLog(STORM_LOG_ERROR, "cAnimation", "Could not open file %s for saving", fullFile.c_str()); return; } //Storm animation file always begin with 16.16 float tmpFile.WriteFloat(16.20f); //File version tmpFile.WriteInt((int)STORM_ANIM_FILEVER); //FPS tmpFile.WriteInt(tmpAni->GetFps()); //Frames count tmpFile.WriteInt(tmpAni->GetFramesCount()); //Use textures, or sections if(tmpAni->IsUseTextures()){ tmpFile.WriteByte('1'); }else{ tmpFile.WriteByte('0'); } //Write frames sTexture *tmpTexture = NULL; for(int i = 0; i < tmpAni->GetFramesCount(); i++){ if(!tmpAni->IsUseTextures()){ //Animation is made using texture sections sTextureSection *tmpSec = tManager->GetSection(tmpAni->GetFrame(i)); tmpTexture = tManager->GetTexture(tmpSec->texture); //Write section details tmpFile.WriteInt(tmpSec->x); tmpFile.WriteInt(tmpSec->y); tmpFile.WriteInt(tmpSec->width); tmpFile.WriteInt(tmpSec->height); // // // }else{ //Animation is made using multiple textures tmpTexture = tManager->GetTexture(tmpAni->GetFrame(i)); //Filename length tmpFile.WriteString(tmpTexture->filename); } } if(!tmpAni->IsUseTextures()){ //Animation is made using texture sections //Save texture filename tmpFile.WriteString(tmpTexture->filename.c_str()); } //Write all frame gropus std::map<std::string, sAnimFrameGroup> *groups = tmpAni->GetFrameGroups(); tmpFile.WriteInt((int)groups->size()); if(groups->size() > 0){ for (auto& kv : *groups) { tmpFile.WriteString(kv.first); tmpFile.WriteInt(kv.second.start); tmpFile.WriteInt(kv.second.end); } } //Integer 777 marks EOF tmpFile.WriteInt(777); tmpFile.Close(); StormPrintLog(STORM_LOG_INFO, "cAnimation", "Animation %s saved", fullFile.c_str()); }
int main(int argc, char *argv[]) { float *a, *b, *c; gmactime_t s, t; ecl::error err; assert(ecl::compileSource(kernel) == eclSuccess); float * orig = (float *) malloc(vecSize * sizeof(float)); std::ifstream o_file(VECTORC); o_file.read((char *)orig, vecSize * sizeof(float)); o_file.close(); getTime(&s); // Alloc & init input data assert(ecl::malloc((void **)&a, vecSize * sizeof(float)) == eclSuccess); assert(ecl::malloc((void **)&b, vecSize * sizeof(float)) == eclSuccess); assert(ecl::malloc((void **)&c, vecSize * sizeof(float)) == eclSuccess); getTime(&t); printTime(&s, &t, "Alloc: ", "\n"); std::ifstream a_file(VECTORA); std::ifstream b_file(VECTORB); getTime(&s); a_file.read((char *)a, vecSize * sizeof(float)); a_file.close(); b_file.read((char *)b, vecSize * sizeof(float)); b_file.close(); getTime(&t); printTime(&s, &t, "Init: ", "\n"); // Call the kernel getTime(&s); ecl::config localSize (blockSize); ecl::config globalSize (vecSize / blockSize); if(vecSize % blockSize) globalSize.x++; globalSize.x *= localSize.x; ecl::kernel kernel("vecAdd", err); assert(err == eclSuccess); #ifndef __GXX_EXPERIMENTAL_CXX0X__ err = kernel.setArg(0, c); assert(err == eclSuccess); err = kernel.setArg(1, a); assert(err == eclSuccess); err = kernel.setArg(2, b); assert(err == eclSuccess); err = kernel.setArg(3, vecSize); assert(err == eclSuccess); err = kernel.callNDRange(globalSize, localSize); assert(err == eclSuccess); #else assert(kernel(c, a, b, vecSize)(globalSize, localSize) == eclSuccess); #endif getTime(&t); printTime(&s, &t, "Run: ", "\n"); getTime(&s); float error = 0.f; for(unsigned i = 0; i < vecSize; i++) { error += orig[i] - (c[i]); } getTime(&t); printTime(&s, &t, "Check: ", "\n"); getTime(&s); std::ofstream c_file("vectorC_shared"); c_file.write((char *)c, vecSize * sizeof(float)); c_file.close(); getTime(&t); printTime(&s, &t, "Write: ", "\n"); getTime(&s); ecl::free(a); ecl::free(b); ecl::free(c); getTime(&t); printTime(&s, &t, "Free: ", "\n"); return error != 0; }