C++ String Getline Class

Download here. getline

Features of the below string getline class

1) Can be used like getline function for streams

2) Can have multiple delimiters as opposed to the getline function for streams

3) Delimiter need not be single char. It can be a string

4) Pointer to current position in the string is automatically incremented

similar to that in stringstream

5) It is possible to rewind the pointer position to zero using the rewind() method.

6) It is also possible to get single char at the current position of pointer from the string

7) It is possible to get single char at current pointer position if it is one of the

characters of the list.  This is an optional argument to the get method

8) stream getline feature can be realized by calling the getline method of this

class without passing any arguments. No arguments to getline method defaults to

newline char as delimiter

// File : Getline.hh
// Author : A.G.Raja
// License : GPL
// Website : https://agraja.wordpress.com

#include <iostream>

using namespace std;

class Getline {
string text, buffer;
int pos,p;
public:
int str(string txt) {
pos = 0;
this->text = txt;
}
string getline(string delim = “\n”, int delim_type = 1);
char get(string delim = “”);
int unget(int decr = 1) {
pos -= decr;
}
int end() {
if(pos>=text.size()) return 1;
else return 0;
}
int rewind() {
pos = 0;
}
};

string Getline::getline(string delim, int delim_type) {
p = delim_type ? text.find(delim,pos)
: text.find_first_of(delim,pos);
if(p!=-1) {
buffer = text.substr(pos,p-pos);
pos = delim_type ? p+delim.length() : p +1;
} else {
buffer = text.substr(pos);
pos += buffer.length();
} return buffer;
}

char Getline::get(string delim) {
if(delim.empty() and pos<text.size()) return text[pos++];
if(delim.find(text[pos])!=-1) {
pos++;
return text[pos-1];
}
return 0; // must
}

// End of File Getline.hh

// File : test_Getline.cpp
// Author : A.G.Raja
// License : GPL
// Website : https://agraja.wordpress.com

#include <iostream>
#include “Getline.hh”

using namespace std;

int main() {
Getline g;
g.str(“Hi, my name is Raja\nI’m an Engineer”);
// Get the substring terminated by “y na”
cout<<g.getline(“y na”)<<endl;
// Result: “Hi, m”

// Get substring terminated by first of “sji”
cout<<g.getline(“sji”,0)<<endl;

// Get line terminated by ‘\n’
cout<<g.getline()<<endl;
// Result: “s Raja”

// Get single char at current position of pointer
cout<<g.get()<<endl;
// Result: “I”

// Get a char at current position of pointer
// if it is in the list “_$%’#”
cout<<g.get(“_$%’#”)<<endl;
// Result: “‘”

// Unget single char, unget 4 chars
g.unget(); g.unget(4);
}

Download here. getline

Advertisements

One thought on “C++ String Getline Class

  1. Can you please show how to parse with multiple delimiters? What I mean by that is to parse a string until either one of chosen delimiters is found, I hope this is what you meant by
    “2) Can have multiple delimiters as opposed to the getline function for streams”

    Thanks in advance!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s