-/***********************************************************************/
-
-inline string hasPath(string longName){
-
- string path = "";
-
- size_t found;
- found=longName.find_last_of("/\\");
-
- if(found != longName.npos){
- path = longName.substr(0, found+1);
- }
-
- return path;
-}
-
-/***********************************************************************/
-
-inline string getExtension(string longName){
-
- string extension = longName;
-
- if(longName.find_last_of('.') != longName.npos){
- int pos = longName.find_last_of('.');
- extension = longName.substr(pos, longName.length());
- }
-
- return extension;
-}
-/***********************************************************************/
-inline bool isBlank(string fileName){
-
- ifstream fileHandle;
- fileHandle.open(fileName.c_str());
- if(!fileHandle) {
- cout << "Error: Could not open " << fileName << endl;
- return false;
- }else {
- //check for blank file
- gobble(fileHandle);
- if (fileHandle.eof()) { fileHandle.close(); return true; }
- }
- return false;
-}
-/***********************************************************************/
-
-inline string getFullPathName(string fileName){
- try{
-
- string path = hasPath(fileName);
- string newFileName;
- int pos;
-
- if (path == "") { return fileName; } //its a simple name
- else { //we need to complete the pathname
- // ex. ../../../filename
- // cwd = /user/work/desktop
-
- string cwd;
- //get current working directory
- #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
- if (path.rfind("./") == -1) { return fileName; } //already complete name
- else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
-
- char* cwdpath = new char[1024];
-
- size_t size;
- cwdpath=getcwd(cwdpath,size);
-
- cwd = cwdpath;
-
- //rip off first '/'
- string simpleCWD;
- if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
-
- //break apart the current working directory
- vector<string> dirs;
- while (simpleCWD.find_first_of('/') != -1) {
- string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
- simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
- dirs.push_back(dir);
- }
- //get last one // ex. ../../../filename = /user/work/desktop/filename
- dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
-
-
- int index = dirs.size()-1;
-
- while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
- if (path[(pos-1)] == '.') { //you want your parent directory ../
- path = path.substr(0, pos-1);
- index--;
- if (index == 0) { break; }
- }else if (path[(pos-1)] == '/') { //you want the current working dir ./
- path = path.substr(0, pos);
- }else if (pos == 1) { break;
- }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
- }
-
- for (int i = index; i >= 0; i--) {
- newFileName = dirs[i] + "/" + newFileName;
- }
-
- newFileName = "/" + newFileName;
- return newFileName;
-
- #else
- if (path.rfind(".\\") == -1) { return fileName; } //already complete name
- else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
-
- char *cwdpath = NULL;
- cwdpath = getcwd(NULL, 0); // or _getcwd
- if ( cwdpath != NULL) { cwd = cwdpath; }
- else { cwd = ""; }
-
- //break apart the current working directory
- vector<string> dirs;
- while (cwd.find_first_of('\\') != -1) {
- string dir = cwd.substr(0,cwd.find_first_of('\\'));
- cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
- dirs.push_back(dir);
-
- }
- //get last one
- dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
-
- int index = dirs.size()-1;
-
- while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
- if (path[(pos-1)] == '.') { //you want your parent directory ../
- path = path.substr(0, pos-1);
- index--;
- if (index == 0) { break; }
- }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
- path = path.substr(0, pos);
- }else if (pos == 1) { break;
- }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
- }
-
- for (int i = index; i >= 0; i--) {
- newFileName = dirs[i] + "\\" + newFileName;
- }
-
- return newFileName;
-
- #endif
- }
- }
- catch(exception& e) {
- cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function getFullPathName. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
- exit(1);
- }
-}
-/***********************************************************************/
-
-inline int openInputFile(string fileName, ifstream& fileHandle, string m){
-
- //get full path name
- string completeFileName = getFullPathName(fileName);
-
- fileHandle.open(completeFileName.c_str());
- if(!fileHandle) {
- return 1;
- }else {
- //check for blank file
- gobble(fileHandle);
- return 0;
- }
-}
-/***********************************************************************/
-
-inline int openInputFile(string fileName, ifstream& fileHandle){
-
- //get full path name
- string completeFileName = getFullPathName(fileName);
-
- fileHandle.open(completeFileName.c_str());
- if(!fileHandle) {
- cout << "Error: Could not open " << completeFileName << endl;
- return 1;
- }
- else {
- //check for blank file
- gobble(fileHandle);
- if (fileHandle.eof()) { cout << completeFileName << " is blank. Please correct." << endl; return 1; }
-
- return 0;
- }
-
-}
-/***********************************************************************/
-
-inline int renameFile(string oldName, string newName){
-
- ifstream inTest;
- int exist = openInputFile(newName, inTest, "");
-
-#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
- if (exist == 0) { //you could open it so you want to delete it
- inTest.close();
- string command = "rm " + newName;
- system(command.c_str());
- }
-
- string command = "mv " + oldName + " " + newName;
- system(command.c_str());
-#else
- remove(newName.c_str());
- int renameOk = rename(oldName.c_str(), newName.c_str());
-#endif
- return 0;
-}
-
-/***********************************************************************/
-
-inline int openOutputFile(string fileName, ofstream& fileHandle){
-
- string completeFileName = getFullPathName(fileName);
-
- fileHandle.open(completeFileName.c_str(), ios::trunc);
- if(!fileHandle) {
- cout << "Error: Could not open " << completeFileName << endl;
- return 1;
- }
- else {
- return 0;
- }
-
-}
-
-/***********************************************************************/
-
-inline int getNumSeqs(ifstream& file){
-
- int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
- file.seekg(0);
- return numSeqs;
-
-}
-/***********************************************************************/
-
-inline bool inVector(string member, vector<string> group){
-
- for (int i = 0; i < group.size(); i++) {
- if (group[i] == member) { return true; }
- }
-
- return false;
-}
-/***********************************************************************/
-
-//This function parses the estimator options and puts them in a vector
-inline void splitAtDash(string& estim, vector<string>& container) {
- try {
- string individual;
-
- while (estim.find_first_of('-') != -1) {
- individual = estim.substr(0,estim.find_first_of('-'));
- if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
- estim = estim.substr(estim.find_first_of('-')+1, estim.length());
- container.push_back(individual);
- }
- }
- //get last one
- container.push_back(estim);
- }
- catch(exception& e) {
- cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
- exit(1);
- }
-}
-
-/***********************************************************************/
-//This function parses the label options and puts them in a set
-inline void splitAtDash(string& estim, set<string>& container) {
- try {
- string individual;
-
- while (estim.find_first_of('-') != -1) {
- individual = estim.substr(0,estim.find_first_of('-'));
- if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
- estim = estim.substr(estim.find_first_of('-')+1, estim.length());
- container.insert(individual);
- }
- }
- //get last one
- container.insert(estim);
- }
- catch(exception& e) {
- cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
- exit(1);
- }
-}
-/***********************************************************************/
-//This function parses the line options and puts them in a set
-inline void splitAtDash(string& estim, set<int>& container) {
- try {
- string individual;
- int lineNum;
-
- while (estim.find_first_of('-') != -1) {
- individual = estim.substr(0,estim.find_first_of('-'));
- if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
- estim = estim.substr(estim.find_first_of('-')+1, estim.length());
- convert(individual, lineNum); //convert the string to int
- container.insert(lineNum);
- }
- }
- //get last one
- convert(estim, lineNum); //convert the string to int
- container.insert(lineNum);
- }
- catch(exception& e) {
- cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
- exit(1);
- }
-}
-/***********************************************************************/
-//This function parses the a string and puts peices in a vector
-inline void splitAtComma(string& estim, vector<string>& container) {
- try {
- string individual;
-
- while (estim.find_first_of(',') != -1) {
- individual = estim.substr(0,estim.find_first_of(','));
- if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
- estim = estim.substr(estim.find_first_of(',')+1, estim.length());
- container.push_back(individual);
- }
- }
- //get last one
- container.push_back(estim);
- }
- catch(exception& e) {
- cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtComma. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
- exit(1);
- }
-}
-/***********************************************************************/
-
-//This function splits up the various option parameters
-inline void splitAtComma(string& prefix, string& suffix){
- try {
- prefix = suffix.substr(0,suffix.find_first_of(','));
- if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
- suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
- string space = " ";
- while(suffix.at(0) == ' ')
- suffix = suffix.substr(1, suffix.length());
- }
-
- }
- catch(exception& e) {
- cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtComma. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
- exit(1);
- }
-}
-/***********************************************************************/