0

I am just starting out in C++.

I am writing a console application, to "read in" an .evt file (custom, not to be confused with Event viewer files in Windows) and its contents but now I need to write a method to.

a) Store each block of 'EVENT X' including but also ending at 'END'.

b) Make the contents of each block searchable/selectable.

If the content wasn't so 'wildly' varied, I would be happy to put this into some SQL table or experiment with an array but I don't know a starting point to do this as the number of 'fields' or parameters varies. The maximum number of lines I have seen in a block is around 20, the maximum number of parameters per line I have seen is around 13.

I'm not asking for an explicit answer or the whole code to do it although it is welcome, just a generic sample of code to get started that might be appropriate.

This my function to just load the data as it is.

void event_data_loader()
{
    string evt_data;
    string response2;

    cout << "You have chosen to Create/Load Soma events\n\n";
    ifstream named_EVT("C:/evts/1.evt");


    while (getline(named_EVT, evt_data))
    {
        // Output the text from the file
        cout << evt_data << "\n"; // Iterate out each line of the EVT file including spaces


        //name_EVT.close();*/


    }
    cout << "Does the output look ok?(Y/N)";
    cin >> response2;

    if (response2 == "Y")
    {
     // Vectors? Dynamic array? to re-arrange the data?
}
}

The files themselves have content like this. I know what most of the functions do, less so all of the parameters. For some reason putting this on the page it puts them into a single line.

EVENT 01

A CHECK_HUMAN

A CHECK_POSITION 1 250 90 350 90

E BBS_OPEN 1 0

END

EVENT 02

E SELECT_MSG 336 363 314 337 03 338 12 -1 -1

END

EVENT 03

E RUN_EVENT 761

E RUN_EVENT 04

E RUN_EVENT 05

END

EVENT 761

A EXIST_ITEM 373 1

E SELECT_MSG 857 315 762 316 763 -1 -1 -1 -1

E RETURN

END

EVENT 762

A EXIST_ITEM 373 1

E ROB_ITEM 373 1

E SHOW_MAGIC 6

E CHANGE_HP 1 10000

E CHANGE_MP 1 10000

E MESSAGE_NONE 858

E RETURN

END

EVENT 1862

A ABSENT_EVENT 1582

A EXIST_ITEM 1800 1

A EXIST_ITEM 1801 1

A EXIST_ITEM 1802 1

A EXIST_ITEM 1803 1

A EXIST_ITEM 1804 1

A EXIST_ITEM 1805 1

A EXIST_ITEM 1806 1

A EXIST_ITEM 1807 1

A WEIGHT 365 1854 1 1832 1 -1 1 -1 -1 -1 -1

A CHECK_ITEMSLOT 393 1854 1 1832 1 -1 1 -1 -1 -1 -1

A GENDER 1

E ADD_EVENT 1582

E MESSAGE_NONE 3237

E ROB_ITEM 1800 1

E ROB_ITEM 1801 1

E ROB_ITEM 1802 1

E ROB_ITEM 1803 1

E ROB_ITEM 1804 1

E ROB_ITEM 1805 1

E ROB_ITEM 1806 1

E ROB_ITEM 1807 1

E GIVE_ITEM 1854 1

E GIVE_ITEM 1832 1

E RETURN

END

Phoxeh
  • 13
  • 3
  • I think you mean "explicit" answer ;) – Anonymous1847 Nov 10 '20 at 16:30
  • Just read the file line by line to `std::string` which you can push to `std::vector`. After that you can [split the strings](https://stackoverflow.com/a/37454181/4165552) using space character as delimiter, and then just do whatever you need to do. I think the question is too broad to write a good answer. – pptaszni Nov 10 '20 at 16:34
  • @anonymous1847 Thank you. – Phoxeh Nov 10 '20 at 16:43
  • @pptaszni Thank you that is the general start I guess I need. – Phoxeh Nov 10 '20 at 16:46

1 Answers1

0

I would do something like this:

struct Subevent {
    std::string selector;
    std::string name;
    std::vector<int> params;
};

struct Event {
    int id;
    std::vector<Subevent> subevents;
};

std::vector<Event> load_events(std::istream& input_stream) {
    std::vector<Event> out;
    Event current_event {}; // current event being built
    std::string line;
    bool inside_event = false; // are we inside the scope of an event?
    while (std::getline(input_stream, line)) {
        // strip trailing whitespace
        while (isspace(line.back())) {
            line.pop_back();
        }
        // skip empty lines
        if (line.size() == 0) {
            continue;
        }
        // read first token (until first space)
        std::stringstream ss(line);
        std::string first_token;
        ss >> first_token;
        bool is_new_event_line = first_token == "EVENT";
        bool is_end_line = first_token == "END";
        if (is_new_event_line) {
            // line: EVENT <id>
            if (inside_event) {
                // error: "not expecting new event"
                // choose your own error messaging method
            }
            int id;
            ss >> id; // read <id>
            // setup new event
            current_event.id = id;
            inside_event = true;
        }
        else if (is_end_line) {
            // line: END
            if (!inside_event) {
                // error: "unexpected END"
            }
            // record and clear current event
            out.push_back(current_event);
            inside_event = false;
            current_event = Event();
        }
        else {
            // line: <selector> <name> <params...>
            // e.g.: A          GENDER 1
            if (!inside_event) {
                // error: "unexpected property entry"
            }
            // read subevent
            Subevent subevent {};
            subevent.selector = first_token;
            ss >> subevent.name;
            // copy over the int params from the line
            std::copy(
                std::istream_iterator<int>(ss),
                std::istream_iterator<int>(),
                std::back_inserter(subevent.params)
            );
            // push back subevent
            event.subevents.push_back(subevent);
        }
    }
    return out;
}
Anonymous1847
  • 2,518
  • 8
  • 15
  • Thank you @anonymous1847 I don't understand enough of it but its more than enough for me to 'step through' each part of that code and get it to do what I want, how I want. Appreciate the help :) – Phoxeh Nov 10 '20 at 21:45
  • @Phoxeh I added some more comments, if it helps. – Anonymous1847 Nov 10 '20 at 21:50
  • Thank you a lot for this, I am trying to integrate it into my existing code and having great difficulty but I now have no more errors. However... I dont know how to run it to see it work? I'm sorry :[ – Phoxeh Nov 12 '20 at 21:26