Parsing Data using std::string

Config from Network Stream

What changes would be needed to load the config from a different source, like a network stream, instead of a file?

Abstract art representing computer programming

Loading a configuration from a network stream instead of a file involves a few key changes. The main difference is how we initially get the data into a std::string. Instead of using SDL_RWFromFile(), we'll need functions to receive data over the network.

Network Communication

For network communication, we might use libraries like SDL_net or platform-specific APIs. For simplicity, let's assume we have a hypothetical function ReceiveNetworkData that can read data from a network stream and return it as a std::string.

Adapting ReadFile()

Our original ReadFile() function used SDL_RWops to read from a file. We'll need a new function, let's call it ReadNetworkData(), that uses our network function to get the data.

We'll also update our Load function

#include <iostream>
#include <string>

// Hypothetical function - implementation
// depends on your network library
std::string ReceiveNetworkData() {
  return "WINDOW_TITLE: Networked Window\n"
    "WINDOW_WIDTH: 800";
}

class Config {
public:
  std::string WindowTitle;
  int WindowWidth;
  void Load(const std::string& Path) {
    std::string Content{ReadFile(Path)};
    if (Content.empty()) return;
    ParseConfig(Content);
  }

  void LoadFromNetwork() {
    std::string Content{ReadNetworkData()};
    if (Content.empty()) return;
    ParseConfig(Content);
  }

  void ParseConfig(const std::string& Content) {
    size_t Start{ 0 };
    size_t End{ Content.find('\n', Start) };
    while (End != std::string::npos) {
      ProcessLine(Content.substr(
        Start, End - Start));
      Start = End + 1;
      End = Content.find('\n', Start);
    }

    ProcessLine(Content.substr(Start));
  }
    
  void ProcessLine(const std::string& Line) {
    size_t Delim{ Line.find(": ") };
    if (Delim == std::string::npos) return;

    std::string Key{ Line.substr(0, Delim) };
    std::string Value{ Line.substr(Delim + 2) };

    if (Key == "WINDOW_TITLE") {
      WindowTitle = Value;
    }
    else if (Key == "WINDOW_WIDTH") {
      WindowWidth = std::stoi(Value);
    }
  }

 private:
  std::string ReadFile(const std::string& Path) {
    return ""; // Not used in this example
  }

  std::string ReadNetworkData() {
    return ReceiveNetworkData();
  }
};

int main() {
  Config C;
  C.LoadFromNetwork();
  std::cout << C.WindowTitle << "\n";
  std::cout << C.WindowWidth << "\n";
}
Networked Window
800

Other Considerations

  • Error Handling: Network communication is more prone to errors than local file reading. You'll need robust error handling to manage timeouts, disconnects, and other network issues.
  • Buffering: Network data might arrive in chunks. You might need to buffer the incoming data until you have a complete configuration.
  • Protocol: You'll need a defined protocol to ensure that both the sender and receiver understand how the configuration data is structured and transmitted over the network.

Answers to questions are automatically generated and may not have been reviewed.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 79 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2025 - All Rights Reserved