FBB::ReadLineHistory(3bobcat)

Editing input lines
(libbobcat-dev_6.04.00)

2005-2023

NAME

FBB::ReadLineHistory - std::streambuf offering line-editing and history

SYNOPSIS

#include <bobcat/readlinebuf>
Linking option: -lbobcat -lreadline

DESCRIPTION

The FBB::ReadLineHistory object offers access to the history maintained by FBB::ReadLineBuf and ReadLineStream objects.

The latter two classes use Gnu's readline library to allow editing of input lines. The accumulated history of these objects can be accessed from the ReadLineHistory object.

Since Gnu's readline library maintains global data there can only be one history. The ReadLineHistory class is therefore, like ReadLineBuf a singleton. (Gnu's readline library does, however, offer functions allowing programs to use multiple histories. So it would in principle be possible to design a non-singleton ReadLineHistory class. Since programs normally only interact with a single terminal, there is probably little use for non-singleton ReadLineHistory class).

The ReadLineHistory class encapsulates history access. It offers limited facilities: either forward or backward iterations over the history are offered as well as reading and writing the history from/to streams. When reading the history from a stream it replaces the currently available lines in Gnu's readline history. The content of the history lines and --if defined-- the timestamps of the lines in the history can be obtained using iterators defined by ReadLineHistory.

NAMESPACE

FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.

INHERITS FROM

-

NESTED TYPES

The class ReadLineHistory defines the following nested types:

HistoryElement

The iterators made available by the ReadLineHistory object provide access to a HistoryElement object. These objects can be copied and assigned to each other, but user programs cannot otherwise construct HistoryElement objects.

The class HistoryElement has but two members:

const_iterator and const_reverse_iterator

The iterators returned by members of the class ReadLineHistory are input iterators, pointing to HistoryElement objects. As they are input iterators modification of the history elements to which they refer is not allowed.

The class const_iterator allows iterations from the first to the last history element, the class const_reverse_iterator allows iterations from the last back to the first history element.

The iterators can be incremented, compared for (in)equality and offer operator* and operator-> members, offering access to, respectively, HistoryElement objects and their addresses.

CONSTRUCTORS

As the class ReadLineHistory is a singleton class, there are no publicly available constructors, nor are assignment operators available.

STATIC MEMBER FUNCTIONS

MEMBER FUNCTIONS

OVERLOADED OPERATORS

EXAMPLE

#include <iostream>
#include <algorithm>
#include <fstream>

#include <bobcat/datetime>
#include <bobcat/readlinestream>

//#include <bobcat/readlinehistory>
#include "../readlinehistory"

using namespace std;
using namespace FBB;

void showHis(ReadLineHistory::HistoryElement const &element)
{
    cout << element.timestamp() << " " << element.line() << '\n';
}

string timestamp()
{
    return DateTime().rfc2822();
};

int main(int argc, char **argv)
{
    ReadLineStream in("? ", ReadLineBuf::EXPAND_HISTORY);
    in.useTimestamps(&timestamp);

    cout << "Enter some lines, end the input using ctrl-d\n";
    string line;
    while (getline(in, line))
        ;
                                            // argument means: write/read
                                            // history timestamps
    ReadLineHistory &history = ReadLineHistory::instance(argc > 1);

    cout << "All lines, from the first to the last:\n";
    for_each(history.begin(), history.end(), showHis);

    cout << "\n"
            "Again: all lines, from the first to the last:\n";
    for_each(history.begin(), history.end(), showHis);

    cout << "\n"
            "All lines, from the last to the first:\n";
    for_each(history.rbegin(), history.rend(), showHis);

    cout << "\n"
            "History out and in:\n"
            "\n";

    ofstream hisout("history.out");

    hisout << history;

    hisout.close();

    ifstream hisin("history.out");
    hisin >> history;

    cout << "All lines, from the first to the last:\n";
    for_each(history.begin(), history.end(), showHis);

    cout << "\n"
            "All lines, from the last to the first:\n";
    for_each(history.rbegin(), history.rend(), showHis);

}










FILES

bobcat/readlinehistory - defines the class interface

SEE ALSO

bobcat(7), readline(3), readlinebuf(3), readlinestream(3)

BUGS

None Reported.

BOBCAT PROJECT FILES

BOBCAT

Bobcat is an acronym of `Brokken's Own Base Classes And Templates'.

COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

AUTHOR

Frank B. Brokken (f.b.brokken@rug.nl).