/*
* Copyright ©2026 Soham Pardeshi. All rights reserved.
* Permission is hereby granted to students registered for University of
* Washington CSE 333 for use solely during Summer Quarter 2026 for
* purposes of the course. No other use, copying, distribution, or
* modification is permitted without prior written consent. Copyrights
* for third-party components of this work must be honored. Instructors
* interested in reusing these course materials should contact the author.
*/
#ifndef _SEARCHSERVER_SOCKETLINEREADER_H_
#define _SEARCHSERVER_SOCKETLINEREADER_H_
#include <string>
#include <vector>
// A SocketLineReader is a class that reads data from a socket into
// an internal buffer, splits the data into lines terminated by '\n',
// and returns lines to the caller one at a time.
class SocketLineReader {
public:
explicit SocketLineReader(int cfd) : cfd_(cfd) { }
virtual ~SocketLineReader() { }
// This method returns the next line of data received from the
// socket to the caller. A line of data is data that terminates
// with a '\n' character.
//
// If a line of data is found, it is returned through the output
// parameter "nextline", and the function returns true.
//
// If EOF or an error occurs before the next line is found,
// returns false.
bool GetNextLine(std::string *line);
// Takes a line and splits it up into zero or more
// whitespace-separated words. Returns a vector of these words.
static std::vector<std::string> SplitIntoWords(const std::string &line);
private:
// The file descriptor we're reading from.
int cfd_;
// The buffer we're reading into.
std::vector<char> buf_;
// The amount we attempt to read on each pass through the main
// read loop.
static const unsigned int kReadAmount = 2048;
};
#endif // _SEARCHSERVER_SOCKETLINEREADER_H_