void Guys::configurationFileCheck() { if( configFileExists("guys/twitter") ) { //std::cout << "file exists" << std::endl; FILE *pFile; char buffer [100]; std::string result; std::string command = "cd ~/ && pwd"; std::string file; pFile = popen(command.c_str(), "r"); if (pFile == NULL) { std::cout << "handle error" << std::endl; return; } else { while ( !feof(pFile) ) { fgets (buffer , 100 , pFile); } pclose (pFile); } result = buffer; configPath = result + "/.config/guys/"; //accessible to all functions std::string::iterator it; for ( it=configPath.begin() ; it < configPath.end(); it++ ) if (*it == 10) configPath.erase(it); //erase return characters } else { if( folderExists("~/.config/guys") ) { std::cout << "creating config files in ~/.config/guys/" << std::endl; //but not the file makeConfigFile("guys/twitter"); //create the file configurationFileCheck(); } else { makeConfigDir("guys"); configurationFileCheck(); } } return; }
std::string Config::setup() { versionDifferent = false; bool configFileFound = false; bool configFileValid = false; bool firstTime = false; bool setupDone = false; string strSetupResult = ""; // get currentDir path currentDir = getCurrentDir(); // current directory cout << "currentDir = " << currentDir << endl; // get ENV's appdata path PLUS 'BeepComp' appDataPath = getAppDataPath(); bool appDataCreateResult; // // create the parent folder in appdata // appDataCreateResult = CreateDirectory(appDataPath.c_str(), NULL); cout << "Create %APPDATA%\\BeepComp folder: " << appDataPath << endl; // success-fail check if(appDataCreateResult) cout << "success!\n"; else { cout << "fail.\n"; if(GetLastError()==ERROR_ALREADY_EXISTS) cout << "The folder already exists.\n"; } // now check for the config file's existance cout << "check if beepcomp.config exists...\n"; configFilePath = appDataPath + "\\" + "beepcomp.config"; cout << "configFilePath = " << configFilePath << endl; // if config file found ... get Path information from that configFileFound = configFileExists(configFilePath); // if config file found -> the program has already run once after installation // we will check for the validity of config file... // if config file was found, let's try to get path data from it if(configFileFound) { cout << "Config file exists, reading contents and check data validity...\n"; configFileValid = readConfigFile(); // read config file ... you get boolean result for data validity if(configFileValid) { cout << "Config file's data is good!.\n"; cout << "Path information is now:\n"; cout << "userDocPath: " << userDocPath << endl; cout << "userdataParentPath: " << userdataParentPath << endl; cout << "userdataPath: " << userdataPath << endl; setupDone = true; // congrats, all done here } else cout << "Config file's data is NOT good...\n"; } // config file NOT found. first time here else { cout << "Config file NOT found. first time here...\n"; firstTime = true; } if(versionDifferent) cout << "Installed version is different - we should overwrite userdata.\n"; // if config file is found and also its data is valid... // we have read all the valid path information - we will exit without setup if(setupDone) { strSetupResult = "SETUP SKIPPED - Config file found and is valid"; return strSetupResult; } //////////////////////////////////////////////////////////////////////////////////////// // File not found - means program is being run for the first time after installation // - will do initial setup! setupDone = false; if(firstTime) { cout << "Config file does not exist. Must be fresh after installation!\n"; strSetupResult = "Initial setup - "; } else { cout << "Config file corrupt - need to rebuild! (or possibly dest folders erased?)\n"; strSetupResult = "Rebuild corrupt config file (or reconstructing dest folders) - "; } // get the userDoc path from env var userDocPath = getUserDocPathFromSystem(); cout << "User's 'Documents' path = " << userDocPath << endl; // if this EVER fails... safeguard: set it to USERPROFILE folder... if(userDocPath.empty()) { userDocPath = string(getenv("USERPROFILE")); cout << "User's Documents path corrected to USERPROFILE folder:\n"; cout << userDocPath << endl; } bool result; // // create the parent folder in userdata // userdataParentPath = userDocPath + "\\" + "BeepComp"; result = CreateDirectory(userdataParentPath.c_str(), NULL); cout << "Create parent folder: " << userdataParentPath << endl; // success-fail check if(result) cout << "success!\n"; else { cout << "fail.\n"; if(GetLastError()==ERROR_ALREADY_EXISTS) cout << "The folder already exists.\n"; } // // create the userdata folder in the parent folder // userdataPath = userdataParentPath + "\\" + "userdata"; result = CreateDirectory(userdataPath.c_str(), NULL); cout << "Create userdata folder: " << userdataPath << endl; // success-fail check if(result) cout << "success!\n"; else { cout << "fail.\n"; if(GetLastError()==ERROR_ALREADY_EXISTS) cout << "The folder already exists.\n"; } // now get ready to copy all the txt files to destination... cout << "Okay, first time here. Let's set up your userdata folder...\n"; // get ready to copy all the txt files from userdata source userdataPathOrigin = currentDir + "\\" + "userdata"; vector<string> dataFiles = getFileNamesInFolder(userdataPathOrigin); int nFiles = dataFiles.size(); cout << "Listing files to copy...\n"; for(int i=0; i<nFiles; i++) cout << dataFiles[i] << endl; // now copy all these files to to the newly created userdata directory! for(int i=0; i<nFiles; i++) { string source = userdataPathOrigin + "\\" + dataFiles[i]; string destination = userdataPath + "\\" + dataFiles[i]; cout << "Copying...\n"; cout << "From: " << source << endl; cout << "To: " << destination << endl; int copyResult; copyResult = CopyFile(source.c_str(), destination.c_str(), FALSE); // will force overwrite if(copyResult==0) cout << "...failed\n"; else cout << "...success!\n"; } // write all path information to config file... result = writeConfigFile(); if(!result) { cout << "Unable to write: " << configFilePath << endl; strSetupResult += "error writing config file"; } else { cout << "beepcomp.config file created!\n"; strSetupResult += "SUCCESS"; } return strSetupResult; }