r_folder.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       r_folder.cc
00003 ///             Record parsing class for the folders database.
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2009, Net Direct Inc. (http://www.netdirect.ca/)
00008     Copyright (C) 2007, Brian Edginton
00009 
00010     This program is free software; you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation; either version 2 of the License, or
00013     (at your option) any later version.
00014 
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 
00019     See the GNU General Public License in the COPYING file at the
00020     root directory of this project for more details.
00021 */
00022 
00023 #include "r_folder.h"
00024 #include "record-internal.h"
00025 #include "protostructs.h"
00026 #include "data.h"
00027 #include "time.h"
00028 #include "debug.h"
00029 #include "iconv.h"
00030 #include <ostream>
00031 #include <iomanip>
00032 
00033 using namespace std;
00034 using namespace Barry::Protocol;
00035 
00036 namespace Barry {
00037 
00038 ///////////////////////////////////////////////////////////////////////////////
00039 // Folder Class
00040 
00041 // Folder Field Codes
00042 
00043 #define FFC_NUMBER      0x0a
00044 #define FFC_LEVEL       0x0b
00045 #define FFC_NAME        0x0c
00046 #define FFC_ADDRESS1    0x0d
00047 #define FFC_ADDRESS2    0x0e
00048 #define FFC_TYPE        0x0f
00049 #define FFC_END 0xffff
00050 
00051 // Folder Types
00052 #define SUBTREE 0x00
00053 #define DELETED 0x01
00054 #define INBOX           0x02
00055 #define OUTBOX          0x03
00056 #define SENT            0x04
00057 #define OTHER           0x05
00058 #define DRAFT           0x0a
00059 
00060 // Folder Status
00061 #define ORPHAN          0x50
00062 #define UNFILED 0x51
00063 #define FILED           0x52
00064 
00065 #define INVALID         -1
00066 
00067 #define SEPARATOR       0x2f
00068 #define ROOT_SEPARATOR  0x3a
00069 
00070 static FieldLink<Folder> FolderFieldLinks[] = {
00071     { FFC_NAME, "FolderName",  0, 0, &Folder::FolderName, 0, 0, 0, 0, true },
00072     { FFC_END,  "End of List", 0, 0, 0, 0, 0, 0, 0, false },
00073 };
00074 
00075 Folder::Folder()
00076 {
00077         Clear();
00078 }
00079 
00080 
00081 Folder::~Folder()
00082 {
00083 }
00084 
00085 const unsigned char* Folder::ParseField(const unsigned char *begin,
00086                                         const unsigned char *end,
00087                                         const IConverter *ic)
00088 {
00089         const CommonField *field = (const CommonField *) begin;
00090 
00091         // advance and check size
00092         begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
00093         if( begin > end )       // if begin==end, we are ok
00094                 return begin;
00095 
00096         if( !btohs(field->size) )   // if field has no size, something's up
00097                 return begin;
00098         
00099         // cycle through the type table
00100         for(    FieldLink<Folder> *b = FolderFieldLinks;
00101                 b->type != FFC_END;
00102                 b++ )
00103         {
00104                 if( b->type == field->type ) {
00105                         if( b->strMember ) {
00106                                 std::string &s = this->*(b->strMember);
00107                                 s = ParseFieldString(field);
00108                                 if( b->iconvNeeded && ic )
00109                                         s = ic->FromBB(s);
00110                                 return begin;   // done!
00111                         }
00112                         else if( b->timeMember && btohs(field->size) == 4 ) {
00113                                 time_t &t = this->*(b->timeMember);
00114                                 t = min2time(field->u.min1900);
00115                                 return begin;
00116                         }
00117                 }
00118         }
00119         // handle special cases
00120         switch( field->type )
00121         {
00122         case FFC_TYPE:
00123                 FolderType = (FolderTypeEnum)field->u.raw[0];
00124                 return begin;
00125         case FFC_NUMBER:
00126                 FolderNumber = field->u.raw[0]; // two's complement
00127                 return begin;
00128         case FFC_LEVEL:
00129                 FolderLevel = field->u.raw[0];
00130                 return begin;
00131         }
00132 
00133         // if still not handled, add to the Unknowns list
00134         UnknownField uf;
00135         uf.type = field->type;
00136         uf.data.assign((const char*)field->u.raw, btohs(field->size));
00137         Unknowns.push_back(uf);
00138 
00139         // return new pointer for next field
00140         return begin;
00141 }
00142 
00143 void Folder::ParseHeader(const Data &data, size_t &offset)
00144 {
00145         // no header in Folder records
00146 }
00147 
00148 void Folder::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
00149 {
00150         const unsigned char *finish = ParseCommonFields(*this,
00151                 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
00152         offset += finish - (data.GetData() + offset);
00153 }
00154 
00155 void Folder::Clear()
00156 {
00157         FolderName.clear();
00158         Unknowns.clear();
00159         FolderType = FolderSubtree;
00160 }
00161 
00162 void Folder::Dump(std::ostream &os) const
00163 {
00164         static const char *FolderTypeString[] = { "Subtree", "Deleted", "Inbox", "Outbox", "Sent", "Other"};
00165 //      static const char *FolderStatusString[] = { "Orphan", "Unfiled", "Filed" };
00166         
00167         os << "Folder Records\n\n";
00168         os << "Folder Name: " << FolderName << "\n";
00169         os << "Folder Type: ";
00170         if( FolderType < FolderDraft )
00171                 os << FolderTypeString[FolderType] << "\n";
00172         else if( FolderType == FolderDraft )
00173                 os << "Draft\n";
00174         else
00175                 os << "Unknown (" << std::hex << FolderType << ")\n";
00176         os << "Folder Number: " << std::dec << FolderNumber << "\n";
00177         os << "Folder Level: " << std::dec << FolderLevel << "\n";
00178         os << "\n";
00179         os << Unknowns;
00180         os << "\n\n";
00181 }
00182 
00183 } // namespace Barry
00184 

Generated on Mon Jan 12 10:51:13 2009 for Barry by  doxygen 1.5.7.1