CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions
FileToolKit Class Reference

#include <FileToolKit.h>

Inheritance diagram for FileToolKit:
HCAL_HLX::ROOTSchema

Public Member Functions

bool fileExists (const std::string &fileName)
 
 FileToolKit ()
 
void InsertLineAfter (const std::string &fileName, const std::string &newLine, const std::string &searchLine)
 
void InsertLineBefore (const std::string &fileName, const std::string &newLine, const std::string &searchLine)
 
int MakeDir (std::string dirName, mode_t writeMode)
 
void MakeEmptyWebPage (const std::string &fileName, const std::string &title)
 
void Tokenize (const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=std::string(" "))
 
 ~FileToolKit ()
 

Detailed Description

Definition at line 8 of file FileToolKit.h.

Constructor & Destructor Documentation

FileToolKit::FileToolKit ( )
inline

Definition at line 11 of file FileToolKit.h.

11 {}
FileToolKit::~FileToolKit ( )
inline

Definition at line 12 of file FileToolKit.h.

12 {}

Member Function Documentation

bool FileToolKit::fileExists ( const std::string &  fileName)

Definition at line 173 of file FileToolKit.cc.

References recoMuon::in.

173  {
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 }
void FileToolKit::InsertLineAfter ( const std::string &  fileName,
const std::string &  newLine,
const std::string &  searchLine 
)

Definition at line 96 of file FileToolKit.cc.

References ntuplemaker::iline, recoMuon::in, and dbtoconf::out.

98  {
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 }
tuple out
Definition: dbtoconf.py:99
void FileToolKit::InsertLineBefore ( const std::string &  fileName,
const std::string &  newLine,
const std::string &  searchLine 
)

Definition at line 136 of file FileToolKit.cc.

References ntuplemaker::iline, and dbtoconf::out.

138  {
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 }
tuple out
Definition: dbtoconf.py:99
int FileToolKit::MakeDir ( std::string  dirName,
mode_t  writeMode 
)

Definition at line 38 of file FileToolKit.cc.

References i, and Tokenize().

38  {
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 }
int i
Definition: DBlmapReader.cc:9
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=std::string(" "))
Definition: FileToolKit.cc:16
void FileToolKit::MakeEmptyWebPage ( const std::string &  fileName,
const std::string &  title 
)

Definition at line 78 of file FileToolKit.cc.

References dbtoconf::out.

79  {
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 }
tuple out
Definition: dbtoconf.py:99
void FileToolKit::Tokenize ( const std::string &  str,
std::vector< std::string > &  tokens,
const std::string &  delimiters = std::string(" ") 
)

Definition at line 16 of file FileToolKit.cc.

References pos.

Referenced by MakeDir().

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 }
uint16_t size_type