forked from bronevet/old_sight
/
utils.C
90 lines (78 loc) · 2.89 KB
/
utils.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <fstream>
#include <iostream>
#include <sstream>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <iostream>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "binreloc.h"
#include <errno.h>
#include "getAllHostnames.h"
#include "utils.h"
using namespace std;
namespace sight {
// Create the directory workDir/dirName and return the string that contains the absolute
// path of the created directory
string createDir(string workDir, string dirName) {
ostringstream fullDirName; fullDirName<<workDir<<"/"<<dirName;
ostringstream cmd; cmd << "mkdir -p "<<fullDirName.str();
int ret = system(cmd.str().c_str());
if(ret == -1) { cout << "ERROR creating directory \""<<workDir<<"/"<<dirName<<"\"!"; exit(-1); }
return fullDirName.str();
}
// Copy dirName from the source code path to the workDir
string copyDir(string workDir, string dirName) {
ostringstream fullDirName; fullDirName << workDir << "/" << dirName;
ostringstream cmd; cmd << "cp -fr "<<ROOT_PATH<<"/"<<dirName<<" "<<workDir;
//cout << "Command \""<<cmd.str()<<"\"\n";
int ret = system(cmd.str().c_str());
if(ret == -1) { cout << "ERROR copying files from directory \""<<ROOT_PATH<<"/"<<dirName<<"\" directory \""<<fullDirName.str()<<"\"!"; exit(-1); }
chmod(fullDirName.str().c_str(), S_IRWXU);
return fullDirName.str();
}
// Open a freshly-allocated output stream to write a file with the given name and return a pointer to the object.
ofstream& createFile(string fName) {
ofstream *f = new ofstream();
try {
f->open(fName.c_str(), std::ios::out);
} catch (ofstream::failure e)
{ cout << "createFile() ERROR opening file \""<<fName<<"\" for writing!"; exit(-1); }
return *f;
}
// Returns a string that contains n tabs
string tabs(int n)
{
string s;
for(int i=0; i<n; i++)
s+="\t";
return s;
}
// Given a string and a line width returns a variant of the string where line breaks are inserted
// at approximately every lineWidth characters.
std::string wrapStr(std::string str, unsigned int lineWidth) {
string multiLineStr = "";
unsigned int i=0;
while(i<str.length()-lineWidth) {
// Look for the next line-break
unsigned int nextLB = str.find_first_of("\n", i);
// If the next line is shorter than lineWidth, add it to labelMulLineStr and move on to the next line
if(nextLB-i < lineWidth) {
multiLineStr += str.substr(i, nextLB-i+1);
i = nextLB+1;
// If the next line is longer than lineWidth, add just lineWidth characters to labelMulLineStr
} else {
// If it is not much longer than lineWidth, don't break it up
if(i>=str.length()-lineWidth*1.25) break;
multiLineStr += str.substr(i, lineWidth) + "\\n";
i += lineWidth;
}
}
// Add the last line in str to labelMulLineStr
if(i<str.length())
multiLineStr += str.substr(i, str.length()-i);
return multiLineStr;
}
} // namespace sight