#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/regex.hpp>
using namespace boost::filesystem;
using namespace std;
// Apply Regular Expression + substitution
string process_str(string str, string regex, string substitution) {
cout << "applying regular expression.. " << regex << endl;
string result=string();
boost::regex reg(regex);
result=boost::regex_replace(str,reg,substitution);
return result;
}
// Read file fname and return its contents in string format
string load_data_in_str(string fname) {
// ifstream fIn;
// Error 1 error C2872: 'ifstream' : ambiguous symbol
// Because ifstream is defined by 2 namespaces:
// namespace std and boost::filesystem (!)
// Use either std::ifstream fIn; either boost::filesystem::ifstream fIn;
// If I use boost::filesystem::ifstream
boost::filesystem::ifstream fIn;
// Obj fIn can has as argument string fname:
// fIn.open(fname,std::ios::in);
// either obj boost::filesystem::path:
// fIn.open(path(fname),std::ios::in);
// But for comparibility with std::ifstream I use:
fIn.open(fname.c_str(),std::ios::in);
if (!fIn) {
cerr << "string load_data_in_str(string fname)" << endl;
cerr << "Error reading the file: " << fname << endl;
getchar();
exit(0);
}
string l;
stringstream ss;
ss << fIn.rdbuf();
return ss.str();
}
// Write to filename fname contents of string data (with default the overwrite: append=false)
void write_str_in_file(string fname, string data, bool append=false) {
// For the same reason as in function string load_data_in_str(string fname)
// I choose with lib I will use from namespaces: (boost::filesystem:: vs std::).
boost::filesystem::ofstream fOut;
if (append)
fOut.open(fname.c_str(),ios::app);
else
fOut.open(fname.c_str(),ios::out);
if (!fOut) {
cerr << "void write_str_in_file(string fname, string data, bool append)" << endl;
cerr << "Error writing to file: " << fname << endl;
getchar();
exit(0);
}
fOut << data;
fOut.close();
}
int main(int argc, char* argv[]) {
if (argc!=5) {
cerr << "Usage: " << argv[0]
<< " [ files dir ] [ reg ex ] [ substritution ] [ out file dir ]" << endl;
cerr << "Press <enter> to continue.." << endl;
getchar();
return (EXIT_FAILURE);
}
// template <class Path> Path system_complete(const Path& p);
// Effects: Composes a complete path from p, using the same rules
// used by the operating system to resolve a path passed as the
// filename argument to standard library open functions.
// Returns: The composed path.
// Postcondition: For the returned path, rp, rp.is_complete() is true.
// Throws: If p.empty().
// Returns: The composed path.
// [Note: For POSIX, system_complete(p) has the same semantics as complete(p, current_path()).
// For Windows, system_complete(p) has the same semantics as complete(ph, current_path())
// if p.is_complete() || !p.has_root_name() or p and base have the same root_name().
// Otherwise it acts like complete(p, kinky), where kinky is the current directory for
// the p.root_name() drive. This will be the current directory of that drive the last
// time it was set, and thus may be residue left over from a prior program run by the
// command processor! Although these semantics are often useful, they are also very error-prone.
path InputPath=system_complete(path(argv[1],native));
// in #include <stdlib.h> are defined EXIT_FAILURE and EXIT_SUCCESS
if (!exists(InputPath)) {
cerr << "Error: the directoty " << InputPath
<< " does not exist." << endl;
return (EXIT_FAILURE);
}
if (!is_directory(InputPath)) {
cout << InputPath << " is not a directory!" << endl;
return (EXIT_SUCCESS);
}
path OutputPath=system_complete(path(argv[4],native));
if (!exists(OutputPath)) {
cerr << "Error: the directoty " << OutputPath
<< " does not exist." << endl;
return (EXIT_FAILURE);
}
if (!is_directory(OutputPath)) {
cout << InputPath << " is not a directory!" << endl;
return (EXIT_SUCCESS);
}
directory_iterator end;
for (directory_iterator it(InputPath); it!= end; ++it) {
cout << it->leaf() << endl;
cout << it->string();
string load_txt;
if (is_directory(*it))
cout << " (dir)";
else {
// Read file into the string load_txt
load_txt=load_data_in_str(it->string());
// Substitute the match of regular expression argv[2]
// with the text of substitution argv[3].
load_txt=process_str(load_txt, argv[2], argv[3]);
/// write_str_in_file(OutputPath.directory_string()+(it->leaf()),load_txt);
// basic_path& operator/=(const basic_path& rhs);
// Effects: The path stored in rhs is appended to the stored path.
// The following function as we read from documenation @ www.boost.org
// http://www.boost.org/libs/filesystem/doc/tr2_proposal.html#basic_path-observers
// path(OutputPath)/=path(it->leaf())
// Gets OutputPath = OutputPath + / + path(it->leaf());
// It concatenates path + filename.
// At the end with .string() we convert to string in order to pass it as argument
write_str_in_file((path(OutputPath)/=path(it->leaf())).string(),load_txt);
}
cout << endl;
}
getchar();
return (EXIT_SUCCESS);
}
Programming links:
20071102
C++ Boost: Reading all the text files of directory and apply a substitution using a regular expression.
Εγγραφή σε:
Σχόλια ανάρτησης (Atom)
Πληροφορίες
Αρχειοθήκη ιστολογίου
-
►
2010
(5)
- ► Σεπτεμβρίου (1)
- ► Φεβρουαρίου (2)
-
►
2008
(14)
- ► Δεκεμβρίου (3)
- ► Φεβρουαρίου (1)
- ► Ιανουαρίου (5)
-
▼
2007
(40)
- ► Δεκεμβρίου (4)
-
▼
Νοεμβρίου
(11)
- Newline terminator in Unix/Linux, Windows and Mac ...
- Convert a tab delimited file to CSV format using R...
- Remove all "^M" characters from a file using vi.
- Convert all new lines to tab delimited using regex...
- Regular expressions links/briefly.
- Λεξικά για Firefox/Thunderbird, Open Office για Ελ...
- C++: Defining/initializing static member(s) within...
- C++: Example of class with ctor, increment operato...
- C++: difference between prefix and suffix incremen...
- Visual Studio .NET 2005: Static library creation/u...
- C++ Boost: Reading all the text files of directory...
Ετικέτες
- C++ (25)
- Unix/Linux (9)
- SuSE 10.2 (8)
- boost (7)
- regex (6)
- Windows (5)
- Windows Vista (4)
- functions (4)
- vim (4)
- Visual Studio .NET 2005 (3)
- firefox (3)
- Windows 7 (2)
- asus eee 901 (2)
- open office (2)
- thunderbird (2)
- video tools (2)
- LaTeX (1)
- Praat Script (1)
- adobe cs4 (1)
- apache (1)
- files (1)
- laptop repair (1)
- php/MySQL (1)
- ubuntu 10.04 (1)
- ubuntu 12.04 (1)
- ubuntu 9.04 (1)
- ubuntu eee (1)
- western digital (1)
- wordpress (1)

Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου