Created
April 3, 2019 15:11
-
-
Save Pk13055/8e7fe3df4f7a1ad8e78dbf9d29ceac8f to your computer and use it in GitHub Desktop.
Utility functions to map common file inputs to vectors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#include <string> | |
#include <fstream> | |
#include <sstream> | |
#include <algorithm> | |
using namespace std; | |
long int n_buffers, buffer_size, output_offset; | |
string input_buffer; | |
vector<int> int_tokens; | |
vector<string> input_tokens; | |
void extractInt(string str) { | |
stringstream ss; | |
ss << str; | |
string temp; | |
int found; | |
while (!ss.eof()) { | |
ss >> temp; | |
if (stringstream(temp) >> found) int_tokens.emplace_back(found); | |
temp = ""; | |
} | |
} | |
void processInput(istream &input) { | |
string buffer(buffer_size, 0); | |
int _iter = 0; | |
while (!input.eof() and _iter < n_buffers - 1) { | |
input.read(buffer.data(), buffer.size()); | |
vector<char> del_chars = {'\n', 'I', 'N', 'D', 'O', 'U', 'T', 'A', 'G', 'E'}; | |
for(auto ch: del_chars) buffer.erase(remove(buffer.begin(), buffer.end(), ch), buffer.end()); | |
input_buffer.insert(input_buffer.end(), buffer.begin(), buffer.end()); | |
_iter++; | |
} | |
output_offset = input.tellg(); | |
extractInt(input_buffer); | |
vector<char> del_nos = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-'}; | |
for(auto ch: del_nos) input_buffer.erase(remove(input_buffer.begin(), input_buffer.end(), ch), input_buffer.end()); | |
istringstream temp_buf(input_buffer); | |
input_tokens = vector<string>(istream_iterator<string>{temp_buf}, | |
istream_iterator<string>()); | |
} | |
signed main() { | |
n_buffers = atoi(argv[2]), buffer_size = atoi(argv[3]); | |
ifstream fin(argv[1], ifstream::binary); | |
processInput(fin); | |
fin.close(); | |
ofstream fout(argv[1], ios::out | ios::app); | |
if(output_offset < 0) | |
fout.seekp(0, ios::end); | |
else | |
fout.seekp(output_offset, ios::beg); | |
auto output = process(int_tokens, input_tokens); // should return ostringstream | |
cout<<output.str(); | |
fout<<output.str(); // if ostringstream and not str | |
fout.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment