Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

DBTemplate.cpp

Go to the documentation of this file.
00001 /*
00002  * DBTemplate.cpp
00003  *
00004  * Part of Fly! Legacy project
00005  *
00006  * Copyright 2003 Chris Wallace
00007  *
00008  * Fly! Legacy is free software; you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation; either version 2 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  * Fly! Legacy is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  *   along with Fly! Legacy; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00033 #include "../Include/Globals.h"
00034 #include "../Include/Database.h"
00035 #include "../Include/Utility.h"
00036 #include "../Include/Pod.h"
00037 
00038 using namespace std;
00039 
00040 
00041 CDatabaseTemplateItem::CDatabaseTemplateItem (void)
00042 {
00043   id = 0;
00044   start = length = 0;
00045   type = 0;
00046   memset (name, 0, 256);
00047 }
00048 
00049 
00050 CDatabaseTemplate::~CDatabaseTemplate (void)
00051 {
00052   vector<CDatabaseTemplateItem*>::iterator i;
00053   for (i=item.begin(); i!=item.end(); i++) {
00054     delete *i;
00055   }
00056 }
00057 
00058 //
00059 // Loader which takes an open PODFILE* reference to the DBT file
00060 //
00061 void CDatabaseTemplate::Load(PODFILE* f)
00062 {
00063   int i, j;
00064 
00065   // Buffer for file input
00066   char buffer[256];
00067 
00068   // Ensure file is rewound
00069   prewind (f);
00070 
00071   // Read number of fields
00072   int nItems;
00073   pgets (buffer, 256, f);
00074   if (sscanf (buffer, "%d", &nItems) != 1) {
00075     gtfo ("CDatabaseTemplate::Load : Cannot read nItems");
00076   }
00077 
00078   // Allocate storage for template items
00079   item.reserve(nItems);
00080  
00081   // Parse each field
00082   for (i=0; i<nItems; i++) {
00083     CDatabaseTemplateItem *ti = new CDatabaseTemplateItem;
00084 
00085     pgets (buffer, 256, f);
00086 
00087     // Parse input line into individual fields
00088     char sId[8], cType, sLeft[256], sRight[256];
00089     memset (sId, 0, 8);
00090     unsigned long start, length;
00091 
00092     // Split input line at point of final comma separator
00093     char *sSplit = strrchr (buffer, ',');
00094     int nLeft = sSplit - buffer;
00095     strncpy (sLeft, buffer, nLeft);
00096     strcpy (sRight, sSplit+1);
00097 
00098     // Parse left-hand portion of input line into data fields
00099     if (sscanf (sLeft, "%4s,%lu,%lu,%c", sId, &start, &length, &cType) != 4) {
00100       gtfo ("CDatabaseTemplate::Load : Cannot read item %d", i);
00101     } else {
00102       // Record parsed successfully
00103       ti->id = StringToTag (sId);
00104       ti->start = start;
00105       ti->length = length;
00106       ti->type = cType;
00107     }
00108 
00109     // Copy right-hand portion of input line as field description, trim trailing whitespace
00110     for (j=strlen(sRight)-1; j>=0; j--) {
00111       if ((sRight[j] == '\n') ||
00112         (sRight[j] == ' ')  ||
00113         (sRight[j] == 13) ||
00114         (sRight[j] == '\t'))
00115       {
00116         // Replace trailing whitespace with null
00117         sRight[j] = '\0';
00118       } else {
00119         // Non-whitespace found; exit loop
00120         break;
00121       }
00122     }
00123     strcpy (ti->name, sRight);
00124 
00125     // Add the new template item to the vector
00126     item.push_back (ti);
00127   }
00128 }
00129 
00130 
00131 //
00132 // Constructor which accepts a const char* filename of the DBT file
00133 //
00134 void CDatabaseTemplate::Load (const char* dbtFilename)
00135 {
00136   char fullFilename[64];
00137   strcpy (fullFilename, "Database\\");
00138   strcat (fullFilename, dbtFilename);
00139   PODFILE *p = popen (&globals->pfs, fullFilename);
00140 
00141   if (p) {
00142     Load (p);
00143     pclose (p);
00144   }
00145 }
00146 
00147 
00148 //
00149 // Data access function to get the number of template items
00150 //
00151 int CDatabaseTemplate::GetNumItems ()
00152 {
00153   return item.size();
00154 }
00155 
00156 
00157 //
00158 // Data access function to get the specified template item details
00159 //
00160 CDatabaseTemplateItem *CDatabaseTemplate::GetItem (int i)
00161 {
00162   CDatabaseTemplateItem *rc = NULL;
00163 
00164   if (i < GetNumItems()) {
00165     rc = item[i];
00166   }
00167 
00168   return rc;
00169 }
00170 
00171 void CDatabaseTemplate::Dump (FILE *f)
00172 {
00173   vector<CDatabaseTemplateItem*>::iterator i;
00174   for (i=item.begin(); i!=item.end(); i++) {
00175     fprintf (f, "0x%08X,%d,%d,%c,%s\n",
00176       (*i)->id,
00177       (*i)->start,
00178       (*i)->length,
00179       (*i)->type,
00180       (*i)->name);
00181   }
00182 }
SourceForge.net Logo Documentation generated by doxygen