CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FileToolKit.cc
Go to the documentation of this file.
2 
3 // STL Headers
4 #include <fstream>
5 
6 // Linux
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 
11 // C
12 #include <cerrno>
13 #include <cstdio>
14 #include <cstring>
15 
16 void FileToolKit::Tokenize(const std::string& str,
17  std::vector< std::string >& tokens,
18  const std::string& delimiters )
19 {
20  using std::string;
21 
22  // Skip delimiters at beginning.
23  string::size_type lastPos = str.find_first_not_of(delimiters, 0);
24  // Find first "non-delimiter".
25  string::size_type pos = str.find_first_of(delimiters, lastPos);
26 
27  while (string::npos != pos || string::npos != lastPos)
28  {
29  // Found a token, add it to the vector.
30  tokens.push_back(str.substr(lastPos, pos - lastPos));
31  // Skip delimiters. Note the "not_of"
32  lastPos = str.find_first_not_of(delimiters, pos);
33  // Find next "non-delimiter"
34  pos = str.find_first_of(delimiters, lastPos);
35  }
36 }
37 
38 int FileToolKit::MakeDir( std::string dirName, mode_t writeMode ){
39 
40  using std::vector;
41  using std::string;
42 
43  int result;
44  int errsv = 0;
45 
46  vector< string > tkDirName;
47  string currentDirName = "";
48 
49  Tokenize(dirName, tkDirName, "/");
50 
51  if(dirName[0] == '/') currentDirName += "/";
52 
53  struct stat mStat;
54 
55  for(unsigned int i = 0; i < tkDirName.size(); ++i ){
56 
57  currentDirName += tkDirName[i];
58  currentDirName += "/";
59 
60  errno = 0;
61  stat( currentDirName.c_str(), &mStat );
62  errsv = errno;
63 
64  if( errsv == 2 ){ // No such file or directory
65 
66  errno = 0;
67  result = mkdir( currentDirName.c_str(), writeMode);
68  errsv = errno;
69  if( errno == 0 ){
70  errno = 0;
71  result = chmod( currentDirName.c_str(), writeMode);
72  errsv = errno;
73  }
74  }
75  }
76  return errsv;
77 }
78 
79 void FileToolKit::MakeEmptyWebPage( const std::string &fileName,
80  const std::string &title ){
81 
82  std::fstream fileStream;
83 
84  fileStream.open( fileName.c_str(), std::fstream::out);
85 
86  fileStream << "<html>" << std::endl;
87  fileStream << "<title>" << std::endl;
88  fileStream << title << std::endl;
89  fileStream << "</title>" << std::endl;
90  fileStream << "<body>" << std::endl;
91  fileStream << "</body>" << std::endl;
92  fileStream << "</html>" << std::endl;
93 
94  fileStream.close();
95 }
96 
97 void FileToolKit::InsertLineAfter( const std::string &fileName,
98  const std::string &newLine,
99  const std::string &searchLine){
100 
101  bool bMatch = false;
102 
103  std::vector< std::string > fileContents;
104  char lineBuffer[256];
105 
106  std::fstream fileStream;
107 
108  // Read file into memory and insert new line.
109  fileStream.open(fileName.c_str(), std::fstream::in );
110  while( fileStream.good() ){
111  fileStream.getline( lineBuffer, 256 );
112 
113  fileContents.push_back( lineBuffer );
114 
115  if( strcmp( lineBuffer, searchLine.c_str() ) == 0 ){
116  fileContents.push_back( newLine );
117  bMatch = true;
118  }
119  }
120  fileStream.close();
121 
122  // If search line was found, write file from buffer.
123  if(bMatch){
124  std::fstream fileStream2;
125 
126  fileStream2.open( fileName.c_str(), std::fstream::out );
127 
128  for(unsigned int iline = 0; iline < fileContents.size(); ++iline)
129  fileStream2 << fileContents[iline] << std::endl;
130 
131  fileStream2.close();
132 
133  //rename( ( fileName ).c_str(), fileName.c_str() );
134  }
135 }
136 
137 void FileToolKit::InsertLineBefore( const std::string &fileName,
138  const std::string &newLine,
139  const std::string &searchLine ){
140 
141  bool bMatch = false;
142 
143  std::vector< std::string > fileContents;
144  char lineBuffer[256];
145 
146  std::fstream fileStream;
147 
148  fileStream.open(fileName.c_str());
149  while( fileStream.good() ){
150  fileStream.getline( lineBuffer, 256 );
151  if(strcmp(lineBuffer, searchLine.c_str()) == 0){
152  fileContents.push_back( newLine );
153  bMatch = true;
154  }
155  fileContents.push_back( lineBuffer );
156  }
157  fileStream.close();
158 
159  if(bMatch){
160  std::fstream fileStream2;
161 
162  fileStream2.open( fileName.c_str(), std::fstream::out );
163 
164  for(unsigned int iline = 0; iline < fileContents.size(); ++iline){
165  fileStream2 << fileContents[iline] << std::endl;
166  }
167 
168  fileStream2.close();
169 
170  //rename( (fileName ).c_str(), fileName.c_str());
171  }
172 }
173 
174 bool FileToolKit::fileExists( const std::string &fileName){
175 
176  std::fstream filestr;
177 
178  filestr.open (fileName.c_str(), std::fstream::in );
179  if (filestr.is_open()){
180  filestr.close();
181  return true;
182  }
183  else
184  {
185  return false;
186  }
187 }
int i
Definition: DBlmapReader.cc:9
void InsertLineAfter(const std::string &fileName, const std::string &newLine, const std::string &searchLine)
Definition: FileToolKit.cc:97
uint16_t size_type
int MakeDir(std::string dirName, mode_t writeMode)
Definition: FileToolKit.cc:38
bool fileExists(const std::string &fileName)
Definition: FileToolKit.cc:174
tuple result
Definition: query.py:137
tuple out
Definition: dbtoconf.py:99
void InsertLineBefore(const std::string &fileName, const std::string &newLine, const std::string &searchLine)
Definition: FileToolKit.cc:137
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=std::string(" "))
Definition: FileToolKit.cc:16
void MakeEmptyWebPage(const std::string &fileName, const std::string &title)
Definition: FileToolKit.cc:79