⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fooobject.cpp

📁 用于词法分析的词法分析器
💻 CPP
字号:
/*  $Id: FooObject.cpp,v 1.7 1997/02/02 01:31:01 matt Exp $  FooObject class.    (c) July 96 Matt Phillips*/#include <ctype.h>#include "FooObject.h"const FooObject::Objects FooObject::emptyList;const FooObject::Attrs FooObject::emptyAttrs;const string FooObject::emptyString;static int toInt (const string &str){  int intValue = 0;  int i;  int negative = 1;  // skip whitespace  for (i = 0; i < str.length () && isspace (str [i]); i++);    // handle negative value  if (i < str.length () && str [i] == '-')  {    negative = -1;    i++;  }  // read int value  while (i < str.length () && isdigit (str [i]))  {    intValue *= 10;    intValue += str [i] - '0';    i++;  }  return intValue * negative;}FooObject::FooObject () : attrs (0), stringValue (0){}FooObject::FooObject (const string &value) :  attrs (0), stringValue (new string (value)){}FooObject::~FooObject (){  delete stringValue;  delete attrs;}void FooObject::addAttr (const string &attr, FooObject &value){  Objects &attrValues = getOrCreateAttrs (attr);      attrValues.addTail (value);}void FooObject::addAttr (const string &attr,			 const FooObject::Objects &values){  Objects &attrValues = getOrCreateAttrs (attr);      attrValues.addTail (values);}void FooObject::addAttr (const string &value){  if (stringValue == 0)    stringValue = new string (value);  else    *stringValue = value;}const FooObject::Objects &FooObject::getAttrs (const string &attr) const{  Objects *attrValues = getAttrValues (attr);  if (attrValues)    return *attrValues;  else    return emptyList;}FooObject::Objects &FooObject::getOrCreateAttrs (const string &attr){  Objects *values;  if (!attrs)  {    attrs = new Attrs;    values = 0;  } else    values = attrs->get (attr);    if (!values)  {    values = new Objects;    attrs->add (*new string (attr), *values);  }  return *values;}FooObject *FooObject::getAttr (const string &attr) const{  Objects *values = getAttrValues (attr);  if (values && values->nItems () > 0)    return &(values->peekTail ());  else    return 0;}const string &FooObject::getAttr () const{  if (stringValue)    return *stringValue;  else    return emptyString;}int FooObject::getAttrInt () const{  return toInt (getAttr ());}string &FooObject::getOrCreateAttr (){  if (!stringValue)    stringValue = new string;  return *stringValue;}const string &FooObject::getAttrString (const string &attr) const{  FooObject *object = getAttr (attr);  if (object)    return object->getAttr ();  else    return emptyString;}int FooObject::getAttrInt (const string &attr) const{  return toInt (getAttrString (attr));}FooObject::Iterator FooObject::getIter () const{  if (!attrs)    return Iterator (emptyAttrs);  else    return Iterator (*attrs);}int FooObject::nAttrs () const{  if (attrs)    return attrs->nItems ();  else    return 0;}FooObject *FooObject::getAttrPath (const string &path) const{  Objects *objects = getAttrsPathPtr (path);  if (objects && objects->nItems () > 0)    return &(objects->peekTail ());  else    return 0;}FooObject::Objects *FooObject::getAttrsPathPtr (const string &path) const{  int start = 0, end;  const FooObject *object = this;  Objects *objects;  do  {    end = path.find ('/', start);    int length;    if (end == string::npos)      length = path.length () - start;    else      length = end - start;    if (object && object->attrs)    {      objects = object->attrs->get (path.substr (start, length));      if (objects && objects->nItems () > 0)	object = &(objects->peekTail ());      else	objects = 0;    } else      objects = 0;    start = end + 1;  } while (end != string::npos && objects != 0);  return objects;}const FooObject::Objects &FooObject::getAttrsPath (const string &path) const{  Objects *objects = getAttrsPathPtr (path);  if (objects)    return *objects;  else    return emptyList;}const string &FooObject::getAttrStringPath (const string &path) const{  Objects *objects = getAttrsPathPtr (path);  if (objects && objects->nItems () > 0)    return objects->peekTail ().getAttr ();  else    return emptyString;}string *FooObject::getAttrStringPathPtr (const string &path) const{  Objects *objects = getAttrsPathPtr (path);  if (objects && objects->nItems () > 0)    return objects->peekTail ().stringValue;  else    return 0;}int FooObject::getAttrIntPath (const string &path) const{  return toInt (getAttrStringPath (path));}FooObject::Objects *FooObject::getAttrValues (const string &attr) const{  if (!attrs)    return 0;  else    return attrs->get (attr);}int FooObject::removeAttr (const string &attr){  if (attrs)    return attrs->remove (attr);  else    return 0;}int FooObject::removeAttr (){  if (stringValue)  {    delete stringValue;    stringValue = 0;    return 1;  } else    return 0;}template class TypeIOLinkedListWithTail(FooObject);template class TypeIOOpenHash(string, FooObject::Objects, FooObject::Hash);template class Container<FooObject>;template class DAssocKey<basic_string<char, string_char_traits<char> > >;template class RContainerIter<FooObject>;template class ContainerIter<FooObject>;template class IContainerData<FooObject, 1>;template class List<FooObject>;template class ILinkedItem<FooObject, 1>;template class LinkedListImp<FooObject, ILinkedItem<FooObject, 1> >;template class LinkedListWithTailImpIter<FooObject, ILinkedItem<FooObject, 1> >;template class Container<LinkedListWithTailImp<FooObject, ILinkedItem<FooObject, 1> > >;template class ITAssocItem<basic_string<char, string_char_traits<char> >, LinkedListWithTailImp<FooObject, ILinkedItem<FooObject, 1> >, 1>;template class OpenHashImpIter<basic_string<char, string_char_traits<char> >, LinkedListWithTailImp<FooObject, ILinkedItem<FooObject, 1> >, ITAssocItem<basic_string<char, string_char_traits<char> >, LinkedListWithTailImp<FooObject, ILinkedItem<FooObject, 1> >, 1>, FooObject::Hash>;template class ContainerIter<LinkedListWithTailImp<FooObject, ILinkedItem<FooObject, 1> > >;template class LinkedListImpIter<FooObject, ILinkedItem<FooObject, 1> >;template class ListIter<FooObject>;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -