Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* txt_filter: Operates on plaintext/xml files,
- * replacing any <tagnames> to uppercase <TAGNAMES>.
- * Equivalent to running
- #!/bin/sh
- cat input.xml | \
- sed 's@\<\([a-z]\+\)\>@\U\1@g' | \
- sed 's@\<\([a-z]\+\)\( \+.*\)\>@\U\1\L\2@g' > output.xml
- */
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- int main()
- {
- const string filename = "input.txt";
- const string filename_out = "output.txt";
- ifstream istrm(filename, ios::in);
- if (!istrm.is_open()) {
- cerr << "failed to open " << filename << endl;
- return 1;
- }
- ofstream ostrm(filename_out, ios::out);
- string linebuf, tagname;
- string tag, tag_upper;
- int count_replaced = 0;
- // locale loc = locale();
- while (!istrm.eof()) {
- getline(istrm, linebuf, '\n');
- // cerr << linebuf << endl;
- // Find an opening XML tag: '<', alphabetics [a-zA-Z], '>'
- const size_t pos1 = linebuf.find('<');
- const size_t pos2 = linebuf.find('>');
- if ((pos1 == -1UL) || (pos2 == -1UL) ||
- ( !isalpha(linebuf[pos1+1]) && !(linebuf[pos1+1] == '/') )
- ) {
- ; // No tag, skip filtering
- } else {
- // Found a line containing a tag, ex.
- // ' <ingredient amount="3" unit="spoon">Flour</ingredient>'
- // First pass: find the end of tag name
- const size_t pos_tag_start = pos1 + 1;
- size_t pos_tag_end = pos2;
- tag = linebuf.substr(pos_tag_start, pos_tag_end - pos_tag_start);
- clog << "Parsing tagline: " << tag << endl;
- for (size_t i = 0; i<tag.length(); i++) {
- if ((tag[i] == ' ') || (tag[i] == '\t') || (tag[i] == '\n')) {
- pos_tag_end = i + pos_tag_start;
- break;
- }
- }
- // tag_upper = toupper(tag, loc);
- // Second pass: filter only the name
- tagname = linebuf.substr(pos_tag_start, pos_tag_end - pos_tag_start);
- clog << "Found tag: " << tagname << endl;
- tag_upper = tagname; // allocate by copy, then patch
- for (size_t i = 0; i<tag_upper.length(); i++) {
- tag_upper[i] = toup1per(tagname[i]);
- }
- linebuf.replace(pos_tag_start, pos_tag_end - pos_tag_start, tag_upper);
- count_replaced++;
- }
- // cout << linebuf << endl;
- ostrm << linebuf << endl;
- }
- cout << "Replaced " << count_replaced << " tags to uppercase." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement