/* Program: Objects Author : Kim Moser Date : System : IBM PC / Borland C++ 3.0 Descrip: */ #include #include #include "lists.h" static char* strncpyz(char* dest, char* src, int n); static char* strncpyz(char* dest, char* src, int n) /* Same as strncpy(), but always null-terminates 'dest' at or before dest[i-1] */ { int i = strlen(src); char* r = strncpy(dest, src, n); if (i && (i >= n)) { dest[i-1] = '\0'; } return (r); } static const MAX_ATTRIBUTE_NAME_LEN = 40; static const MAX_KLASS_NAME_LEN = 40; class attribute { private: char thename[MAX_ATTRIBUTE_NAME_LEN+1]; // This attribute's name public: inline char* attributename(void) { return (thename); } int setname(char* s); }; int attribute::setname(char* s) { int r = 1; // Assume if (strlen(s) > MAX_ATTRIBUTE_NAME_LEN) { cerr << "attribute::setname(): name \"" << s << "\" exceeds max len (" << MAX_ATTRIBUTE_NAME_LEN << ")\n"; r = 0; } strncpyz(thename, s, MAX_ATTRIBUTE_NAME_LEN+1); return (r); } // ********************************************************************** class object { friend object* newobject(char* name); friend int numobjects(void); friend object* nthobject(int n); private: static list TheObjects; // List of objects char thename[MAX_KLASS_NAME_LEN+1]; // This class's name list attribs; // List of this type of object's attributes list all; // List of attributes which describe ALL objects of this type list some; // List of attributes which describe SOME objects of this type list none; // List of attributes which describe NO objects of this type object(object& k) {}; public: object(void) {} inline char* objectname(void) { return (thename); } void* operator new(size_t size, char* name); void operator delete(void* k); int objecthasattribute(char* s); int noobjecthasattribute(char* s); int someobjectshaveattribute(char* s); int allobjectshaveattribute(char* s); int giveobjectattribute(char* s); int givenoobjectattribute(char* s); int givesomeobjectsattribute(char* s); int giveallobjectsattribute(char* s); }; list object::TheObjects; // List of objects #pragma argsused // Suppress "argument 'size' never used... void* object::operator new(size_t size, char* name) { object* k; k = (object*) ::operator new(sizeof(object)); strcpy(k->thename, name); return (k); } void object::operator delete(void* k) { ((object*) k)->thename[0] = '\0'; // not really necessary } static int objectexists(char* name) { for (int i=numobjects()-1; i >= 0; i--) { if (!strcmp(nthobject(i)->objectname(), name)) return (1); } return (0); } static object* newobject(char* name); static object* newobject(char* name) { object* k = new(name) object; object::TheObjects.listinsert(k, sizeof(object), 32767); return (k); } static int numobjects(void); static int numobjects(void) { return (object::TheObjects.listlen()); } static object* nthobject(int n); static object* nthobject(int n) { return ((object*) object::TheObjects.listelmt(n)); } static void deleteobject(object* k); static void deleteobject(object* k) { delete k; } static void deletenthobject(int n); static void deletenthobject(int n) { deleteobject(nthobject(n)); } int listcontainsstring(list& p, char* s); int listcontainsstring(list& p, char* s) { for (int i=p.listlen()-1; i >= 0; i--) { if (!strcmp((char*) p.listelmt(i), s)) { return (1); } } return (0); } int listcontainslist(list& p, list& q); int listcontainslist(list& p, list& q) // Returns whether all strings in list 'q' are in list 'p' { for (int i=q.listlen()-1; i >= 0; i--) { if (!listcontainsstring(p, (char*) q.listelmt(i))) { return (0); } } return (1); } int listequalslist(list& p, list& q); int listequalslist(list& p, list& q) // Returns whether all strings in list 'q' are in list 'p' { if (p.listlen() != q.listlen()) return (0); for (int i=q.listlen()-1; i >= 0; i--) { if (!strcmp((char*) p.listelmt(i), (char*) q.listelmt(i))) return (0); } return (1); } int insertstringsorted(list& p, char* s); int insertstringsorted(list& p, char* s) { int i; int len = p.listlen(); for (i=0; i < len; i++) { if (strcmp(s, (char*) p.listelmt(i)) > 0) break; } return (p.listinsert(s, strlen(s)+1, i) != NULL); } int object::objecthasattribute(char* s) { return (listcontainsstring(attribs, s)); } int object::noobjecthasattribute(char* s) { return (listcontainsstring(none, s)); } int object::someobjectshaveattribute(char* s) { return (listcontainsstring(some, s)); } int object::allobjectshaveattribute(char* s) { return (listcontainsstring(all, s)); } int object::giveobjectattribute(char* s) { return (insertstringsorted(attribs, s)); } int object::givenoobjectattribute(char* s) { return (insertstringsorted(none, s)); } int object::givesomeobjectsattribute(char* s) { return (insertstringsorted(some, s)); } int object::giveallobjectsattribute(char* s) { return (insertstringsorted(all, s)); } // ********************************************************************** /* [attribute ...] IS|ARE [attribute ...]. IS [A|AN] [attribute ...] . IS ANYTHING ? WHAT IS ? IS ? IS EVERY [attribute ...] ? IS ANY " " " WHAT|WHICH IS|ARE ? WHAT|WHICH IS|ARE NOT ? */ main(void) { object* k = newobject("testing"); deleteobject(k); return (0); }