|
| 1 | +#include <string> |
| 2 | +#include <istream> |
| 3 | +#include <iostream> |
| 4 | +#include <fstream> |
| 5 | +#include <sstream> |
| 6 | +#include <vector> |
| 7 | +#include <map> |
| 8 | +#include <algorithm> |
| 9 | +#include <locale> |
| 10 | + |
| 11 | +struct DataRow { |
| 12 | + unsigned long sheet; |
| 13 | + unsigned long id1; |
| 14 | + unsigned long id2; |
| 15 | + unsigned long id3; |
| 16 | + unsigned long id4; |
| 17 | + std::wstring loc; |
| 18 | + |
| 19 | + ///overloading operator < because of std::sort function |
| 20 | + bool operator < (const DataRow& row) const { |
| 21 | + if (sheet == row.sheet) { |
| 22 | + if (id1 == row.id1) { |
| 23 | + if (id2 == row.id2) { |
| 24 | + if (id3 == row.id3) { |
| 25 | + return (id4 < row.id4); |
| 26 | + } |
| 27 | + return (id3 < row.id3); |
| 28 | + } |
| 29 | + return (id2 < row.id2); |
| 30 | + } |
| 31 | + return (id1 < row.id1); |
| 32 | + } |
| 33 | + return (sheet < row.sheet); |
| 34 | + } |
| 35 | + |
| 36 | + friend std::wistream& operator>>(std::wistream& in, DataRow& data); |
| 37 | + friend std::wofstream& operator<<(std::wofstream& out, DataRow& data); |
| 38 | +}; |
| 39 | + |
| 40 | +///overloading operator >>, so I can use it to read DataRow type |
| 41 | +std::wistream& operator>>(std::wistream& is, DataRow& data) |
| 42 | +{ |
| 43 | + is >> data.sheet; |
| 44 | + is >> data.id1; |
| 45 | + is >> data.id2; |
| 46 | + is >> data.id3; |
| 47 | + is >> data.id4; |
| 48 | + is >> std::ws; //discard whitespace |
| 49 | + std::getline(is, data.loc); |
| 50 | + |
| 51 | + return is; |
| 52 | +} |
| 53 | + |
| 54 | +///overloading operator <<, so I can use it to write DataRow type |
| 55 | +std::wofstream& operator<<(std::wofstream& os, DataRow& data) |
| 56 | +{ |
| 57 | + os << data.sheet << L'\t'; |
| 58 | + os << data.id1 << L'\t'; |
| 59 | + os << data.id2 << L'\t'; |
| 60 | + os << data.id3 << L'\t'; |
| 61 | + os << data.id4 << L'\t'; |
| 62 | + os << data.loc; |
| 63 | + |
| 64 | + return os; |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +int main(int argc, char *argv[]) |
| 69 | +{ |
| 70 | + if (argc < 4) |
| 71 | + { |
| 72 | + std::cerr << "Usage: " << argv[0] << " <replace> <original> <target>\n\n" |
| 73 | + << "<replace>\t: file which contains translated/modified strings\n" |
| 74 | + << "<original>\t: original file with string table\n" |
| 75 | + << "<target>\t: output file" << std::endl; |
| 76 | + return 1; |
| 77 | + } |
| 78 | + |
| 79 | + std::string replace_name = argv[1]; |
| 80 | + std::string original_name = argv[2]; |
| 81 | + std::string target_name = argv[3]; |
| 82 | + |
| 83 | + std::wifstream replace(replace_name, std::ios::binary); |
| 84 | + if (replace.fail()){ |
| 85 | + std::cerr << "Error opening " << replace_name << std::endl; |
| 86 | + return 1; |
| 87 | + } |
| 88 | + |
| 89 | + std::wifstream original(original_name, std::ios::binary); |
| 90 | + if (original.fail()){ |
| 91 | + std::cerr << "Error opening " << original_name << std::endl; |
| 92 | + return 1; |
| 93 | + } |
| 94 | + |
| 95 | + std::wofstream target(target_name, std::ios::binary); |
| 96 | + if (target.fail()){ |
| 97 | + std::cerr << "Error opening " << target_name << std::endl; |
| 98 | + return 1; |
| 99 | + } |
| 100 | + |
| 101 | + ///create lookup table |
| 102 | + std::vector<DataRow> lookup; |
| 103 | + std::wstring row; |
| 104 | + |
| 105 | + std::cout << "Reading \"" << replace_name << "\"..." ; |
| 106 | + while (std::getline(replace, row)){ |
| 107 | + DataRow dataRow; |
| 108 | + std::wstringstream ss; |
| 109 | + |
| 110 | + ss.str(row); |
| 111 | + ss >> dataRow; |
| 112 | + |
| 113 | + lookup.push_back(dataRow); |
| 114 | + } |
| 115 | + std::cout << " (" << lookup.size() << " lines found)" << std::endl; |
| 116 | + |
| 117 | + ///sorting is necessary so I can speed up searching using a map (lookupHelper) |
| 118 | + std::sort(lookup.begin(), lookup.end()); |
| 119 | + |
| 120 | + ///lookupHelper is a map which assigns pair of pointers, start and end (something like row numbers), |
| 121 | + ///to the sheet number (first number on the row in text file) |
| 122 | + std::map<int, std::pair<std::vector<DataRow>::iterator, std::vector<DataRow>::iterator> > lookupHelper; |
| 123 | + std::vector<DataRow>::iterator it, first, last; |
| 124 | + first = lookup.begin(); |
| 125 | + int sheet = first->sheet; |
| 126 | + for (it = lookup.begin(); it != lookup.end(); ++it) { |
| 127 | + last = it; |
| 128 | + if (sheet < it->sheet){ |
| 129 | + lookupHelper[sheet] = std::make_pair(first, --last); ///last is now the first line of the new sheet, so we need to get previous value |
| 130 | + first = it; |
| 131 | + sheet = it->sheet; |
| 132 | + } |
| 133 | + } |
| 134 | + lookupHelper[sheet] = std::make_pair(first, last); ///write the last one |
| 135 | + |
| 136 | + //main loop |
| 137 | + std::cout << "Copying lines from \"" << original_name << "\" to \"" << target_name << "\"..." << std::endl; |
| 138 | + |
| 139 | + bool found = false; |
| 140 | + unsigned int changed = 0, unchanged = 0; |
| 141 | + std::map<int, std::pair<std::vector<DataRow>::iterator, std::vector<DataRow>::iterator> >::iterator iHelp; ///I love this type! |
| 142 | + |
| 143 | + while (std::getline(original, row)){ |
| 144 | + DataRow originalRow; |
| 145 | + std::wstringstream ss; |
| 146 | + |
| 147 | + ss.str(row); |
| 148 | + ss >> originalRow; |
| 149 | + |
| 150 | + found = false; |
| 151 | + iHelp = lookupHelper.find(originalRow.sheet); |
| 152 | + if (iHelp != lookupHelper.end()){ |
| 153 | + it = iHelp->second.first; ///I don't want to search for key multiple times using map[key], so I'm using less comprehensible |
| 154 | + while (it <= iHelp->second.second) { ///dereferencing form iterator. It points to map pair<key, value> and I have stored in value |
| 155 | + if (originalRow.sheet == it->sheet && ///another pair. It looks like this: pair<key, value<first, second>> |
| 156 | + originalRow.id1 == it->id1 && ///Pair members are accessible as .first and .second, so I have to use: pointer->second.first |
| 157 | + originalRow.id2 == it->id2 && |
| 158 | + originalRow.id3 == it->id3 && |
| 159 | + originalRow.id4 == it->id4){ |
| 160 | + |
| 161 | + found = true; |
| 162 | + break; |
| 163 | + } else { |
| 164 | + it++; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if (found){ |
| 170 | + originalRow.loc = it->loc; |
| 171 | + changed++; |
| 172 | + } else { |
| 173 | + unchanged++; |
| 174 | + } |
| 175 | + target << originalRow << '\n'; |
| 176 | + } |
| 177 | + |
| 178 | + std::cout << "Processed lines: " << changed + unchanged << " (replaced: " << changed << ", original: " << unchanged << ")\nDone!" << std::endl; |
| 179 | + |
| 180 | + return 0; |
| 181 | +} |
0 commit comments