refix 1.0
Unix filter utility that maintains footnotes in a text file.
Functions
main.cpp File Reference

The implementation for the refix filter. More...

#include <fstream>
#include <iostream>
#include <regex>

Functions

int main (int argc, const char *argv[])
 Resequences the reference/footnote numbers. More...
 

Detailed Description

The implementation for the refix filter.

Function Documentation

◆ main()

int main ( int  argc,
const char *  argv[] 
)

Resequences the reference/footnote numbers.

Parameters
[in]argc= 1 for normal operation, or = 2 for test mode.
[in]argvCurrently ignored.
Returns
= 0 means no errors, = 1 means no test file.
196 {
197
198 using namespace std::string_literals;
199 const auto testing{argc > 1};
200 enum class SV { // A 'state variable'
201 strt, // Start of scan, looking for first ref
202 inRef, // Started ref processing
203 inFt // Started footnote processing
204 };
205
206/*
207 * using enum SV; Doesn't work yet :-(
208 *
209 * This should not be needed in c++20 .
210 * constexpr auto sStrt = SVenm::strt;
211 * constexpr auto sInRef = SVenm::inRef;
212 * constexpr auto sInFt = SVenm::inFt;
213 */
214 constexpr auto kNoTestFile{1};
215 std::fstream ifile; // Input file
216 if (testing) {
217 if (ifile.open("test.txt"), ifile) ;
218 else return std::cerr << "refix: no file.\n", kNoTestFile;
219 }
220 auto &in{testing ? ifile : std::cin};
221 auto l{""s}; // Line of input
222 auto xptOrd{0}; // The expected refno
223 auto sv{SV::strt}; // State variable
224 while (getline(in, l)) {
225 std::smatch mch; // What we actually matched, if anything.
226 std::regex pat{R"((\[)(\d+)(\]))"}; // A footnote like '[20]'
227 if (regex_search(l, mch, pat)) {
228 ++xptOrd;
229 auto refOrd{stoi(mch.str(2))}; // The seen refno
230 switch (sv) {
231 case SV::strt:
232 refOrd = 1;
233 sv = SV::inRef;
234 break;
235 case SV::inRef:
236 if(refOrd == 1) sv = SV::inFt, xptOrd = 1;
237 break;
238 case SV::inFt:
239 default:
240 break;
241 }
242 l = mch.prefix().str() + "["s +
243 std::to_string(xptOrd) + "]"s +
244 mch.suffix().str();
245 }
246 std::cout << l << '\n';
247 }
248 return 0;
249}