Logo
~Apps~
~Projects~
~Contact~


BString Class Reference

metainfo file reader More...

#include <BString.h>

Collaboration diagram for BString:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 BString ()
 ~BString ()
void read_file (FILE *fil)
void read_buf (const char *, size_t)
void Show ()
BTObjectGetBTObject (const std::string &)
BTIntegerGetInteger (const std::string &)
BTStringGetString (const std::string &)
std::string GetBString (const std::string &path)
std::string printable (const std::string &s)
size_t GetIndex ()
unsigned char * GetHash (const std::string &node)
const std::string & GetHashAsString (const std::string &node)

Private Member Functions

bool reg_char (char)
void AddDictionary ()
void AddList ()
void AddInteger (const std::string &)
void AddString (const std::string &)
void End ()

Private Attributes

std::string m_bstring
size_t m_bindex
BTList m_root
BTObjectm_current
int m_state
int m_strlen
std::string m_str
std::string m_name
bool m_bDictionary
bool m_bList
bool m_bKey
unsigned char m_hash [20]
std::string m_hash_str

Detailed Description

metainfo file reader

Definition at line 36 of file BString.h.


Constructor & Destructor Documentation

BString::BString (  ) 

Definition at line 53 of file BString.cpp.

References m_current, and m_root.

00054 :m_bindex(0)
00055 ,m_root(0)
00056 ,m_current(NULL)
00057 ,m_state(0)
00058 ,m_strlen(0)
00059 ,m_str("")
00060 ,m_name("")
00061 ,m_bDictionary(false) // key-value pairs
00062 ,m_bList(false) // values
00063 ,m_bKey(false) // used when m_bDictionary is true
00064 {
00065         m_current = &m_root;
00066 }

BString::~BString (  ) 

Definition at line 69 of file BString.cpp.

00070 {
00071 }


Member Function Documentation

void BString::read_file ( FILE *  fil  ) 

Definition at line 74 of file BString.cpp.

References reg_char().

Referenced by MetainfoSocket::InitSession(), main(), and validate().

00075 {
00076         char c;
00077 
00078         fread(&c,1,1,fil);
00079         while (!feof(fil))
00080         {
00081                 if (!reg_char(c))
00082                 {
00083                         break;
00084                 }
00085                 //
00086                 fread(&c,1,1,fil);
00087         }
00088 }

void BString::read_buf ( const char *  ,
size_t   
)

Definition at line 91 of file BString.cpp.

References reg_char().

00092 {
00093         for (size_t i = 0; i < l; i++)
00094         {
00095                 if (!reg_char(p[i]))
00096                 {
00097                         break;
00098                 }
00099         }
00100 }

void BString::Show (  ) 

Definition at line 343 of file BString.cpp.

References m_root, and BTList::Show().

00344 {
00345         m_root.Show();
00346         printf("\n");
00347 }

BTObject * BString::GetBTObject ( const std::string &   ) 

Definition at line 350 of file BString.cpp.

References m_root.

Referenced by GetBString(), GetInteger(), GetString(), MetainfoSocket::InitSession(), and main().

00351 {
00352         ParseBT pa( (char *)path.c_str(), ".", 1);
00353         char slask[200];
00354         BTObject *p = &m_root;
00355         BTList *l = dynamic_cast<BTList *>(p);
00356 //      assert(l && l -> GetSize() == 1);
00357         if (!l || l -> GetSize() != 1)
00358         {
00359                 throw(BTException("GetBTObject"));
00360         }
00361         p = l -> GetFirst();
00362         pa.getword(slask);
00363         while (*slask)
00364         {
00365                 BTDictionary *d = dynamic_cast<BTDictionary *>(p);
00366 //              assert(d);
00367                 if (!d)
00368                 {
00369                         throw(BTException("Not Dictionary"));
00370                 }
00371                 p = d -> Find(slask);
00372                 if (!p)
00373                         return NULL;
00374                 pa.getword(slask);
00375         }
00376         return p;
00377 }

BTInteger * BString::GetInteger ( const std::string &   ) 

Definition at line 380 of file BString.cpp.

References GetBTObject().

Referenced by MetainfoSocket::InitSession(), and main().

00381 {
00382         BTObject *p = GetBTObject(str);
00383         return dynamic_cast<BTInteger *>(p);
00384 }

BTString * BString::GetString ( const std::string &   ) 

Definition at line 387 of file BString.cpp.

References GetBTObject().

Referenced by MetainfoSocket::InitSession(), and main().

00388 {
00389         BTObject *p = GetBTObject(str);
00390         return dynamic_cast<BTString *>(p);
00391 }

std::string BString::GetBString ( const std::string &  path  ) 

Definition at line 394 of file BString.cpp.

References GetBTObject(), GetIndex(), and m_bstring.

Referenced by GetHash(), and GetHashAsString().

00395 {
00396         BTObject *p = GetBTObject(path);
00397         size_t begin = p -> GetIndex();
00398         size_t length = p -> GetLength() + 1;
00399         std::string str = m_bstring.substr(begin,length);
00400 //      printf("%s\n",printable(str).c_str());
00401         return str;
00402 }

std::string BString::printable ( const std::string &  s  )  [inline]

Definition at line 52 of file BString.h.

00052                                                 {
00053                 std::string str;
00054                 for (size_t i = 0; i < s.size(); i++)
00055                 {
00056                         if (isprint(s[i]))
00057                                 str += s[i];
00058                         else
00059                                 str += ".";
00060                 }
00061                 return str;
00062         }

size_t BString::GetIndex (  )  [inline]

Definition at line 63 of file BString.h.

References m_bindex.

Referenced by GetBString().

00063 { return m_bindex; }

unsigned char * BString::GetHash ( const std::string &  node  ) 

Definition at line 405 of file BString.cpp.

References GetBString(), and m_hash.

Referenced by MetainfoSocket::InitSession().

00406 {
00407         std::string tmp = GetBString( node );
00408         SHA1( (unsigned char *)tmp.c_str(), tmp.size(), m_hash);
00409         return m_hash;
00410 }

const std::string & BString::GetHashAsString ( const std::string &  node  ) 

Definition at line 413 of file BString.cpp.

References GetBString(), m_hash, and m_hash_str.

Referenced by MetainfoSocket::InitSession(), and main().

00414 {
00415         std::string tmp = GetBString( node );
00416         char slask[100];
00417         SHA1( (unsigned char *)tmp.c_str(), tmp.size(), m_hash);
00418         *slask = 0;
00419         for (size_t i = 0; i < 20; i++)
00420                 sprintf(slask + strlen(slask), "%02x", m_hash[i]);
00421         m_hash_str = slask;
00422         return m_hash_str;
00423 }

bool BString::reg_char ( char   )  [private]

Referenced by read_buf(), and read_file().

void BString::AddDictionary (  )  [private]

Definition at line 232 of file BString.cpp.

References m_bindex, m_current, and m_name.

00233 {
00234         BTDictionary *tmp = new BTDictionary(m_bindex);
00235         
00236         tmp -> SetParent(m_current);
00237 
00238         switch (m_current -> Type())
00239         {
00240         case 'd': // dictionary
00241                 m_current -> AddObject(m_name, tmp);
00242                 m_current = tmp;
00243                 m_name = "";
00244                 break;
00245         case 'l': // list
00246                 m_current -> AddObject(tmp);
00247                 m_current = tmp;
00248                 break;
00249         default:
00250 //              assert(!"no good");
00251                 throw(BTException("AddDictionary"));
00252                 break;
00253         }
00254 }

void BString::AddList (  )  [private]

Definition at line 257 of file BString.cpp.

References m_bindex, m_current, and m_name.

00258 {
00259         BTList *tmp = new BTList(m_bindex);
00260         
00261         tmp -> SetParent(m_current);
00262 
00263         switch (m_current -> Type())
00264         {
00265         case 'd': // dictionary
00266                 m_current -> AddObject(m_name, tmp);
00267                 m_current = tmp;
00268                 m_name = "";
00269                 break;
00270         case 'l': // list
00271                 m_current -> AddObject(tmp);
00272                 m_current = tmp;
00273                 break;
00274         default:
00275 //              assert(!"no good");
00276                 throw(BTException("AddList"));
00277                 break;
00278         }
00279 }

void BString::AddInteger ( const std::string &   )  [private]

Definition at line 282 of file BString.cpp.

References m_current, and m_name.

00283 {
00284         BTInteger *tmp = new BTInteger;
00285         
00286         tmp -> SetParent(m_current);
00287         tmp -> SetValue(str);
00288 
00289         switch (m_current -> Type())
00290         {
00291         case 'd': // dictionary
00292                 m_current -> AddObject(m_name, tmp);
00293                 m_name = "";
00294                 break;
00295         case 'l': // list
00296                 m_current -> AddObject(tmp);
00297                 break;
00298         default:
00299                 throw(BTException("AddInteger"));
00300 //              assert(!"no good");
00301                 break;
00302         }
00303 }

void BString::AddString ( const std::string &   )  [private]

Definition at line 306 of file BString.cpp.

References m_current, and m_name.

00307 {
00308         if (m_current -> Type() == 'd' && m_name.size() == 0)
00309         {
00310                 m_name = str;
00311                 return;
00312         }
00313         BTString *tmp = new BTString;
00314 
00315         tmp -> SetParent(m_current);
00316         tmp -> SetValue(str);
00317 
00318         switch (m_current -> Type())
00319         {
00320         case 'd': // dictionary
00321                 m_current -> AddObject(m_name, tmp);
00322                 m_name = "";
00323                 break;
00324         case 'l': // list
00325                 m_current -> AddObject(tmp);
00326                 break;
00327         default:
00328                 throw(BTException("AddString"));
00329 //              assert(!"no good");
00330                 break;
00331         }
00332 }

void BString::End (  )  [private]

Definition at line 335 of file BString.cpp.

References DEB, and m_current.

00336 {
00337         m_current = m_current -> GetParent();
00338 DEB(    printf(" * Current type is '%c'\n",m_current -> Type());)
00339 
00340 }


Member Data Documentation

std::string BString::m_bstring [private]

Definition at line 75 of file BString.h.

Referenced by GetBString().

size_t BString::m_bindex [private]

Definition at line 76 of file BString.h.

Referenced by AddDictionary(), AddList(), and GetIndex().

Definition at line 77 of file BString.h.

Referenced by BString(), GetBTObject(), and Show().

Definition at line 78 of file BString.h.

Referenced by AddDictionary(), AddInteger(), AddList(), AddString(), BString(), and End().

int BString::m_state [private]

Definition at line 79 of file BString.h.

int BString::m_strlen [private]

Definition at line 80 of file BString.h.

std::string BString::m_str [private]

Definition at line 81 of file BString.h.

std::string BString::m_name [private]

Definition at line 82 of file BString.h.

Referenced by AddDictionary(), AddInteger(), AddList(), and AddString().

bool BString::m_bDictionary [private]

Definition at line 83 of file BString.h.

bool BString::m_bList [private]

Definition at line 84 of file BString.h.

bool BString::m_bKey [private]

Definition at line 85 of file BString.h.

unsigned char BString::m_hash[20] [private]

Definition at line 87 of file BString.h.

Referenced by GetHash(), and GetHashAsString().

std::string BString::m_hash_str [private]

Definition at line 88 of file BString.h.

Referenced by GetHashAsString().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4