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 174 of file FileToolKit.cc.

References recoMuon::in.

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

Definition at line 97 of file FileToolKit.cc.

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

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

Definition at line 137 of file FileToolKit.cc.

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

139  {
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 }
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, query::result, and Tokenize().

38  {
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 }
int i
Definition: DBlmapReader.cc:9
tuple result
Definition: query.py:137
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 79 of file FileToolKit.cc.

References dbtoconf::out.

80  {
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 }
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