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 errsv = 0;
44 
45  vector< string > tkDirName;
46  string currentDirName = "";
47 
48  Tokenize(dirName, tkDirName, "/");
49 
50  if(dirName[0] == '/') currentDirName += "/";
51 
52  struct stat mStat;
53 
54  for(unsigned int i = 0; i < tkDirName.size(); ++i ){
55 
56  currentDirName += tkDirName[i];
57  currentDirName += "/";
58 
59  errno = 0;
60  stat( currentDirName.c_str(), &mStat );
61  errsv = errno;
62 
63  if( errsv == 2 ){ // No such file or directory
64 
65  errno = 0;
66  mkdir( currentDirName.c_str(), writeMode);
67  errsv = errno;
68  if( errno == 0 ){
69  errno = 0;
70  chmod( currentDirName.c_str(), writeMode);
71  errsv = errno;
72  }
73  }
74  }
75  return errsv;
76 }
77 
78 void FileToolKit::MakeEmptyWebPage( const std::string &fileName,
79  const std::string &title ){
80 
81  std::fstream fileStream;
82 
83  fileStream.open( fileName.c_str(), std::fstream::out);
84 
85  fileStream << "<html>" << std::endl;
86  fileStream << "<title>" << std::endl;
87  fileStream << title << std::endl;
88  fileStream << "</title>" << std::endl;
89  fileStream << "<body>" << std::endl;
90  fileStream << "</body>" << std::endl;
91  fileStream << "</html>" << std::endl;
92 
93  fileStream.close();
94 }
95 
96 void FileToolKit::InsertLineAfter( const std::string &fileName,
97  const std::string &newLine,
98  const std::string &searchLine){
99 
100  bool bMatch = false;
101 
102  std::vector< std::string > fileContents;
103  char lineBuffer[256];
104 
105  std::fstream fileStream;
106 
107  // Read file into memory and insert new line.
108  fileStream.open(fileName.c_str(), std::fstream::in );
109  while( fileStream.good() ){
110  fileStream.getline( lineBuffer, 256 );
111 
112  fileContents.push_back( lineBuffer );
113 
114  if( strcmp( lineBuffer, searchLine.c_str() ) == 0 ){
115  fileContents.push_back( newLine );
116  bMatch = true;
117  }
118  }
119  fileStream.close();
120 
121  // If search line was found, write file from buffer.
122  if(bMatch){
123  std::fstream fileStream2;
124 
125  fileStream2.open( fileName.c_str(), std::fstream::out );
126 
127  for(unsigned int iline = 0; iline < fileContents.size(); ++iline)
128  fileStream2 << fileContents[iline] << std::endl;
129 
130  fileStream2.close();
131 
132  //rename( ( fileName ).c_str(), fileName.c_str() );
133  }
134 }
135 
136 void FileToolKit::InsertLineBefore( const std::string &fileName,
137  const std::string &newLine,
138  const std::string &searchLine ){
139 
140  bool bMatch = false;
141 
142  std::vector< std::string > fileContents;
143  char lineBuffer[256];
144 
145  std::fstream fileStream;
146 
147  fileStream.open(fileName.c_str());
148  while( fileStream.good() ){
149  fileStream.getline( lineBuffer, 256 );
150  if(strcmp(lineBuffer, searchLine.c_str()) == 0){
151  fileContents.push_back( newLine );
152  bMatch = true;
153  }
154  fileContents.push_back( lineBuffer );
155  }
156  fileStream.close();
157 
158  if(bMatch){
159  std::fstream fileStream2;
160 
161  fileStream2.open( fileName.c_str(), std::fstream::out );
162 
163  for(unsigned int iline = 0; iline < fileContents.size(); ++iline){
164  fileStream2 << fileContents[iline] << std::endl;
165  }
166 
167  fileStream2.close();
168 
169  //rename( (fileName ).c_str(), fileName.c_str());
170  }
171 }
172 
173 bool FileToolKit::fileExists( const std::string &fileName){
174 
175  std::fstream filestr;
176 
177  filestr.open (fileName.c_str(), std::fstream::in );
178  if (filestr.is_open()){
179  filestr.close();
180  return true;
181  }
182  else
183  {
184  return false;
185  }
186 }
int i
Definition: DBlmapReader.cc:9
void InsertLineAfter(const std::string &fileName, const std::string &newLine, const std::string &searchLine)
Definition: FileToolKit.cc:96
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:173
tuple out
Definition: dbtoconf.py:99
void InsertLineBefore(const std::string &fileName, const std::string &newLine, const std::string &searchLine)
Definition: FileToolKit.cc:136
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:78