All pastes #107303 Raw Edit

Mine

public cpp v1 · immutable
#107303 ·published 2006-07-30 23:30 UTC
rendered paste body
#include "Screen.h"#include "stdio.h"void response_function(Screen *sptr) {    sptr->add(dtioRows(), 0, dtioColumns(), "A response has been sent", false);}int main(void) {    dtioStart();    Screen screen;    Field field(0, 10, 30, "Blah blah crap", false);    Label label(1, 10, 30, "Blah blah crap");    screen.add(field);    screen.add(label);    screen.display();    Response response(0, 0, "YyNnMm", '', response_function);    screen.add(response);    (screen.getField(2))->edit(&screen);    fprintf(stderr, "response.data():%c\n", ((screen.getField(2))->data()));    dtioGetchar();    dtioStop();    return 0;}Field::Field(int r, int c, int l, const char *s, bool edit) {    row = 0;    col = 0;    len = 0;    c_pos = 0;    d = NULL;    init(r, c, l, s, edit);}/* copy constructor */Field::Field(const Field &right):row(0), col(0), len(0), c_pos(-1), d(NULL) {    *this = right;}/* default constructor -- set member data to safe values */Field::Field():row(0), col(0), len(0), c_pos(-1), d(NULL) {    setData("", len);}/* garbage collection */Field::~Field() {    if (d) {        free(d);    }}/* assignment operator * returns a reference to the left operand */const Field& Field::operator=(const Field &right) {    /* if the operands are different instances of Field */    if (this != &right) {        /* initialize the left operand to be the same as the right */        init(right.getRow(), right.getCol(), right.getLen(),             (char*)right.data(), right.editable());    }       return *this;}/* is-equal-to operator, necessary for the use of deques * a Field is considered equal to another field if: * the starting coordinates are the same, the length is the same, * the data is the same, and the editability is the same * returns 1 for true and 0 for false */bool Field::operator==(const Field &right) {    bool ret = false;    if ((*this).row == right.row && (*this).col == right.col       && (*this).len == right.len && (*this).is_editable == right.is_editable       && (strcmp((*this).d, right.d) == 0)) {        ret = true;    }    return ret;}bool Field::operator!=(const Field &right) {    bool ret = false;    if ((*this).row != right.row || (*this).col != right.col       || (*this).len != right.len || (*this).is_editable != right.is_editable       || (strcmp((*this).d, right.d) != 0)) {        ret = true;    }    return ret;}/* initialize this field */void Field::init(int r, int c, int width, const char *s, bool edit) {    setRow(r);    setCol(c);    setLen(width);    /* if a value is specified for s, set the data to it */    if (s) {        setData(s, len);    }    /* otherwise, set it to a safe value */    else {        setData("\0", len);    }    is_editable = edit;}/* display this field */void Field::display() {    dtioDisplay(d, row, col, len);}/* displays the field and, if possible, puts it into edit mode * returns the key used to exit edit mode if editable * returns 0 if not editable */int Field::edit(Screen *sptr) {    int ret = 0;    /* is the field editable? */    if (is_editable) {        /* set c_pos to 0 if it is the first time being edited */        if (c_pos == -1) {            setcPos(0);        }        /* if the requested cursor position is longer than the string         * put c_pos at the end of the string         */        else if (c_pos > len) {            c_pos = len;        }        /* edit the string */        ret = dtioEdit(d, row, col, len, &c_pos);    }    else {        display();    }    return ret;}/* returns a pointer to a new copy of this field */Field* Field::clone()const {    return new(std::nothrow) Field(row, col, len, d, is_editable);}/* START OF RESPONSE CLASS */Response::Response(int r, int c, const char *s, int ch, void (*fptr)(Screen*)) {    valid_chars = NULL;    fptr = NULL;    init(r, c, s, ch, fptr);}Response::~Response() {    if (valid_chars) {        delete [] valid_chars;    }}void Response::init(int r, int c, const char *s, int ch,                    void (*fptr)(Screen *)) {    setRow(r);    setCol(c);    setLen(1);    setData(ch);    valid_chars = new char[strlen(s)];    strcpy(valid_chars, s);    response_f = fptr;    is_editable = true;}int Response::edit(Screen *sptr) {    display();    dtioCursor(row, col);    int new_d = dtioGetchar();    for (int i = 0; i < strlen(valid_chars); i++) {        if (new_d = valid_chars[i]) {            d = new_d;            break;        }    }    if ((d != new_d) && response_f) {        (*response_f)(sptr);    }}void Response::display() {    dtioCursor(row, col);    dtioPutchar(d);}Response* Response::clone()const {    return new(std::nothrow) Response(row, col, valid_chars, d, response_f);}Response::init (this=0x8061830, r=0, c=0, s=0x8061820 "YyNnMm", ch=111, fptr=0)    at Screen.cpp:491491         valid_chars = new char[strlen(s)];(gdb) 492         strcpy(valid_chars, s);(gdb) 493         response_f = fptr;(gdb) 494         is_editable = true;(gdb) 495     }(gdb) Response (this=0x8061830, r=0, c=0, s=0x8061820 "YyNnMm", ch=111, fptr=0)    at Screen.cpp:477477     }(gdb) Response::clone (this=0xbff2941c) at Screen.cpp:520520     }(gdb) std::deque<Field, std::allocator<Field> >::push_back (this=0xbff293f0,     __x=@0x8061830) at stl_deque.h:10411041            if (this->_M_impl._M_finish._M_cur(gdb) 1044                this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);(gdb) __gnu_cxx::__mt_alloc_base<Field>::construct (this=0xbff293f0, __p=0x80614f0,     __val=@0x8061830) at mt_allocator.h:611611           { ::new(__p) _Tp(__val); }(gdb) operator new (__p=0x80614f0) at new:9494      inline void* operator new(std::size_t, void* __p) throw() { return __p; }(gdb) 0x0804b2ba in __gnu_cxx::__mt_alloc_base<Field>::construct (this=0xbff293f0,     __p=0x80614f0, __val=@0x8061830) at mt_allocator.h:611611           { ::new(__p) _Tp(__val); }(gdb) Field (this=0x80614f0, right=@0x8061830) at Screen.cpp:3434      Field::Field(const Field &right):row(0), col(0), len(0), c_pos(-1), d(NULL) {(gdb) 35          *this = right;