123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894 |
- //---------------------------------------------------------------------------
- //
- // csvfile.cpp - This file contains the class declaration for the CSV Files
- //
- // The CSV file is an Excel csv style file.
- //
- // MechCommander 2
- //
- //---------------------------------------------------------------------------//
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- //===========================================================================//
- //---------------------------------------------------------------------------
- // Include files
- #ifndef CSVFILE_H
- #include "csvfile.h"
- #endif
- #ifndef HEAP_H
- #include "heap.H"
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #include <ctype.h>
- #ifndef _MBCS
- #include <gameos.hpp>
- #else
- #include <assert.h>
- #define gosASSERT assert
- #define gos_Malloc malloc
- #define gos_Free free
- #endif
- //---------------------------------------------------------------------------
- // Static Globals
- #undef isspace //Macro Chokes under Intel Compiler!!
- //---------------------------------------------------------------------------
- // class CSVIniFile
- CSVFile::CSVFile (void) : File()
- {
- totalRows = totalCols = 0L;
- }
- //---------------------------------------------------------------------------
- CSVFile::~CSVFile (void)
- {
- close();
- }
- //---------------------------------------------------------------------------
- long CSVFile::countRows (void)
- {
- long count = 0;
- long oldPosition = logicalPosition;
-
- seek(0); //Start at the top.
- char tmp[2048];
-
- readLine((MemoryPtr)tmp,2047);
- while (!eof())
- {
- count++;
- readLine((MemoryPtr)tmp,2047);
- }
-
- //----------------------------------
- // Move back to where we were.
- seek(oldPosition);
-
- return(count);
- }
- //---------------------------------------------------------------------------
- long CSVFile::countCols (void)
- {
- long count = 0, maxCols = 0;
- long oldPosition = logicalPosition;
-
- seek(0); //Start at the top.
- char tmp[2048];
- char *currentChk = tmp;
-
- readLine((MemoryPtr)tmp,2047);
- currentChk = strstr(tmp,",");
- while (currentChk && (*currentChk != '\n') && (*currentChk != '\r'))
- {
- count++;
- currentChk++;
- currentChk = strstr(currentChk,",");
- }
-
- if (count > maxCols)
- maxCols = count;
-
- readLine((MemoryPtr)tmp,2047);
-
- //----------------------------------
- // Move back to where we were.
- seek(oldPosition);
-
- return(maxCols);
- }
- //---------------------------------------------------------------------------
- long CSVFile::getNextWord (char *&line, char *buffer, unsigned long bufLen)
- {
- //--------------------------------------------------
- // Check to see if we are at end of line
- if (*line == '\0')
- return(-1);
-
- //--------------------------------------------------
- // Check to see if the rest of the line is comments
- if (*line == '/')
- return(-1);
- if ( *line == ',' ) // empty column, move on
- return -1;
-
- //------------------------------------------
- // Find start of word from current location
- while ((*line != '\0') && ((*line == ' ') || (*line == '\t') || (*line == ',')))
- {
- line++;
- }
-
- //--------------------------------------------------
- // Check to see if we are at end of line
- if (*line == '\0')
- return(-1);
-
- //--------------------------------------------------
- // Check to see if the rest of the line is comments
- if (*line == '/')
- return(-1);
-
- //-------------------------------------------
- // Find length of word from current location
- char *startOfWord = line;
- unsigned long wordLength = 0;
- while ((*line != '\0') && ((*line != ',')))
- {
- line++;
- wordLength++;
- }
-
- if (wordLength > bufLen)
- return(-2);
-
- strncpy(buffer, startOfWord, wordLength);
- buffer[wordLength] = '\0';
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::afterOpen (void)
- {
- //-------------------------------------------------------
- // Check if we opened this with CREATE and write the
- // FITini Header and position to Write Start.
- if (fileMode == CREATE && parent == NULL)
- {
- STOP(("Cannot write CSV files at present."));
- }
- else
- {
- //------------------------------------------------------
- // Find out how many Rows and cols we have
- totalRows = countRows();
- totalCols = countCols();
- }
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- void CSVFile::atClose (void)
- {
- //------------------------------------------------------------
- // Check if we are in create mode and if so, write the footer
- if (fileMode == CREATE)
- {
- STOP(("Cannot write CSV files at present."));
- }
- totalRows = totalCols = 0;
- }
- //---------------------------------------------------------------------------
- float CSVFile::textToFloat (char *num)
- {
- float result = atof(num);
- return(result);
- }
- //---------------------------------------------------------------------------
- long CSVFile::textToLong (char *num)
- {
- long result = 0;
-
- //------------------------------------
- // Check if Hex Number
- char *hexOffset = strstr(num,"0x");
- if (hexOffset == NULL)
- {
- result = atol(num);
- }
- else
- {
- hexOffset += 2;
- long numDigits = strlen(hexOffset)-1;
- for (int i=0; i<=numDigits; i++)
- {
- if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
- {
- hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
- break;
- }
- }
- numDigits = strlen(hexOffset)-1;
- long power = 0;
- for (long count = numDigits;count >= 0;count--,power++)
- {
- unsigned char currentDigit = toupper(hexOffset[count]);
-
- if (currentDigit >= 'A' && currentDigit <= 'F')
- {
- result += (currentDigit - 'A' + 10)<<(4*power);
- }
- else if (currentDigit >= '0' && currentDigit <= '9')
- {
- result += (currentDigit - '0')<<(4*power);
- }
- else
- {
- //---------------------------------------------------------
- // There is a digit in here I don't understand. Return 0.
- result = 0;
- break;
- }
- }
- }
-
- return(result);
- }
- //---------------------------------------------------------------------------
- short CSVFile::textToShort (char *num)
- {
- short result = 0;
-
- //------------------------------------
- // Check if Hex Number
- char *hexOffset = strstr(num,"0x");
- if (hexOffset == NULL)
- {
- result = atol(num);
- }
- else
- {
- hexOffset += 2;
- long numDigits = strlen(hexOffset)-1;
- for (int i=0; i<=numDigits; i++)
- {
- if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
- {
- hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
- break;
- }
- }
- numDigits = strlen(hexOffset)-1;
- long power = 0;
- for (long count = numDigits;count >= 0;count--,power++)
- {
- unsigned char currentDigit = toupper(hexOffset[count]);
-
- if (currentDigit >= 'A' && currentDigit <= 'F')
- {
- result += (currentDigit - 'A' + 10)<<(4*power);
- }
- else if (currentDigit >= '0' && currentDigit <= '9')
- {
- result += (currentDigit - '0')<<(4*power);
- }
- else
- {
- //---------------------------------------------------------
- // There is a digit in here I don't understand. Return 0.
- result = 0;
- break;
- }
- }
- }
-
- return(result);
- }
- //---------------------------------------------------------------------------
- char CSVFile::textToChar (char *num)
- {
- char result = 0;
-
- //------------------------------------
- // Check if Hex Number
- char *hexOffset = strstr(num,"0x");
- if (hexOffset == NULL)
- {
- result = atol(num);
- }
- else
- {
- hexOffset += 2;
- long numDigits = strlen(hexOffset)-1;
- for (int i=0; i<=numDigits; i++)
- {
- if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
- {
- hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
- break;
- }
- }
- numDigits = strlen(hexOffset)-1;
- long power = 0;
- for (long count = numDigits;count >= 0;count--,power++)
- {
- unsigned char currentDigit = toupper(hexOffset[count]);
-
- if (currentDigit >= 'A' && currentDigit <= 'F')
- {
- result += (currentDigit - 'A' + 10)<<(4*power);
- }
- else if (currentDigit >= '0' && currentDigit <= '9')
- {
- result += (currentDigit - '0')<<(4*power);
- }
- else
- {
- //---------------------------------------------------------
- // There is a digit in here I don't understand. Return 0.
- result = 0;
- break;
- }
- }
- }
-
- return(result);
- }
- //---------------------------------------------------------------------------
- unsigned long CSVFile::textToULong (char *num)
- {
- unsigned long result = 0;
-
- //------------------------------------
- // Check if Hex Number
- char *hexOffset = strstr(num,"0x");
- if (hexOffset == NULL)
- {
- result = atol(num);
- }
- else
- {
- hexOffset += 2;
- long numDigits = strlen(hexOffset)-1;
- for (int i=0; i<=numDigits; i++)
- {
- if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
- {
- hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
- break;
- }
- }
- numDigits = strlen(hexOffset)-1;
- long power = 0;
- for (long count = numDigits;count >= 0;count--,power++)
- {
- unsigned char currentDigit = toupper(hexOffset[count]);
-
- if (currentDigit >= 'A' && currentDigit <= 'F')
- {
- result += (currentDigit - 'A' + 10)<<(4*power);
- }
- else if (currentDigit >= '0' && currentDigit <= '9')
- {
- result += (currentDigit - '0')<<(4*power);
- }
- else
- {
- //---------------------------------------------------------
- // There is a digit in here I don't understand. Return 0.
- result = 0;
- break;
- }
- }
- }
-
- return(result);
- }
- //---------------------------------------------------------------------------
- unsigned short CSVFile::textToUShort (char *num)
- {
- unsigned short result = 0;
-
- //------------------------------------
- // Check if Hex Number
- char *hexOffset = strstr(num,"0x");
- if (hexOffset == NULL)
- {
- result = atol(num);
- }
- else
- {
- hexOffset += 2;
- long numDigits = strlen(hexOffset)-1;
- for (int i=0; i<=numDigits; i++)
- {
- if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
- {
- hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
- break;
- }
- }
- numDigits = strlen(hexOffset)-1;
- long power = 0;
- for (long count = numDigits;count >= 0;count--,power++)
- {
- unsigned char currentDigit = toupper(hexOffset[count]);
-
- if (currentDigit >= 'A' && currentDigit <= 'F')
- {
- result += (currentDigit - 'A' + 10)<<(4*power);
- }
- else if (currentDigit >= '0' && currentDigit <= '9')
- {
- result += (currentDigit - '0')<<(4*power);
- }
- else
- {
- //---------------------------------------------------------
- // There is a digit in here I don't understand. Return 0.
- result = 0;
- break;
- }
- }
- }
-
- return(result);
- }
- //---------------------------------------------------------------------------
- unsigned char CSVFile::textToUChar (char *num)
- {
- unsigned char result = 0;
-
- //------------------------------------
- // Check if Hex Number
- char *hexOffset = strstr(num,"0x");
- if (hexOffset == NULL)
- {
- result = atol(num);
- }
- else
- {
- hexOffset += 2;
- long numDigits = strlen(hexOffset)-1;
- for (int i=0; i<=numDigits; i++)
- {
- if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
- {
- hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
- break;
- }
- }
- numDigits = strlen(hexOffset)-1;
- long power = 0;
- for (long count = numDigits;count >= 0;count--,power++)
- {
- unsigned char currentDigit = toupper(hexOffset[count]);
-
- if (currentDigit >= 'A' && currentDigit <= 'F')
- {
- result += (currentDigit - 'A' + 10)<<(4*power);
- }
- else if (currentDigit >= '0' && currentDigit <= '9')
- {
- result += (currentDigit - '0')<<(4*power);
- }
- else
- {
- //---------------------------------------------------------
- // There is a digit in here I don't understand. Return 0.
- result = 0;
- break;
- }
- }
- }
-
- return(result);
- }
- //---------------------------------------------------------------------------
- bool CSVFile::booleanToLong (char *num)
- {
- char testChar = 0;
- while (num[testChar] && isspace(num[testChar]))
- testChar++;
- // 'N' == NO if you can believe that
- if ((toupper(num[testChar]) == 'F') || (toupper(num[testChar]) == '0') || (toupper(num[testChar]) == 'N') )
- return FALSE;
- else
- return(TRUE);
- }
- //---------------------------------------------------------------------------
- long CSVFile::floatToText (char *result, float num, unsigned long bufLen)
- {
- char temp[250];
- sprintf(temp,"%f4",num);
- unsigned long numLength = strlen(temp);
- if (numLength >= bufLen)
- return(-2);
- strncpy(result,temp,numLength);
- result[numLength] = '\0';
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::longToTextDec (char *result, long num, unsigned long bufLen)
- {
- char temp[250];
- sprintf(temp,"%d",num);
- unsigned long numLength = strlen(temp);
- if (numLength >= bufLen)
- return(-2);
- strncpy(result,temp,numLength);
- result[numLength] = '\0';
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::longToTextHex (char *result, long num, unsigned long bufLen)
- {
- char temp[250];
- sprintf(temp,"0x%x",num);
- unsigned long numLength = strlen(temp);
- if (numLength >= bufLen)
- return(-2);
- strncpy(result,temp,numLength);
- result[numLength] = '\0';
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::shortToTextDec (char *result, short num, unsigned long bufLen)
- {
- char temp[250];
- sprintf(temp,"%d",num);
- unsigned long numLength = strlen(temp);
- if (numLength >= bufLen)
- return(-2);
- strncpy(result,temp,numLength);
- result[numLength] = '\0';
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::shortToTextHex (char *result, short num, unsigned long bufLen)
- {
- char temp[250];
- sprintf(temp,"0x%x",num);
- unsigned long numLength = strlen(temp);
- if (numLength >= bufLen)
- return(-2);
- strncpy(result,temp,numLength);
- result[numLength] = '\0';
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::byteToTextDec (char *result, byte num, unsigned long bufLen)
- {
- char temp[250];
- sprintf(temp,"%d",num);
- unsigned long numLength = strlen(temp);
- if (numLength >= bufLen)
- return(-2);
- strncpy(result,temp,numLength);
- result[numLength] = '\0';
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::byteToTextHex (char *result, byte num, unsigned long bufLen)
- {
- char temp[250];
- sprintf(temp,"0x%x",num);
- unsigned long numLength = strlen(temp);
- if (numLength >= bufLen)
- return(-2);
- strncpy(result,temp,numLength);
- result[numLength] = '\0';
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::open (const char* fName, FileMode _mode, long numChild)
- {
- long result = File::open(fName,_mode,numChild);
- if (result != NO_ERR)
- return(result);
-
- seek(0);
- result = afterOpen();
-
- return(result);
- }
- //---------------------------------------------------------------------------
- long CSVFile::open (FilePtr _parent, unsigned long fileSize, long numChild)
- {
- numChild = -1; //Force all parented CSVs to load from RAM.
- long result = File::open(_parent,fileSize,numChild);
- if (result != NO_ERR)
- return(result);
-
- result = afterOpen();
-
- return(result);
- }
- //---------------------------------------------------------------------------
- long CSVFile::create (char* fName)
- {
- fName;
- //STOP(("CSV file write is not supported %s",fName));
- return(-1);
- }
- //---------------------------------------------------------------------------
- void CSVFile::close (void)
- {
- if (isOpen())
- {
- atClose();
- File::close();
- }
- }
- //---------------------------------------------------------------------------
- long CSVFile::seekRowCol (DWORD row, DWORD col)
- {
- if ((row > totalRows) || (col > totalCols))
- return -1;
-
- DWORD rowCount = 0;
-
- seek(0); //Start at the top.
- char tmp[2048];
-
- do
- {
- rowCount++;
- readLine((MemoryPtr)tmp,2047);
- } while (rowCount != row);
-
- char *currentChk = tmp;
- if (col)
- {
- DWORD colCount = 1;
-
- while (currentChk && (colCount != col))
- {
- colCount++;
- currentChk = strstr(currentChk,",");
- if ( currentChk ) // if we increment to one, bad things happen
- currentChk++;
- }
- }
-
- //---------------------------------------------------
- // We are now pointing at the row and col specified.
- if (currentChk)
- {
- char *data = dataBuffer;
- return getNextWord(currentChk,data,2047);
- }
- else
- {
- return -1;
- // CAN'T do this, for some reason excel writes out empty rows.
- //STOP(("Unable to parse CSV %s, ROW %d, COL %D, ERROR: NULL",getFilename(),row,col));
- }
-
- //return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::readFloat (DWORD row, DWORD col, float &value)
- {
- long result = seekRowCol(row,col);
- if (result == NO_ERR)
- {
- value = textToFloat(dataBuffer);
- }
- else
- value = 0.0f;
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::readLong (DWORD row, DWORD col, long &value)
- {
- long result = seekRowCol(row,col);
- if (result == NO_ERR)
- {
- value = textToLong(dataBuffer);
- }
- else
- value = 0.0f;
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::readBoolean (DWORD row, DWORD col, bool &value)
- {
- long result = seekRowCol(row,col);
- if (result == NO_ERR)
- {
- value = booleanToLong(dataBuffer);
- }
- else
- value = 0;
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::readShort (DWORD row, DWORD col, short &value)
- {
- long result = seekRowCol(row,col);
- if (result == NO_ERR)
- {
- value = textToShort(dataBuffer);
- }
- else
- value = 0.0f;
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::readChar (DWORD row, DWORD col, char &value)
- {
- long result = seekRowCol(row,col);
- if (result == NO_ERR)
- {
- value = textToChar(dataBuffer);
- }
- else
- value = 0.0f;
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::readULong (DWORD row, DWORD col, unsigned long &value)
- {
- long result = seekRowCol(row,col);
- if (result == NO_ERR)
- {
- value = textToULong(dataBuffer);
- }
- else
- value = 0.0f;
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::readUShort (DWORD row, DWORD col, unsigned short &value)
- {
- long result = seekRowCol(row,col);
- if (result == NO_ERR)
- {
- value = textToUShort(dataBuffer);
- }
- else
- value = 0.0f;
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::readUChar (DWORD row, DWORD col, unsigned char &value)
- {
- long result = seekRowCol(row,col);
- if (result == NO_ERR)
- {
- value = textToUChar(dataBuffer);
- }
- else
- value = 0.0f;
-
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::copyString (char *dest, char *src, unsigned long bufLen)
- {
- unsigned long offset = 0;
- //---------------------------------------
- // Copy each character until close quote
- while (*src != '"' && *src != '\0' && offset < bufLen)
- {
- dest[offset] = *src;
- src++;
- offset++;
- }
- //----------------------------------------------------
- // If this string is longer than buffer, let em know.
- if (offset == bufLen)
- {
- return(-2);
- }
- //---------------------------------
- // otherwise, NULL term and return
- dest[offset] = '\0';
- return(NO_ERR);
- }
- //---------------------------------------------------------------------------
- long CSVFile::readString (DWORD row, DWORD col, char *result, unsigned long bufferSize)
- {
- long res = seekRowCol(row,col);
- if (res == NO_ERR)
- {
- long errorCode = copyString(result,dataBuffer,bufferSize);
- if (errorCode != NO_ERR)
- return(errorCode);
- }
- else
- return 1;
- return(0); // gotta return some kind of error!
- }
- //---------------------------------------------------------------------------
- //
- // Edit log
- //
- //---------------------------------------------------------------------------
|