// input0.cpp // from class Tuesday 15/2/2000 // very basic input system defined as a C++ object #include #include #include #include // This is creating "input" as a new data type. From now on, I'll // be allowed to create new variables by saing e.g. "input x;", // just like saying "int x;" creates a new integer variable. // // This class declaration must come first, and include a description // of all the items that belong in an "input" object. I must define // all the functions later. class input { public: // the next four things are accessible to any program // or function that creates an "input" variable. I must // make sure that they provide all the necessary operations // without allowing the user to mess anything up. input(void); // the function with the same name as the class (input) is // the "constructor". It is automatically called every time // an input object is created. We use it to ensure that // variables and things are initialised correctly. int ok(void); char readchar(void); char *readline(char *s, int max); // our users can only create "input" variables, then use // the "ok", "readch", and "readline" functions that belong // to those variables. Anything that is not in the public // section is inaccessible unless those functions give // access somehow. protected: // These variables and function(s) are critical to the // operation of an input object, but we don't want the user // fooling around with them. If the user took something out // of the buffer, or added something back in, without updating // num_taken_out properly, everything would go wrong. // "protected" allows us complete control over access. char buffer[512]; int num_in_buff, num_taken_out, at_end_of_file, allok; void refill_buffer(void); }; // remember to put a semblycolon after the closing cunnybracket of the class def. // A function that belongs to an object and has the same name as that object // always becomes a "constructor" for that object. It is always called when // an object is created, so you can use it to initialise any important variables. input::input(void) // the constructor { num_in_buff=0; num_taken_out=0; at_end_of_file=0; allok=1; } int input::ok(void) { return (allok); } // readchar (and all of its little friends) are members of // the input class (because they were declared inside the // definition of the class). This means that it has full // unrestricted access to all parts public or protected. char input::readchar(void) { if (num_taken_out>=num_in_buff) refill_buffer(); if (!allok) return '\0'; char answer=buffer[num_taken_out]; num_taken_out+=1; return answer; } // we didn't get around to adding readline on Tuesday, but there // is nothing interesting about it. It reads characters until it // reaches the end of a line, putting them into a string. When it // finds the '\n' at the end of the line, it throws it out, replacing // it with the '\0' that marks the end of the string. The user is // required to pass in the length of the string too, so there is // no risk of overflowing it. // // When readline wnats to read a character (which it does a lot), // it uses the already defined readchar. Readchar already knows how // to deal with the buffer being empty and anything else that might // wrong, so we never have to think about such things again. char *input::readline(char *s, int max) { int i; for (i=0; i