-2

I give up, I simply want to know why push_back is not working as intended. Forget how bad or over complicated my code is.

I have a python program which reads a buffer, and displays it line by line from a list. If the last line is not complete, remove it from the list and it will be pre-appended to the buffer on the next iteration.

response += s.recv(1024).decode("utf-8")
    if response[-1] != '\n':
        lines = response.splitlines()
        response = lines[-1]
        lines = lines[:-1]
    else:
        lines = response.splitlines()
        response = '';

for line in lines:
    print("New Line:   " + line)

time.sleep(1)

Prefixed with "New Line" so I can see that multiple lines are not hidden in my "line"

This is the output:

New Line:   :tmi.twitch.tv 001 k20stitchbot :Welcome, GLHF!
New Line:   :tmi.twitch.tv 002 k20stitchbot :Your host is tmi.twitch.tv
New Line:   :tmi.twitch.tv 003 k20stitchbot :This server is rather new
New Line:   :tmi.twitch.tv 004 k20stitchbot :-
New Line:   :tmi.twitch.tv 375 k20stitchbot :-
New Line:   :tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
New Line:   :tmi.twitch.tv 376 k20stitchbot :>
New Line:   :tmi.twitch.tv CAP * ACK :twitch.tv/membership    

So I've written the following in C++ with visual studios as my compiler:

    found = start = end = 0;
    iResult = recv(ConnectSocket, recvbuf, recvbuflen - 1, 0);

    if (iResult > 0)
    {
        recvbuf[iResult] = '\0';
        newString.append(recvbuf);
        std::replace(newString.begin(), newString.end(), '\r', ' ');
        end = newString.length();

        while ((found = newString.find("\n", start)) != -1) {
            myList.push_back(newString.substr(start, found));
            start = found + 1;
            if (start >= end) {
                start = end;
            }
            Sleep(1000);
        }

        if (newString.back() != '\n')
        {
            std::string leftOver = newString.substr(start, end);
            newString = leftOver;
        }
        else 
        {
            newString = "";
        }

        for (myListIt = myList.begin(); myListIt != myList.end(); myListIt++)
        {
            cout << "New Line: " << *myListIt << "\n";
        }

        myList.clear();
    }

My Output here is less than desirable, it's almost as if instead of pushing from start to found onto the list, it's pushing from start to end.

New Line: :tmi.twitch.tv 001 k20stitchbot :Welcome, GLHF!
New Line: :tmi.twitch.tv 002 k20stitchbot :Your host is tmi.twitch.tv
:tmi.twitch.tv 003 k20stitchbot :This server is
New Line: :tmi.twitch.tv 003 k20stitchbot :This server is rather new
:tmi.twitch.tv 004 k20stitchbot :-
:tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You
New Line: :tmi.twitch.tv 004 k20stitchbot :-
:tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>

New Line: :tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>

New Line: :tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>

New Line: :tmi.twitch.tv 376 k20stitchbot :>
  • 1
    The python code is overcomplicated in the first place. Why not append to the list only when you recieve the full line, instead of splitting/truncating/... complex, inefficient, and obviously difficult to port to c++. – Jean-François Fabre Jan 21 '17 at 20:55
  • `Sleep(1000);` -- What's the reason to do this? – PaulMcKenzie Jan 21 '17 at 21:02
  • Also, why are you not using a debugger, instead of guessing where the problem is? – PaulMcKenzie Jan 21 '17 at 21:07
  • @Jean-FrançoisFabre The python code is not over complicated. Granted the splitlines() can be pulled out of the if statement, it's easier to use splitlines and remove the last entry if it's not a full line. I never know what I'm going to get from the server. A perfect line, multiple lines, or a partial line. This covers all cases. Regardless, my question is for the c++ implementation. Why is push_back behaving this way? If you know a simpler solution then why not say it? – Antonio Anonymous Jan 21 '17 at 21:13
  • @PaulMcKenzie Why is the sleep relevant? I simply had it there so i could read the text on the screen in bursts without it scrolling to fast. As for debugging, I'm new to visual studios and am not to familiar with its tools. – Antonio Anonymous Jan 21 '17 at 21:15
  • @AntonioAnonymous Because `Sleep` in a program invariably means some sort of synchronization thing going on, and `Sleep` is a poor thing to use for synchronization. – PaulMcKenzie Jan 21 '17 at 21:17
  • @AntonioAnonymous yes it's overcomplicated _and_ CPU/memory intensive. – Jean-François Fabre Jan 21 '17 at 21:17
  • @PaulMcKenzie #include is not available in visual studio so i cannot use usleep. I am simply trying to "wait around" for a second before proceeding. – Antonio Anonymous Jan 21 '17 at 21:21
  • *I simply want to know why push_back is not working as intended* -- There is nothing wrong with `push_back` -- it is working with what you're giving it. If you're giving it wrong data, that isn't push_back's fault. – PaulMcKenzie Jan 21 '17 at 21:22
  • @Jean-FrançoisFabre if 13 lines is over complicated to to split a 1024 byte buffer into multiple lines and only ensure that full lines get printed... what would _italic_ your solution look like in python – Antonio Anonymous Jan 21 '17 at 21:23
  • @AntonioAnonymous -- *if 13 lines is over complicated to to split a 1024 byte buffer into multiple lines and only ensure that full lines get printed.* -- Why not say that in your question, and forget about the Python code? The way to do this in C++ is much simpler than what you tried to translate from Python. – PaulMcKenzie Jan 21 '17 at 21:24
  • @PaulMcKenzie `std::string copyOf = newString.substr(start, (found - 1 ));` I'm printing to start and found -1 before and after the push_back(copyOf) and the start and end points are right, but it pushes_ start to remainder of string. – Antonio Anonymous Jan 21 '17 at 21:25
  • @PaulMcKenzie could you please elaborate on this much simpler way? you said my pseudo code looked good... unfortunately, this was my implementation of it. – Antonio Anonymous Jan 21 '17 at 21:28
  • @AntonioAnonymous Take a step back just for the moment. On a high-level, what are you trying to accomplish? Forget about `push_back` right now -- you have a string of data containing , and you want to -- that's how you should have presented the question, along with an original string, and the result you want to achieve. Showing Python code is irrelevant. – PaulMcKenzie Jan 21 '17 at 21:28
  • @PaulMcKenzie I have a char recvbuf[1024]. It may contain a partial line, a complete line, multiple lines ending with an incomplete line, or multiple lines ending with a complete line. I am merely trying to process each new line (terminated by \n) independently. – Antonio Anonymous Jan 21 '17 at 21:31
  • [Please take a look at this simple sample](http://ideone.com/sM0SGN). That code breaks up a buffer with a `\r` into separate lines. Take that example and expand on it. There are no "start" or "end" variables and myriads of loops used to accomplish breaking up the string on the `\r` character. That sample is what you should have started with, to get familiar with how to handle strings properly and easily. – PaulMcKenzie Jan 21 '17 at 21:42
  • @PaulMcKenzie Thank you, and this is a similar example of what I started with, but it still leaves me with the problem of incomplete llnes. Once i use the getlines function, it strips out the training \n and i can no longer determine if the last line is complete or not. – Antonio Anonymous Jan 21 '17 at 22:24

1 Answers1

1

The substring arguments are: begin-position, length
and not: begin-position, end-position