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

DBUtilities.cpp

Go to the documentation of this file.
00001 /*
00002  * DBUtilities.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  */
00023 
00032 #include "../Include/Database.h"
00033 #include "../Include/Pod.h"
00034 #include "../Include/Endian.h"
00035 
00036 //
00037 // CDatabaseField
00038 //
00039 CDatabaseField::CDatabaseField (void)
00040 {
00041   type = 0;
00042   length = 0;
00043   memset (data.charData, 0, 256);
00044 }
00045 /*
00046 CDatabaseField::~CDatabaseField (void)
00047 {
00048   switch (type) {
00049   case 'C':
00050     if (data.pCharData != NULL) {
00051       delete[] data.pCharData;
00052     }
00053     break;
00054 
00055   case 'D':
00056     if (data.pDoubleData != NULL) {
00057       delete data.pDoubleData;
00058     }
00059     break;
00060 
00061   case 'I':
00062     if (data.pIntData != NULL) {
00063       delete data.pIntData;
00064     }
00065     break;
00066 
00067   default:
00068     ;
00069   }
00070 }
00071 */
00072 char CDatabaseField::GetType (void)
00073 {
00074   return type;
00075 }
00076 
00077 unsigned long CDatabaseField::GetLength (void)
00078 {
00079   return length;
00080 }
00081 
00082 void CDatabaseField::GetString (char *s)
00083 {
00084   if (type == 'C') {
00085 //    if (data.pCharData != NULL) {
00086       strcpy (s, data.charData);
00087 //    }
00088   }
00089 }
00090 
00091 double CDatabaseField::GetDouble (void)
00092 {
00093   double rc = 0;
00094   if (type == 'D') {
00095 //    if (data.pDoubleData != NULL) {
00096       rc = data.doubleData;
00097 //    }
00098   }
00099   return rc;
00100 }
00101 
00102 long CDatabaseField::GetInt (void)
00103 {
00104   long rc = 0;
00105   if (type == 'I') {
00106 //    if (data.pIntData != NULL) {
00107       rc = data.intData;
00108 //    }
00109   }
00110   return rc;
00111 }
00112 
00113 void CDatabaseField::Format (char *s)
00114 {
00115   switch (type) {
00116   case 'C':
00117     {
00118       // Copy string to supplied buffer
00119       strcpy (s, data.charData);
00120 
00121       // Pad with spaces to data length
00122       for (unsigned int i=strlen(data.charData); i < length; i++) {
00123         s[i] = ' ';
00124       }
00125     
00126       // Null terminator
00127       s[length] = '\0';
00128     }
00129     break;
00130 
00131   case 'D':
00132     {
00133       // Double-precision float
00134       sprintf (s, "%g", data.doubleData);
00135     }
00136     break;
00137 
00138   case 'I':
00139     {
00140       // Integer (unsigned long)
00141       sprintf (s, "%lu", data.intData);
00142     }
00143     break;
00144   }
00145 }
00146 
00147 //
00148 //  Read unsigned long from binary file
00149 //
00150 void ReadULong (PODFILE *f, unsigned long *pData)
00151 {
00152   pread (pData, sizeof(unsigned long), 1, f);
00153   *pData = LittleEndian(*pData);
00154 }
00155 
00156 
00157 //
00158 //  Read signed long from binary file
00159 //
00160 void ReadLong (PODFILE *f, long *pData)
00161 {
00162   pread (pData, sizeof(long), 1, f);
00163   *pData = LittleEndian(*pData);
00164 }
00165 
00166 
00167 //
00168 //  Read unsigned short from (little-endian) database binary file
00169 //  Automatically adjusts for big-endian processors
00170 //
00171 void ReadUShort (PODFILE *f, unsigned short *pData)
00172 {
00173   pread (pData, sizeof(unsigned short), 1, f);
00174   *pData = LittleEndian(*pData);
00175 }
00176 
00177 
00178 //
00179 //  Read double float from (little-endian) database binary file
00180 //  Automatically adjusts for big-endian processors
00181 //
00182 void ReadDouble (PODFILE *f, double *pData)
00183 {
00184   pread (pData, sizeof(double), 1, f);
00185   *pData = LittleEndian(*pData);
00186 }
00187 
00188 //
00189 //  Read float from (little-endian) database binary file
00190 //  Automatically adjusts for big-endian processors
00191 //
00192 void ReadFloat (PODFILE *f, float *pData)
00193 {
00194   pread (pData, sizeof(float), 1, f);
00195   *pData = LittleEndian(*pData);
00196 }
00197 
00198 
00199 //
00200 //  Read unsigned char from (little-endian) database binary file
00201 //
00202 void ReadUChar (PODFILE *f, unsigned char *pData)
00203 {
00204   pread (pData, sizeof(unsigned char), 1, f);
00205 }
00206 
00207 
00208 
00209 //
00210 // Returns the next tab-delimitted field in the passed-in string.
00211 // The string pointer is updated to point beyond the delimitting tab
00212 //
00213 char * TabNextField (char *s, char *szField)
00214 {
00215   char *pTab = strchr (s, '\t');
00216   if (pTab != NULL) {
00217     int nChars = pTab - s;
00218     strncpy (szField, s, nChars);
00219 
00220     // Adjust s to point beyond tab
00221     s = pTab + 1;
00222   }
00223   return s;
00224 }
00225 
00226 //
00227 // Skips the specified number of tab-delimitted fields
00228 //
00229 char * TabSkipFields (char *s, int nFields)
00230 {
00231   int i;
00232   for (i=0; i<nFields; i++) {
00233     char *pTab = strchr (s, '\t');
00234     if (pTab != NULL) {
00235       // Adjust s to point beyond tab
00236       s = pTab + 1;
00237     }
00238   }
00239   return s;
00240 }
SourceForge.net Logo Documentation generated by doxygen