animals-201207131226/0000755000175000017500000000000012000012270012014 5ustar phiphianimals-201207131226/Animal.h0000644000175000017500000000562311777671070013431 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #ifndef Animal_h #define Animal_h #include "Record.h" #include #include "str-conversions.h" class Animal : public Record { private: std::string *animal; void Default(void) { animal = new std::string; } void Set(const char *anAnimal) { animal = new std::string(anAnimal); } void Copy(const Animal &orig) { if(orig.animal) { animal = new std::string(*orig.animal); } else { animal = 0; } } protected: virtual void Destroy(void) { if(animal) delete animal; animal = 0; } virtual void SetNull(void) { animal = new std::string; } virtual void SetFromDataString(const char *theDataString) { const char *data = theDataString; if(data[0] == 'a') { std::istringstream it(data + 2); std::ostringstream theAnimal; // grab the animal text char nextCh = it.get(); while(nextCh != '|') { theAnimal << nextCh; nextCh = it.get(); } animal = new std::string(MakeCStringCopy(theAnimal)); } else SetNull(); } public: Animal() { Default(); } Animal(const char *anAnimal) { Set(anAnimal); } Animal(std::string &anAnimal) { Set(anAnimal.c_str()); } Animal(const Animal &orig) { Copy(orig); } Animal &operator=(const Animal &orig) { Destroy(); Copy(orig); return *this; } virtual ~Animal() { Destroy(); } Animal(Db &handle, const char *animalKey) { Default(); Read(handle, animalKey); } Animal(Db &handle, const std::string &animalKey) { Default(); Read(handle, animalKey); } virtual const char *FormNewDataCString() const { std::string temp = "a|"; if(animal) temp += (*animal); temp += "|"; return MakeCStringCopy(temp); } virtual void Print(std::ostream &out) const { out << (animal ? (*animal) : std::string()); } }; #endif animals-201207131226/COPYING0000644000175000017500000004310311777667347013121 0ustar phiphi GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. animals-201207131226/ChangeLog0000644000175000017500000000070312000012201013560 0ustar phiphiv. 201207131226 - Fri Jul 13 2012 14:26 CEST * Changed ChangeLog format to RoarAudio Changelog Format. * Allow answering "sure" to "Want to play again?". * Updated legal infrastructure. * Updated manpage. * Updated Makefile. v. 201007161925 - Fri Jul 16 2010 21:24 CEST * New maintainer. * Patches are merged by upstream. * Some usability fixes. * Added support for multible databases. NOTE: for older changelog entrys see debian/changelog animals-201207131226/Makefile0000644000175000017500000000463212000011617013465 0ustar phiphi#this is the -*- makefile -*- for animals. # Copyright (c) 1997 by Jim Lynch. # Copyright (c) 2010-2012 by Philipp Schafft. # This software comes with NO WARRANTY WHATSOEVER. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 dated June, 1991, or, at your # option, any LATER version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this software; see the file COPYING. If not, write to # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. # if you want the db in your home dir (maybe for testing) #CPPFLAGS = -O2 -g -DDbName=\"$(HOME)/animals.db\" # this one is pre-FHS, so uses FSSTND #CPPFLAGS = -O2 -g -DDbName=\"/var/lib/games/animals/animals.db\" # use this one for FHS-based debian package CPPFLAGS += -O2 -g -DDbName=\"/var/games/animals/animals.db\" # Setting some common linker flags LDFLAGS += -O2 -g -Wall -Wextra # Edited for Debian GNU/Linux. DESTDIR = CXX=g++ animals: main.o db4++-stuff.o str-conversions.o util.o $(CXX) $(LDFLAGS) -o animals main.o db4++-stuff.o str-conversions.o util.o -ldb_cxx # various tests are conducted in test.cc test: db4++-stuff.o test.o str-conversions.o util.o $(CXX) $(LDFLAGS) -o test test.o db4++-stuff.o str-conversions.o -ldb_cxx # tests of the string conversion funcs are conducted in test-str-cvt.cc test-str-cvt: test-str-cvt.o str-conversions.o $(CXX) $(LDFLAGS) -o test-str-cvt test-str-cvt.o str-conversions.o test-str-cvt.o: test-str-cvt.cc str-conversions.h str-conversions.o: str-conversions.cc str-conversions.h main.o: main.cc Record.h Question.h Animal.h db++-stuff.h str-conversions.h db4++-stuff.o: db4++-stuff.cc db++-stuff.h util.o: util.cc util.h install-prep-dirs: mkdir -p $(DESTDIR)/usr/games mkdir -p $(DESTDIR)/var/games/animals mkdir -p $(DESTDIR)/usr/share/man/man6 install: animals install-prep-dirs cp animals $(DESTDIR)/usr/games/animals cp animals.6 $(DESTDIR)/usr/share/man/man6 all: animals clean: rm -f animals test test-str-cvt *.o *~ core new: clean all animals-201207131226/Question.h0000644000175000017500000001026211777671070014032 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #ifndef Question_h #define Question_h #include "Record.h" #include "str-conversions.h" #include class Question : public Record { private: std::string *question; std::string *yesKey; std::string *noKey; void Set ( const char *aQuestion, const char *aYesKey, const char *aNoKey ) { question = new std::string(aQuestion); yesKey = new std::string(aYesKey); noKey = new std::string(aNoKey); } void Copy(const Question &orig) { question = yesKey = noKey = 0; if(orig.question) question = new std::string(*orig.question); if(orig.yesKey) yesKey = new std::string(*orig.yesKey); if(orig.noKey) noKey = new std::string(*orig.noKey); } void Default(void) { question = new std::string; yesKey = new std::string; noKey = new std::string; } protected: virtual void Destroy(void) { if(question) delete question; if(yesKey) delete yesKey; if(noKey) delete noKey; question = yesKey = noKey = 0; } // Warning: You must call Destroy() before calling // any method starting with "Set" in this class, // -unless- you are initializing the object. virtual void SetNull(void) { question = new std::string; noKey = new std::string; yesKey = new std::string; } virtual void SetFromDataString(const char *theDataString) { const char *data = theDataString; if(data[0] == 'q') { std::istringstream it(theDataString + 2); std::ostringstream theQuestion; std::ostringstream theYesKey; std::ostringstream theNoKey; // grab the question text char nextCh = it.get(); while(nextCh != '|') { theQuestion << nextCh; nextCh = it.get(); } question = new std::string(MakeCStringCopy(theQuestion)); // grab the yesKey text nextCh = it.get(); while(nextCh != '|') { theYesKey << nextCh; nextCh = it.get(); } yesKey = new std::string(MakeCStringCopy(theYesKey)); // grab the noKey text nextCh = it.get(); while(nextCh != '|') { theNoKey << nextCh; nextCh = it.get(); } noKey = new std::string(MakeCStringCopy(theNoKey)); } else { SetNull(); } } public: Question() { Default(); } Question(const Question &orig) { Copy(orig); } Question ( const char *aQuestion, const char *aYesKey, const char *aNoKey ) { Set(aQuestion, aYesKey, aNoKey); } virtual ~Question() { Destroy(); } Question &operator=(const Question &orig) { Destroy(); Copy(orig); return *this; } Question(Db &handle, const std::string &theKey) { Default(); Read(handle, theKey); } std::string GetYesKey(void) { return *yesKey; } std::string GetNoKey(void) { return *noKey; } virtual const char *FormNewDataCString() const { std::string temp = "q|"; if(question) temp += (*question); temp += "|"; if(yesKey) temp += (*yesKey); temp += "|"; if(noKey) temp += (*noKey); temp += "|"; return MakeCStringCopy(temp); } virtual void Print(std::ostream &out) const { out << (question ? (*question) : std::string()); } }; #endif animals-201207131226/Record.h0000644000175000017500000000520111777671070013436 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #ifndef Record_h #define Record_h #include "db4++-stuff.h" #include "str-conversions.h" // #include #include #include #include class Record { protected: virtual void Destroy(void) { } virtual void SetNull(void) { } virtual void SetFromDataString(const char *theDataString) { } public: Record() { } virtual ~Record() { } virtual const char *FormNewDataCString() const { } virtual void Read(Db &handle, const std::string &theKey) { std::string theValue; Destroy(); if(::Read(handle, theKey, theValue)) { SetFromDataString(theValue.c_str()); } else { SetNull(); } // const char *key = theKey.c_str(); // // Read(handle, key); } virtual void Read(Db &handle, const char *theKey) { std::string key(theKey); Read(handle, key); // Dbt key((void*) theKey, strlen(theKey) + 1); // Dbt content; // char * p; // // handle.get(NULL, &key, &content, 0); // p = (char *) content.get_data(); // // Destroy(); // // if(p) // { // char *tmp = MakeCStringCopy(p); // // SetFromDataString(tmp); // } // else // SetNull(); } virtual void Write(Db &handle, const char *theKey) const { std::string p(FormNewDataCString()); std::string key(theKey); int wrOK = ::Write(handle, key, p); } virtual void Print(std::ostream &out) const { const char *p = FormNewDataCString(); out << p; delete[] p; } }; inline std::ostream &operator<<(std::ostream &out, const Record &it) { it.Print(out); return out; } #endif animals-201207131226/animals.60000644000175000017500000000243512000007637013546 0ustar phiphi.TH ANIMALS 6 "July 2012" .\" NAME should be all caps, SECTION should be 1-8, maybe w/ subsection .\" other parms are allowed: see man(7), man(1) .SH NAME animals \- traditional AI game that learns about animals from the player .SH SYNOPSIS .B animals [DatabaseFile [TypeString [PluralTypeString]]] or .B animals {\-h|\-\-help} .SH "DESCRIPTION" This manual page documents briefly the .BR animals game. .B animals is a text-based animal guessing game. .SH RULES You think of an animal, and hit enter when you have one. Then the game asks you yes-or-no questions that successively narrows down your animal. Eventually, the game will make a guess, and ask you if it was right. If it was wrong, you win! You then teach it about your animal. .SH FILES /var/games/animals/animals.db .SH AUTHOR This manual page was originally written by Jim Lynch (for the Debian GNU/Linux system). The current Author is Philipp "ph3-der-loewe" Schafft . .SH "HISTORY" animals was initially released on Fri, 4th of Jun 1999. On Fri, 16th of Jul 2010 the first non Debian native version was released. This was part of the takeover by Philipp Schafft (see AUTHORS) of the Debian as it was orphaned. On Wed, 27th of Jun 2012 he abdicated Debian maintainership but is still upstream author. animals-201207131226/db++-stuff.h0000644000175000017500000000251311777671070014063 0ustar phiphi// Copyright (c) 1997,2003 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include #include #include extern long lastKeyAssigned; int Write(Db &theFile, const std::string &theKey, const std::string &theValue); int Read(Db &theFile, const std::string &theKey, std::string &theValue); Db *OpenFile(const char * dbfile); void CloseFile(Db &handle); void SetLast(Db &handle, const std::string &newKey); std::string GetLast(Db &handle); int IsAnimal(Db &handle, const std::string &theKey); animals-201207131226/db2++-stuff.cc0000644000175000017500000001721011777671070014303 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include "db++-stuff.h" #include "str-conversions.h" #include "main.h" #include #include #include #include #include #include #include #include long lastKeyAssigned; int Write(Db &theFile, const std::string &theKey, const std::string &theValue) { int putBad; Dbt key; Dbt value; key.set_data((void*) theKey.c_str()); key.set_size(strlen(theKey.c_str()) + 1); value.set_data((void*) theValue.c_str()); value.set_size(strlen(theValue.c_str()) + 1); try { putBad = theFile.put(NULL, &key, &value, 0); } catch(DbException &e) { std::cerr << "db2++ Write(Db filehandle, "; std::cerr << '"' << theKey << '"'; std::cerr << ", "; std::cerr << '"' << theValue << '"'; std::cerr << "): " << e.what() << std::endl; putBad = 1; // or throw game exception here? } return ! putBad; } int Read(Db &theFile, const std::string &theKey, std::string &theValue) { int getBad; Dbt key; Dbt value; key.set_data((void*) theKey.c_str()); key.set_size(strlen(theKey.c_str()) + 1); try { getBad = theFile.get(NULL, &key, &value, 0); } catch(DbException &e) { std::cerr << "db2++ Read(Db filehandle, "; std::cerr << '"' << theKey << '"'; std::cerr << ", "; std::cerr << "(some value placeholder)"; std::cerr << "): " << e.what() << std::endl; getBad = 1; // or throw game exception here? } if(value.get_data()) { theValue = (char*) value.get_data(); } else { theValue = ""; } return ! getBad; } Db *OpenFile(const char * dbfile) { Db *theFile; struct stat dummy; if(stat(dbfile, &dummy) == -1) // stat failed? { if(errno == ENOENT) // file not found? { int createBad; // create the file here, fully; ready for animals. mode_t saveMode = umask(0); try { createBad = Db::open ( dbfile, NULL, DB_BTREE /* use B+Tree structure */, DB_CREATE, 0660, NULL, NULL, &theFile ); } catch(DbException &e) { cerr << "db2++ Open(): while trying to create"; cerr << ": " << e.what() << endl; } umask(saveMode); if(createBad) // can't create? { // errno may be set now. // Deal with it... std::cout << progName << ": OpenFile: file " << dbfile << ": errno=" << errno << std::endl; exit(-1); } else { // new gdbm file. Add initial infrastructure. int putBad; putBad = ! Write(*theFile, "last", "0"); //Dbt key; //Dbt lastAddedKey; // //key.set_data((void*) "last"); //key.set_size(5); // //lastAddedKey.set_data((void*) "0"); //lastAddedKey.set_size(2); // //try //{ // putBad = theFile->put(NULL, &key, &lastAddedKey, 0); //} //catch(DbException &e) //{ // cerr << e.what() << endl; // putBad = 1; // or throw game exception here? //} } } // endif(errno == ENOENT) else { } } else { int chmod_res, openBad; try { openBad = Db::open ( dbfile, DB_BTREE /* use B+Tree structure */, 0, 0660, NULL, NULL, &theFile ); } catch(DbException &e) { std::cerr << e.what() << std::endl; openBad = 1; // or throw game exception here? } if(openBad) // can't open existing db file? { // either or both of gdbm_errno and errno may be set now. // Deal with it... std::cout << progName << ": OpenFile: file " << dbfile; std::cout << ": errno=" << errno << std::endl; exit(-1); } else { // file is open for update; get "last" into lastKeyAssigned std::string lastString; int getBad; getBad = ! Read(*theFile, "last", lastString); //Dbt key((void*) "last", 5); //Dbt value; // //try // { // getBad = theFile->get(NULL, &key, &value, 0); // } //catch(DbException &e) // { // cerr << e.what() << endl; // getBad = 1; // or throw a game exception? // } // if(lastString == "") // should not be null; // if it is, "last" is missing { std::cout << "'last' key value missing: database seems corrupt"; std::cout << std::endl; theFile->close(0); exit(-1); } std::istringstream *theLastStream = new std::istringstream(lastString.c_str()); (*theLastStream) >> lastKeyAssigned; delete theLastStream; } } return theFile; } void CloseFile(Db &handle) { try { handle.close(0); } catch(DbException &e) { std::cerr << e.what() << std::endl; // throw game exception here? } } void SetLast(Db &handle, const std::string &newKey) { int wrOK = Write(handle, "last", newKey); // Dbt key((void*) "last", 5); // char *p; // // p = MakeCStringCopy(newKey); // // Dbt content((void*) p, strlen(p) + 1); // // try // { // handle.del(NULL, &key, 0); // handle.put(NULL, &key, &content, 0); // } // catch(DbException &e) // { // cerr << e.what() << endl; // // maybe propigate the exception to a game exception // } // // delete[] p; } std::string GetLast(Db &handle) { std::string result; int rdOK = Read(handle, "last", result); // Dbt content; // Dbt key((void*) "last", 5); // int getOK = 1; // // try // { // handle.get(NULL, &key, &content, 0); // } // catch(DbException &e) // { // cerr << e.what() << endl; // getOK = 0; // } // // if(getOK) // { // if(content.get_data()) // { // result = (char*) content.get_data(); // } // } // else // { // // throw game exception? // } return result; } int IsAnimal(Db &handle, const std::string &theKey) { int result = 0; std::string theValue; int getOK = Read(handle, theKey, theValue); char firstChar = theValue[0]; result = (firstChar == 'a'); // char *p = MakeCStringCopy(theKey); // Dbt key((void*) p, strlen(p) + 1); // Dbt content; // // try // { // handle.get(NULL, &key, &content, 0); // } // catch(DbException &e) // { // cerr << e.what() << endl; // getOK = 0; // } // // if(p) // delete[] p; // // if(getOK) // { // p = (char *) content.get_data(); // // if(p) // if(*p == 'a') // result = 1; // } // else // { // // perhaps throw an exception here relevent to the game... // } return result; } animals-201207131226/db2++-stuff.h0000644000175000017500000000244711777671070014153 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include #include #include extern long lastKeyAssigned; int Write(Db &theFile, const string &theKey, const string &theValue); int Read(Db &theFile, const string &theKey, string &theValue); Db *OpenFile(const char * dbfile); void CloseFile(Db &handle); void SetLast(Db &handle, const string &newKey); string GetLast(Db &handle); int IsAnimal(Db &handle, const string &theKey); animals-201207131226/db4++-stuff.cc0000644000175000017500000002076511777671070014316 0ustar phiphi// Copyright (c) 1997,2002 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include "db4++-stuff.h" #include "main.h" #include #include #include #include #include #include #include #include #include #include #include "str-conversions.h" long lastKeyAssigned; int Write(Db &theFile, const std::string &theKey, const std::string &theValue) { int putBad; Dbt key; Dbt value; key.set_data((void*) theKey.c_str()); key.set_size(strlen(theKey.c_str()) + 1); value.set_data((void*) theValue.c_str()); value.set_size(strlen(theValue.c_str()) + 1); try { putBad = theFile.put(NULL, &key, &value, 0); } catch(DbException &e) { std::cerr << "db2++ Write(Db filehandle, "; std::cerr << '"' << theKey << '"'; std::cerr << ", "; std::cerr << '"' << theValue << '"'; std::cerr << "): " << e.what() << std::endl; putBad = 1; // or throw game exception here? } return ! putBad; } int Read(Db &theFile, const std::string &theKey, std::string &theValue) { int getBad; Dbt key; Dbt value; key.set_data((void*) theKey.c_str()); key.set_size(strlen(theKey.c_str()) + 1); try { getBad = theFile.get(NULL, &key, &value, 0); } catch(DbException &e) { std::cerr << "db3++ Read(Db filehandle, "; std::cerr << '"' << theKey << '"'; std::cerr << ", "; std::cerr << "(some value placeholder)"; std::cerr << "): " << e.what() << std::endl; getBad = 1; // or throw game exception here? } if(value.get_data()) { theValue = (char*) value.get_data(); } else { theValue = ""; } return ! getBad; } Db *OpenFile(const char * dbfile) { Db *theFile; struct stat dummy; if(stat(dbfile, &dummy) == -1) // stat failed? { if(errno == ENOENT) // file not found? { int createBad; // create the file here, fully; ready for animals. mode_t saveMode = umask(0); try { theFile = new Db(NULL, 0); createBad = theFile->open ( NULL, /* transaction object ptr (unused) */ dbfile, NULL, /* name of database within file (unused) */ DB_BTREE /* use B+Tree structure */, DB_CREATE, 0660 ); } catch(DbException &e) { std::cerr << "db2++ Open(): while trying to create"; std::cerr << ": " << e.what() << std::endl; CloseFile(*theFile); return NULL; } umask(saveMode); if(createBad) // can't create? { // errno may be set now. // Deal with it... std::cout << progName << ": OpenFile: file " << dbfile << ": errno=" << errno << std::endl; exit(-1); } else { // new gdbm file. Add initial infrastructure. int putBad; putBad = ! Write(*theFile, "last", "0"); //Dbt key; //Dbt lastAddedKey; // //key.set_data((void*) "last"); //key.set_size(5); // //lastAddedKey.set_data((void*) "0"); //lastAddedKey.set_size(2); // //try //{ // putBad = theFile->put(NULL, &key, &lastAddedKey, 0); //} //catch(DbException &e) //{ // cerr << e.what() << endl; // putBad = 1; // or throw game exception here? //} } } // endif(errno == ENOENT) else { } } else { int chmod_res, openBad; try { theFile = new Db(NULL, 0); openBad = theFile->open ( NULL, /* transaction obj ptr (unused) */ dbfile, NULL, /* name of database within file (unused) */ DB_BTREE /* use B+Tree structure */, 0, 0660 ); } catch(DbException &e) { std::cerr << e.what() << std::endl; openBad = 1; // or throw game exception here? } if(openBad) // can't open existing db file? { // either or both of gdbm_errno and errno may be set now. // Deal with it... std::cout << progName << ": OpenFile: file " << dbfile; std::cout << ": errno=" << errno << std::endl; exit(-1); } else { // file is open for update; get "last" into lastKeyAssigned std::string lastString; int getBad; getBad = ! Read(*theFile, "last", lastString); //Dbt key((void*) "last", 5); //Dbt value; // //try // { // getBad = theFile->get(NULL, &key, &value, 0); // } //catch(DbException &e) // { // cerr << e.what() << endl; // getBad = 1; // or throw a game exception? // } // if(lastString == "") // should not be null; // if it is, "last" is missing { std::cout << "'last' key value missing: database seems corrupt"; std::cout << std::endl; theFile->close(0); exit(-1); } std::istringstream *theLastStream = new std::istringstream(lastString.c_str()); (*theLastStream) >> lastKeyAssigned; delete theLastStream; } } return theFile; } void CloseFile(Db &handle) { try { handle.close(0); } catch(DbException &e) { std::cerr << e.what() << std::endl; // throw game exception here? } } void SetLast(Db &handle, const std::string &newKey) { int wrOK = Write(handle, "last", newKey); // Dbt key((void*) "last", 5); // char *p; // // p = MakeCStringCopy(newKey); // // Dbt content((void*) p, strlen(p) + 1); // // try // { // handle.del(NULL, &key, 0); // handle.put(NULL, &key, &content, 0); // } // catch(DbException &e) // { // std::cerr << e.what() << endl; // // maybe propigate the exception to a game exception // } // // delete[] p; } std::string GetLast(Db &handle) { std::string result; int rdOK = Read(handle, "last", result); // Dbt content; // Dbt key((void*) "last", 5); // int getOK = 1; // // try // { // handle.get(NULL, &key, &content, 0); // } // catch(DbException &e) // { // cerr << e.what() << endl; // getOK = 0; // } // // if(getOK) // { // if(content.get_data()) // { // result = (char*) content.get_data(); // } // } // else // { // // throw game exception? // } return result; } void SetDbType(Db &handle, const std::string &newType, bool plural) { int wrOK = Write(handle, plural ? "dbtype_plural" : "dbtype_singular", newType); } std::string GetDbType(Db &handle, bool plural) { std::string result; int rdOK = Read(handle, plural ? "dbtype_plural" : "dbtype_singular", result); // Test if we got something or use defaults: if ( !rdOK || result.length() == 0 ) { if ( plural ) { result = GetDbType(handle, false); result += "s"; // add plural 's' } else { return "animal"; } } return result; } int IsAnimal(Db &handle, const std::string &theKey) { int result = 0; std::string theValue; int getOK = Read(handle, theKey, theValue); char firstChar = theValue[0]; result = (firstChar == 'a'); // char *p = MakeCStringCopy(theKey); // Dbt key((void*) p, strlen(p) + 1); // Dbt content; // // try // { // handle.get(NULL, &key, &content, 0); // } // catch(DbException &e) // { // cerr << e.what() << endl; // getOK = 0; // } // // if(p) // delete[] p; // // if(getOK) // { // p = (char *) content.get_data(); // // if(p) // if(*p == 'a') // result = 1; // } // else // { // // perhaps throw an exception here relevent to the game... // } return result; } animals-201207131226/db4++-stuff.h0000644000175000017500000000270011777671070014145 0ustar phiphi// Copyright (c) 1997,2002 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include #include #include extern long lastKeyAssigned; int Write(Db &theFile, const std::string &theKey, const std::string &theValue); int Read(Db &theFile, const std::string &theKey, std::string &theValue); Db *OpenFile(const char * dbfile); void CloseFile(Db &handle); void SetLast(Db &handle, const std::string &newKey); std::string GetLast(Db &handle); void SetDbType(Db &handle, const std::string &newType, bool plural); std::string GetDbType(Db &handle, bool plural); int IsAnimal(Db &handle, const std::string &theKey); animals-201207131226/db4.0++-stuff.cc0000644000175000017500000001740611777671070014452 0ustar phiphi// Copyright (c) 1997,2002 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include "db4++-stuff.h" #include "main.h" #include #include #include #include #include #include #include #include #include "str-conversions.h" long lastKeyAssigned; int Write(Db &theFile, const std::string &theKey, const std::string &theValue) { int putBad; Dbt key; Dbt value; key.set_data((void*) theKey.c_str()); key.set_size(strlen(theKey.c_str()) + 1); value.set_data((void*) theValue.c_str()); value.set_size(strlen(theValue.c_str()) + 1); try { putBad = theFile.put(NULL, &key, &value, 0); } catch(DbException &e) { std::cerr << "db2++ Write(Db filehandle, "; std::cerr << '"' << theKey << '"'; std::cerr << ", "; std::cerr << '"' << theValue << '"'; std::cerr << "): " << e.what() << std::endl; putBad = 1; // or throw game exception here? } return ! putBad; } int Read(Db &theFile, const std::string &theKey, std::string &theValue) { int getBad; Dbt key; Dbt value; key.set_data((void*) theKey.c_str()); key.set_size(strlen(theKey.c_str()) + 1); try { getBad = theFile.get(NULL, &key, &value, 0); } catch(DbException &e) { std::cerr << "db3++ Read(Db filehandle, "; std::cerr << '"' << theKey << '"'; std::cerr << ", "; std::cerr << "(some value placeholder)"; std::cerr << "): " << e.what() << std::endl; getBad = 1; // or throw game exception here? } if(value.get_data()) { theValue = (char*) value.get_data(); } else { theValue = ""; } return ! getBad; } Db *OpenFile(const char * dbfile) { Db *theFile; struct stat dummy; if(stat(dbfile, &dummy) == -1) // stat failed? { if(errno == ENOENT) // file not found? { int createBad; // create the file here, fully; ready for animals. mode_t saveMode = umask(0); try { theFile = new Db(NULL, 0); createBad = theFile->open ( dbfile, NULL, /* name of database within file (unused) */ DB_BTREE /* use B+Tree structure */, DB_CREATE, 0660 ); } catch(DbException &e) { std::cerr << "db2++ Open(): while trying to create"; std::cerr << ": " << e.what() << std::endl; } umask(saveMode); if(createBad) // can't create? { // errno may be set now. // Deal with it... std::cout << progName << ": OpenFile: file " << dbfile << ": errno=" << errno << std::endl; exit(-1); } else { // new gdbm file. Add initial infrastructure. int putBad; putBad = ! Write(*theFile, "last", "0"); //Dbt key; //Dbt lastAddedKey; // //key.set_data((void*) "last"); //key.set_size(5); // //lastAddedKey.set_data((void*) "0"); //lastAddedKey.set_size(2); // //try //{ // putBad = theFile->put(NULL, &key, &lastAddedKey, 0); //} //catch(DbException &e) //{ // cerr << e.what() << endl; // putBad = 1; // or throw game exception here? //} } } // endif(errno == ENOENT) else { } } else { int chmod_res, openBad; try { theFile = new Db(NULL, 0); openBad = theFile->open ( dbfile, NULL, /* name of database within file (unused) */ DB_BTREE /* use B+Tree structure */, 0, 0660 ); } catch(DbException &e) { std::cerr << e.what() << std::endl; openBad = 1; // or throw game exception here? } if(openBad) // can't open existing db file? { // either or both of gdbm_errno and errno may be set now. // Deal with it... std::cout << progName << ": OpenFile: file " << dbfile; std::cout << ": errno=" << errno << std::endl; exit(-1); } else { // file is open for update; get "last" into lastKeyAssigned std::string lastString; int getBad; getBad = ! Read(*theFile, "last", lastString); //Dbt key((void*) "last", 5); //Dbt value; // //try // { // getBad = theFile->get(NULL, &key, &value, 0); // } //catch(DbException &e) // { // cerr << e.what() << endl; // getBad = 1; // or throw a game exception? // } // if(lastString == "") // should not be null; // if it is, "last" is missing { std::cout << "'last' key value missing: database seems corrupt"; std::cout << std::endl; theFile->close(0); exit(-1); } std::istringstream *theLastStream = new std::istringstream(lastString.c_str()); (*theLastStream) >> lastKeyAssigned; delete theLastStream; } } return theFile; } void CloseFile(Db &handle) { try { handle.close(0); } catch(DbException &e) { std::cerr << e.what() << std::endl; // throw game exception here? } } void SetLast(Db &handle, const std::string &newKey) { int wrOK = Write(handle, "last", newKey); // Dbt key((void*) "last", 5); // char *p; // // p = MakeCStringCopy(newKey); // // Dbt content((void*) p, strlen(p) + 1); // // try // { // handle.del(NULL, &key, 0); // handle.put(NULL, &key, &content, 0); // } // catch(DbException &e) // { // std::cerr << e.what() << endl; // // maybe propigate the exception to a game exception // } // // delete[] p; } std::string GetLast(Db &handle) { std::string result; int rdOK = Read(handle, "last", result); // Dbt content; // Dbt key((void*) "last", 5); // int getOK = 1; // // try // { // handle.get(NULL, &key, &content, 0); // } // catch(DbException &e) // { // cerr << e.what() << endl; // getOK = 0; // } // // if(getOK) // { // if(content.get_data()) // { // result = (char*) content.get_data(); // } // } // else // { // // throw game exception? // } return result; } int IsAnimal(Db &handle, const std::string &theKey) { int result = 0; std::string theValue; int getOK = Read(handle, theKey, theValue); char firstChar = theValue[0]; result = (firstChar == 'a'); // char *p = MakeCStringCopy(theKey); // Dbt key((void*) p, strlen(p) + 1); // Dbt content; // // try // { // handle.get(NULL, &key, &content, 0); // } // catch(DbException &e) // { // cerr << e.what() << endl; // getOK = 0; // } // // if(p) // delete[] p; // // if(getOK) // { // p = (char *) content.get_data(); // // if(p) // if(*p == 'a') // result = 1; // } // else // { // // perhaps throw an exception here relevent to the game... // } return result; } animals-201207131226/db4.1++-stuff.cc0000644000175000017500000001754711777671070014461 0ustar phiphi// Copyright (c) 1997,2002 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include "db4++-stuff.h" #include "main.h" #include #include #include #include #include #include #include #include #include "str-conversions.h" long lastKeyAssigned; int Write(Db &theFile, const std::string &theKey, const std::string &theValue) { int putBad; Dbt key; Dbt value; key.set_data((void*) theKey.c_str()); key.set_size(strlen(theKey.c_str()) + 1); value.set_data((void*) theValue.c_str()); value.set_size(strlen(theValue.c_str()) + 1); try { putBad = theFile.put(NULL, &key, &value, 0); } catch(DbException &e) { std::cerr << "db2++ Write(Db filehandle, "; std::cerr << '"' << theKey << '"'; std::cerr << ", "; std::cerr << '"' << theValue << '"'; std::cerr << "): " << e.what() << std::endl; putBad = 1; // or throw game exception here? } return ! putBad; } int Read(Db &theFile, const std::string &theKey, std::string &theValue) { int getBad; Dbt key; Dbt value; key.set_data((void*) theKey.c_str()); key.set_size(strlen(theKey.c_str()) + 1); try { getBad = theFile.get(NULL, &key, &value, 0); } catch(DbException &e) { std::cerr << "db3++ Read(Db filehandle, "; std::cerr << '"' << theKey << '"'; std::cerr << ", "; std::cerr << "(some value placeholder)"; std::cerr << "): " << e.what() << std::endl; getBad = 1; // or throw game exception here? } if(value.get_data()) { theValue = (char*) value.get_data(); } else { theValue = ""; } return ! getBad; } Db *OpenFile(const char * dbfile) { Db *theFile; struct stat dummy; if(stat(dbfile, &dummy) == -1) // stat failed? { if(errno == ENOENT) // file not found? { int createBad; // create the file here, fully; ready for animals. mode_t saveMode = umask(0); try { theFile = new Db(NULL, 0); createBad = theFile->open ( NULL, /* transaction object ptr (unused) */ dbfile, NULL, /* name of database within file (unused) */ DB_BTREE /* use B+Tree structure */, DB_CREATE, 0660 ); } catch(DbException &e) { std::cerr << "db2++ Open(): while trying to create"; std::cerr << ": " << e.what() << std::endl; } umask(saveMode); if(createBad) // can't create? { // errno may be set now. // Deal with it... std::cout << progName << ": OpenFile: file " << dbfile << ": errno=" << errno << std::endl; exit(-1); } else { // new gdbm file. Add initial infrastructure. int putBad; putBad = ! Write(*theFile, "last", "0"); //Dbt key; //Dbt lastAddedKey; // //key.set_data((void*) "last"); //key.set_size(5); // //lastAddedKey.set_data((void*) "0"); //lastAddedKey.set_size(2); // //try //{ // putBad = theFile->put(NULL, &key, &lastAddedKey, 0); //} //catch(DbException &e) //{ // cerr << e.what() << endl; // putBad = 1; // or throw game exception here? //} } } // endif(errno == ENOENT) else { } } else { int chmod_res, openBad; try { theFile = new Db(NULL, 0); openBad = theFile->open ( NULL, /* transaction obj ptr (unused) */ dbfile, NULL, /* name of database within file (unused) */ DB_BTREE /* use B+Tree structure */, 0, 0660 ); } catch(DbException &e) { std::cerr << e.what() << std::endl; openBad = 1; // or throw game exception here? } if(openBad) // can't open existing db file? { // either or both of gdbm_errno and errno may be set now. // Deal with it... std::cout << progName << ": OpenFile: file " << dbfile; std::cout << ": errno=" << errno << std::endl; exit(-1); } else { // file is open for update; get "last" into lastKeyAssigned std::string lastString; int getBad; getBad = ! Read(*theFile, "last", lastString); //Dbt key((void*) "last", 5); //Dbt value; // //try // { // getBad = theFile->get(NULL, &key, &value, 0); // } //catch(DbException &e) // { // cerr << e.what() << endl; // getBad = 1; // or throw a game exception? // } // if(lastString == "") // should not be null; // if it is, "last" is missing { std::cout << "'last' key value missing: database seems corrupt"; std::cout << std::endl; theFile->close(0); exit(-1); } std::istringstream *theLastStream = new std::istringstream(lastString.c_str()); (*theLastStream) >> lastKeyAssigned; delete theLastStream; } } return theFile; } void CloseFile(Db &handle) { try { handle.close(0); } catch(DbException &e) { std::cerr << e.what() << std::endl; // throw game exception here? } } void SetLast(Db &handle, const std::string &newKey) { int wrOK = Write(handle, "last", newKey); // Dbt key((void*) "last", 5); // char *p; // // p = MakeCStringCopy(newKey); // // Dbt content((void*) p, strlen(p) + 1); // // try // { // handle.del(NULL, &key, 0); // handle.put(NULL, &key, &content, 0); // } // catch(DbException &e) // { // std::cerr << e.what() << endl; // // maybe propigate the exception to a game exception // } // // delete[] p; } std::string GetLast(Db &handle) { std::string result; int rdOK = Read(handle, "last", result); // Dbt content; // Dbt key((void*) "last", 5); // int getOK = 1; // // try // { // handle.get(NULL, &key, &content, 0); // } // catch(DbException &e) // { // cerr << e.what() << endl; // getOK = 0; // } // // if(getOK) // { // if(content.get_data()) // { // result = (char*) content.get_data(); // } // } // else // { // // throw game exception? // } return result; } int IsAnimal(Db &handle, const std::string &theKey) { int result = 0; std::string theValue; int getOK = Read(handle, theKey, theValue); char firstChar = theValue[0]; result = (firstChar == 'a'); // char *p = MakeCStringCopy(theKey); // Dbt key((void*) p, strlen(p) + 1); // Dbt content; // // try // { // handle.get(NULL, &key, &content, 0); // } // catch(DbException &e) // { // cerr << e.what() << endl; // getOK = 0; // } // // if(p) // delete[] p; // // if(getOK) // { // p = (char *) content.get_data(); // // if(p) // if(*p == 'a') // result = 1; // } // else // { // // perhaps throw an exception here relevent to the game... // } return result; } animals-201207131226/db4.1++-stuff.h0000644000175000017500000000251311777671070014306 0ustar phiphi// Copyright (c) 1997,2002 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include #include #include extern long lastKeyAssigned; int Write(Db &theFile, const std::string &theKey, const std::string &theValue); int Read(Db &theFile, const std::string &theKey, std::string &theValue); Db *OpenFile(const char * dbfile); void CloseFile(Db &handle); void SetLast(Db &handle, const std::string &newKey); std::string GetLast(Db &handle); int IsAnimal(Db &handle, const std::string &theKey); animals-201207131226/gdbm-stuff.cc0000644000175000017500000001233211777671070014417 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include "gdbm-stuff.h" #include "main.h" #include #include #include #include #include #include #include #include long lastKeyAssigned; long CStringToLong(const char *string) { istrstream in(string); long result; in >> result; return result; } char *LongToNewCString(long theLong) { ostrstream out; out << theLong; return MakeCStringCopy(out); } char *MakeCStringCopy(const string &orig) { char *tmp = new char[orig.size() + 1]; if(tmp) { strcpy(tmp, orig.c_str()); } return tmp; } char *MakeCStringCopy(const char *orig) { char *tmp = new char[strlen(orig) + 1]; if(tmp) { strcpy(tmp, orig); } return tmp; } // this copies the current contents of an ostrstream to a C string // whose bytes are allocated w/ new. (so free them w/ delete[]) char *MakeCStringCopy(ostrstream &orig) { int stringSize = orig.pcount(); char *tmp = new char[stringSize + 1]; if(tmp) { memcpy(tmp, orig.str(), stringSize); tmp[stringSize] = '\0'; orig.freeze(0); } return tmp; } GDBMFile OpenFile(const char * dbfile) { GDBMFile result; struct stat dummy; if(stat(dbfile, &dummy) == -1) // stat failed? { if(errno == ENOENT) // file not found? { // create the file here, fully; ready for animals. mode_t saveMode = umask(0); result = gdbm_open ( dbfile, 0 /* use file system blk size */, GDBM_WRCREAT, 0660, 0 /* use default fatal_func */ ); umask(saveMode); if(! result) // can't create? { // either or both of gdbm_errno and errno may be set now. // Deal with it... cout << progName << ": OpenFile: file " << dbfile << ": errno=" << errno; cout << "; gdbm_errno=" << gdbm_errno << endl; exit(-1); } else { // new gdbm file. Add initial infrastructure. datum key; datum lastAddedKey; key.dptr = "last"; key.dsize = 5; lastAddedKey.dptr = "0"; lastAddedKey.dsize = 2; gdbm_store(result, key, lastAddedKey, GDBM_INSERT); } } // endif(errno == ENOENT) else { } } else { int chmod_res; result = gdbm_open ( dbfile, 0 /* use system blk size */, GDBM_WRITER, 0660, 0 /* use default fatal_func */ ); if(! result) // can't open existing db file? { // either or both of gdbm_errno and errno may be set now. // Deal with it... cout << progName << ": OpenFile: file " << dbfile << ": errno=" << errno; cout << "; gdbm_errno=" << gdbm_errno << endl; exit(-1); } else { // file is open for update; get "last" into lastKeyAssigned datum key; datum value; key.dptr = "last"; key.dsize = 5; value = gdbm_fetch(result, key); if(! value.dptr) // should not be null; if it is, "last" is missing { cout << "'last' key value missing: database seems corrupt"<< endl; gdbm_close(result); exit(-1); } istrstream *theLastStream = new istrstream(value.dptr); (*theLastStream) >> lastKeyAssigned; free(value.dptr); delete theLastStream; } } return result; } void CloseFile(GDBMFile handle) { gdbm_close(handle); } void SetLast(GDBMFile handle, const string &newKey) { datum content; datum key = { "last", 5 }; content.dptr = MakeCStringCopy(newKey); content.dsize = strlen(content.dptr) + 1; gdbm_store(handle, key, content, GDBM_REPLACE); delete[] content.dptr; } string GetLast(GDBMFile handle) { string result; datum content; datum key = { "last", 5 }; content = gdbm_fetch(handle, key); // amarin 748 0276 if(content.dptr) { result = MakeCStringCopy(content.dptr); free(content.dptr); } return result; } int IsAnimal(GDBMFile handle, const string &theKey) { int result = 0; datum key; datum content; key.dptr = MakeCStringCopy(theKey); key.dsize = strlen(key.dptr) + 1; content = gdbm_fetch(handle, key); delete[] key.dptr; if(content.dptr) { if(*content.dptr == 'a') result = 1; free(content.dptr); } return result; } animals-201207131226/gdbm-stuff.h0000644000175000017500000000263611777671070014267 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include #include #include typedef GDBM_FILE GDBMFile; extern long lastKeyAssigned; long CStringToLong(const char *string); char *LongToNewCString(long theLong); char *MakeCStringCopy(const string &orig); char *MakeCStringCopy(const char *orig); char *MakeCStringCopy(ostrstream &orig); GDBM_FILE OpenFile(const char * dbfile); void CloseFile(GDBMFile handle); void SetLast(GDBMFile handle, const string &newKey); string GetLast(GDBMFile handle); int IsAnimal(GDBMFile handle, const string &theKey); animals-201207131226/main.cc0000644000175000017500000002017611777671070013312 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include "Record.h" #include "Animal.h" #include "Question.h" #include "db4++-stuff.h" #include "util.h" #include #include #include // #include #include #include #include #include // There are two possible datum formats, both strings; and the records // in both formats are separated by |. // // The first format is for an animal. Its format is simply: // // "a|nameofanimal|". // // The second format is for a yes-or-no question, which has a pair // of gdbm keys in it. The format is: // // "q|text of question without the question mark|yes key|no key|". // // The keys will be simply numeric strings, converted using a strstream. // Because of this, the program can later be expanded to allow larger // numbers of animals and questions, without changing the file format // _at_ _all_. // // There will be one key/value pair in the database, called "last" whose // value is the ascii digits of the key of the last added. If 0, there // are no animals. Db *theFileHandle = NULL; std::string progName; std::string lineBuf; void PlayGame(Db &handle) { std::string theAnimal; std::string last(GetLast(handle)); std::string dbtype_singular(GetDbType(handle, false)); std::string dbtype_plural(GetDbType(handle, true)); if(last == "0") { std::cout << "I give up! You win! What was your " << dbtype_singular << "? " << std::flush; readline(std::cin, theAnimal); while (theAnimal.length() < 1) { std::cout << "What was your " << dbtype_singular << "? " << std::flush; readline(std::cin, theAnimal); } Animal it(theAnimal); it.Write(handle, "1"); SetLast(handle, "1"); std::cout << "I now know 1 " << dbtype_plural << "!\07" << std::endl << std::endl; } else { std::string browseIndex = "1"; Record *theRecordPtr; int isAnimal = IsAnimal(handle, browseIndex); if(isAnimal) theRecordPtr = new Animal(handle, browseIndex); else theRecordPtr = new Question(handle, browseIndex); while(! isAnimal) { Question theQuestion = *((Question *) theRecordPtr); delete theRecordPtr; std::cout << theQuestion << "? "; readline(std::cin, lineBuf); if(lineBuf[0] == 'y') { browseIndex = theQuestion.GetYesKey(); } else if(lineBuf[0] == 'n') { browseIndex = theQuestion.GetNoKey(); } isAnimal = IsAnimal(handle, browseIndex); if(isAnimal) theRecordPtr = new Animal(handle, browseIndex); else theRecordPtr = new Question(handle, browseIndex); } std::cout << std::endl << "I think I'll try a guess now...\07" << std::endl; std::cout << "Is your " << dbtype_singular << " a " << (*theRecordPtr) << "? " << std::flush; readline(std::cin, lineBuf); if(lineBuf[0] == 'y') { std::cout << "I win! I guessed your " << dbtype_singular << "!" << std::endl << std::endl; } else { std::cout << std::endl; std::cout << "I give up! You win! What was your " << dbtype_singular << "? " << std::flush; readline(std::cin, theAnimal); while (theAnimal.length() < 1) { std::cout << "What was your " << dbtype_singular << "? " << std::flush; readline(std::cin, theAnimal); } std::string questionBuf; int goodQuestion = 0; long guessedAnimalKey; long newAnimalKey; int guessedAnimalAnswer; int newAnimalAnswer; Animal newAnimal; Animal guessedAnimal = *((Animal *) theRecordPtr); while(! goodQuestion) { newAnimal = Animal(theAnimal); std::cout << std::endl; std::cout << "I need a yes-or-no question so I can later" << std::endl; std::cout << "tell the difference between a " << guessedAnimal; std::cout << " and a " << newAnimal << "." << std::endl; readline(std::cin, questionBuf); ChopQuestionMark(questionBuf); guessedAnimalKey = CStringToLong( GetLast(handle).c_str() ) + 1; newAnimalKey = guessedAnimalKey + 1; newAnimalAnswer = -1; while(newAnimalAnswer == -1) { std::cout << "what would be the answer be for a "; std::cout << newAnimal << "? " << std::flush; readline(std::cin, lineBuf); newAnimalAnswer = ((lineBuf[0] == 'y') ? 1 : (lineBuf[0] == 'n') ? 0 : -1); } guessedAnimalAnswer = -1; while(guessedAnimalAnswer == -1) { std::cout << "what would be the answer be for a "; std::cout << guessedAnimal << "? " << std::flush; readline(std::cin, lineBuf); guessedAnimalAnswer = ((lineBuf[0] == 'y') ? 1 : (lineBuf[0] == 'n') ? 0 : -1); } if(newAnimalAnswer != guessedAnimalAnswer) goodQuestion = 1; else { std::cout << std::endl; std::cout << "I don't understand! If both answers are the"; std::cout << std::endl; std::cout << "same, maybe I can't use the question! Maybe"; std::cout << std::endl; std::cout << "I misunderstood? Please tell me again!"; std::cout << std::endl; } } const char *newKeyString = LongToNewCString(newAnimalKey); const char *guessedKeyString = LongToNewCString(guessedAnimalKey); Question newQuestion ( // watch for memory leaks in the next line questionBuf.c_str(), (newAnimalAnswer ? newKeyString : guessedKeyString), (newAnimalAnswer ? guessedKeyString : newKeyString) ); newQuestion.Write(handle, browseIndex.c_str()); guessedAnimal.Write(handle, guessedKeyString); newAnimal.Write(handle, newKeyString); SetLast(handle, newKeyString); std::cout << std::endl; std::cout << "I now know " << (newAnimalKey + 1) / 2 << " " << dbtype_plural << "!"; std::cout << std::endl; std::cout << std::endl; } } } void SigHandler(int sig) { if (theFileHandle) { CloseFile(*theFileHandle); } std::cout << "Killed..." << std::endl; exit(0); } int main(int argc, char * argv[]) { std::string wantsToContinue("y"); std::string dbtype_singular; const char * dbfile = DbName; for (int i = 1; i < argc; i++) { if ( !strcmp(argv[i], "-h") || !strcmp(argv[i], "--help") ) { printf("Usage: %s [DatabaseFile [TypeString [PluralTypeString]]]\n", argv[0]); printf(" Or: %s {-h|--help}\n", argv[0]); return 0; } } progName = argv[0]; if ( argc >= 2 ) { dbfile = argv[1]; } signal(SIGHUP, SigHandler); signal(SIGTERM, SigHandler); signal(SIGINT, SigHandler); std::cout << std::endl << "Welcome to animals!" << std::endl << std::endl; do { theFileHandle = OpenFile(dbfile); if ( argc >= 3 ) { SetDbType(*theFileHandle, argv[2], false); if ( argc >= 4 ) { SetDbType(*theFileHandle, argv[3], true); } } dbtype_singular = GetDbType(*theFileHandle, false); if ( theFileHandle == NULL ) { return 1; } std::cout << "Think of an " << dbtype_singular << "." << std::endl; std::cout << "Press or to continue..." << std::flush; readline(std::cin, lineBuf); std::cout << std::endl; PlayGame(*theFileHandle); CloseFile(*theFileHandle); std::cout << "Want to play again? " << std::flush; readline(std::cin, lineBuf); std::cout << std::endl; wantsToContinue = lineBuf[0]; } while(wantsToContinue == "y" || wantsToContinue == "s"); return 0; } animals-201207131226/main.h0000644000175000017500000000174111777671070013151 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #ifndef main_h #define main_h #include extern std::string progName; #endif animals-201207131226/str-conversions.cc0000644000175000017500000000363211777671070015542 0ustar phiphi// Copyright (c) 2003 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include "str-conversions.h" #include #include #include long CStringToLong(const char *string) { std::istringstream in(string); long result; in >> result; return result; } char *LongToNewCString(long theLong) { std::ostringstream out; out << theLong; return MakeCStringCopy(out); } char *MakeCStringCopy(const std::string &orig) { char *tmp = new char[orig.size() + 1]; if(tmp) { strcpy(tmp, orig.c_str()); } return tmp; } char *MakeCStringCopy(const char *orig) { char *tmp = new char[strlen(orig) + 1]; if(tmp) { strcpy(tmp, orig); } return tmp; } // this copies the current contents of an ostrstream to a C string // whose bytes are allocated w/ new. (so free them w/ delete[]) char *MakeCStringCopy(const std::ostringstream &orig) { std::string theStrObj = orig.str(); const char *theStr = theStrObj.c_str(); int stringSize = strlen(theStr); char *tmp = new char[stringSize + 1]; if(tmp) { strcpy(tmp, theStr); } return tmp; } animals-201207131226/str-conversions.h0000644000175000017500000000231311777671070015377 0ustar phiphi// Copyright (c) 2003 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #ifndef str_conversions_h #define str_conversions_h #include #include long CStringToLong(const char *string); char *LongToNewCString(long theLong); char *MakeCStringCopy(const std::string &orig); char *MakeCStringCopy(const char *orig); char *MakeCStringCopy(const std::ostringstream &orig); #endif animals-201207131226/test-str-cvt.cc0000644000175000017500000000647411777671070014752 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include #include #include #include "str-conversions.h" void readline(std::istream &in, std::string &it) { getline(in, it); } int IsSpace(const char theChar) { int result = 0; switch(theChar) { case ' ': case '\n': case '\t': case '\r': case '\v': case '\f': result = 1; break; } return result; } void ChopTrailingWhiteSpace(std::string &it) { int n = it.size() - 1; while(IsSpace( it[n] )) n--; it = it.substr(0, n + 1); } void ChopQuestionMark(std::string &it) { ChopTrailingWhiteSpace(it); if(*(it.end() - 1) == '?') { it = it.substr(0, it.size() - 1); ChopTrailingWhiteSpace(it); } } std::string progName; std::string line; // void ChopQuestionMark(std::string &it) // void ChopTrailingWhiteSpace(std::string &it) // int IsSpace(const char theChar) // void readline(std::istream &in, std::string &it) int main(int argc, char *argv[]) { progName = argv[0]; std::cout << "I'm reading a line" << std::endl; readline(std::cin, line); std::cout << "the line I just read was" << std::endl; std::cout << '|' << line << '|' << std::endl; ChopTrailingWhiteSpace(line); std::cout << "the line without trailing spaces" << std::endl; std::cout << '|' << line << '|' << std::endl; ChopQuestionMark(line); std::cout << "the line without the last trailing '?'" << std::endl; std::cout << '|' << line << '|' << std::endl; std::cout << "===== end of my own functions =====" << std::endl; std::cout << "Enter a long:" << std::endl; std::string theLongStr; long theLong; readline(std::cin, theLongStr); // will a string be converted automatically to a const char*? No. const char *theLongCStr = theLongStr.c_str(); theLong = CStringToLong(theLongCStr); const char *theLongCStr2 = LongToNewCString(theLong); std::cout << "the long was " << theLongCStr2 << std::endl; char *s1 = MakeCStringCopy(theLongStr); char *s2 = MakeCStringCopy(theLongCStr); std::ostringstream ss3(theLongStr); char *s3 = MakeCStringCopy(ss3); std::cout << "s1 was |" << s1 << "|" << std::endl; std::cout << "s2 was |" << s2 << "|" << std::endl; std::cout << "s3 was |" << s3 << "|" << std::endl; delete theLongCStr2; // note!! placement of this seems critical... // it can't be right after its last use for some // reason return 0; } // std::cout << "" << std::endl; animals-201207131226/test.cc0000644000175000017500000000260311777671070013340 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010-2012 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include #include #include "Record.h" #include "Animal.h" #include "Question.h" #include "db4++-stuff.h" #include "util.h" #include #include std::string progName; int main(int argc, char *argv[]) { Db *theFileHandle; std::string wantsToContinue; char ret[3]; progName = argv[0]; theFileHandle = OpenFile(DbName); std::string response(GetLast(*theFileHandle)); CloseFile(*theFileHandle); std::cout << response << std::endl; return 0; } animals-201207131226/tut.cc0000644000175000017500000000315311416745170013167 0ustar phiphi#include #include #include #define DATABASE "access.db" int main(void) { Db *dbp; Dbt key, data; int ret, t_ret, openBad; int getBad, putBad, delBad; int result = 0; // create a dbp openBad = Db::open(DATABASE, DB_BTREE, DB_CREATE, 0600, NULL, NULL, &dbp); if(! openBad) { // // open (creating if necessary) the file name DATABASE // ret = dbp->open(dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664); // set key to "fruit" key.set_data((void*) "fruit"); key.set_size(sizeof("fruit")); // set data to "apple" data.set_data((void*) "apple"); data.set_size(sizeof("apple")); // try to put it in the database putBad = dbp->put(NULL, &key, &data, 0); if (! putBad) printf("db: %s: key stored.\n", (char*) key.get_data()); else { printf("DB->put\n"); goto err; } // try to get it back getBad = dbp->get(NULL, &key, &data, 0); if (! getBad) printf ( "db: %s: key retrieved: data was %s.\n", (char *)key.get_data(), (char *)data.get_data() ); else { printf("DB->get\n"); goto err; } // remove the key delBad = dbp->del(NULL, &key, 0); if (! delBad) printf("db: %s: key was deleted.\n", (char *)key.get_data()); else { printf("DB->del\n"); goto err; } err: t_ret = dbp->close(0); if (t_ret != 0 && ret == 0) result = t_ret; } else { fprintf(stderr, "db_create: %s\n", "couldn't open"); result = 1; } return result; } animals-201207131226/util.cc0000644000175000017500000000302311777671070013333 0ustar phiphi// Copyright (c) 1997, 2003 by Jim Lynch. // Copyright (c) 2010 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include "util.h" void readline(std::istream &in, std::string &it) { getline(in, it); } int IsSpace(const char theChar) { int result = 0; switch(theChar) { case ' ': case '\n': case '\t': case '\r': case '\v': case '\f': result = 1; break; } return result; } void ChopTrailingWhiteSpace(std::string &it) { int n = it.size(); while(IsSpace( it[--n] )) ; it = it.substr(0, n + 1); } void ChopQuestionMark(std::string &it) { ChopTrailingWhiteSpace(it); while(*(it.end() - 1) == '?') { it = it.substr(0, it.size() - 1); ChopTrailingWhiteSpace(it); } } animals-201207131226/util.h0000644000175000017500000000217411777671070013203 0ustar phiphi// Copyright (c) 1997 by Jim Lynch. // Copyright (c) 2010 by Philipp Schafft. // This software comes with NO WARRANTY WHATSOEVER. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991, or, at your // option, any LATER version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this software; see the file COPYING. If not, write to // the Free Software Foundation, 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #ifndef util_h #define util_h #include #include void readline(std::istream &in, std::string &it); int IsSpace(const char theChar); void ChopTrailingWhiteSpace(std::string &it); void ChopQuestionMark(std::string &it); #endif