#include <fstream>
#include <regex>
#include <string>
#include <iostream>
#include <cstdlib>


int main(int argc, char **argv) {
    using namespace std;
    
    if (argc <= 1) {
        cerr << "File name required" << endl;
        return -1;
    } 
    
    ifstream stream(argv[1], ios::in);
    if (!stream.is_open()) {
        cerr << "Could not open file " << argv[1] << endl;
        return -1;
    }
    
    regex rx("(\\w+):(\\d+)\\$");
    int book_counter = 0;
    string line;
    while (getline(stream, line)) {
        smatch results;
        if (regex_search(line, results, rx)) {
            auto book_name = results.str(1);
            auto book_count = atoi(results.str(2).data());
            book_counter += book_count;
            
            cout << "Entry added: " << book_name << ", count " << book_count << endl;
        }
    }
    
    cout << "Total books: " << book_counter << endl;
    
    return 0;
}
