/************************** * * * * * * * * * * * * *************************** Copyright (c) 1999-2005 Ryan Bobko ryan@ostrich-emulators.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *************************** * * * * * * * * * * * * **************************/ #include "qhaccutils.h" #include "qhacc.h" #include #include #include int Utils::QD=2; /* debug levels are: * * 0: no debugging nor errors * 1: fatal errors only * 2: errors that affect operation * 3: errors requiring default values to be used * 4: print major debugging information * 5: print minor debugging information * 6: for the curiosity cat * */ const int Utils::PRINT =0; const int Utils::ERRFATAL =1; const int Utils::ERROPER =2; const int Utils::ERRDEFAULT=3; const int Utils::DBGMAJOR =4; const int Utils::DBGMINOR =5; const int Utils::CURIOSITY =6; const int Utils::COLS[]={ QC::PCOLS, QC::LCOLS, QC::ACOLS, QC::TCOLS, QC::SCOLS, QC::NCOLS, QC::JCOLS, QC::XCOLS }; const char ** Utils::NAMES[]={ QC::PCOLNAMES, QC::LCOLNAMES, QC::ACOLNAMES, QC::TCOLNAMES, QC::SCOLNAMES, QC::NCOLNAMES, QC::JCOLNAMES, QC::XCOLNAMES }; const ColType * Utils::TYPES[]={ QC::PCOLTYPES, QC::LCOLTYPES, QC::ACOLTYPES, QC::TCOLTYPES, QC::SCOLTYPES, QC::NCOLTYPES, QC::JCOLTYPES, QC::XCOLTYPES }; const int Utils::PKS[]={ QC::PPREF, QC::LID, QC::AID, QC::TID, QC::SID, QC::NID, QC::JID, QC::XSID }; Utils * Utils::obj=0; Utils * Utils::get(){ if( obj==0 ) obj=new Utils(); return obj; } Utils::~Utils(){ if( obj ) delete obj; } bool Utils::debug( int lvl, std::ostream *& str ){ str=&cout; return ( lvl<=QD ); } bool Utils::error( int lvl, std::ostream *& str ){ str=&cerr; return ( lvl<=QD ); } void Utils::setDebug( int i ){ QD=i; } int Utils::getDebug(){ return QD; } void Utils::parser( const QString& str, const QString& sep, int start, QString * tokens, int numT ){ // parse a string, starting at start, into numT tokens // the return array must be big enough to hold the // tokens already /* int finder=0; for( int j=0; j-1 && idx=0; if( ret ){ if( interest ) *interest=regexp.cap( QC::LOANINT ).toFloat(); if( numpays ) *numpays=regexp.cap( QC::LOANNPR ).toInt(); if( payment ) *payment=regexp.cap( QC::LOANPMT ); } return ret; } bool Utils::isMarket( const Split& split, QString * shares, QString * price ){ QRegExp regexp( QC::MARKETREGEXP ); bool ret=regexp.search( split[QC::SMETA].gets() )>=0; if( ret ){ if( shares ) *shares=regexp.cap( QC::MARKETSHARES ); if( price ) *price=regexp.cap( QC::MARKETPRICE ); } return ret; } bool Utils::isMarket( const QString& val, QString& shrs, QString& price ){ QRegExp regexp( QC::MARKETREGEXP ); bool ret=regexp.search( val )>=0; if( ret ){ shrs=regexp.cap( QC::MARKETSHARES ); price=regexp.cap( QC::MARKETPRICE ); } return ret; } /**** the money converter ****/ const QString MonCon::ecurs=QHacc::CURRENCYSEPARATOR; MonCon::MonCon( QHacc * e ){ engine=e; factor=engine->getFP( "ALTCURRENCYFACTOR" ); if( factor==0 ) factor=1; asymb=engine->getSP( "ALTCSYMBOL" ); csymb=engine->getSP( "CSYMBOL" ); curs=engine->getSP( "CURRENCYSEPARATOR" ); inAlt=engine->getBP( "USEALTCURRENCY" ); figurePrefs(); } MonCon::~MonCon(){} void MonCon::decoder( ConvertFlags from, ConvertFlags to, bool& fromenginesep, bool& fromengineval, bool& toenginesep, bool& toengineval ) const{ // a couple notes... // this function seems a bit more complicated than necessary // because the from and to flags might not contain a value // that has both a Sep and Val value. We need to take this // into account when figuring out what conversions are // necessary. If one of the flags doesn't have a value, use // the value from the other flag (i.e., no conversion will be // needed). If neither flags has the proper value, we still // don't need a conversion, so set the booleans appropriately // set the default return results fromenginesep=fromengineval=toenginesep=toengineval=true; // first, figure out value conversions if( from & HasVal ){ if( from & PrefVal ) fromengineval=prefUseEVal; else fromengineval=( from & EngVal ); } if( to & HasVal ){ if( to & PrefVal ) toengineval=prefUseEVal; else toengineval=( to * EngVal ); } else toengineval=fromengineval; if( !from & HasVal ) fromengineval=toengineval; // next, figure out separator conversions if( from & HasSep ){ if( from & PrefSep ) fromenginesep=prefUseESep; else fromenginesep=( from & EngSep ); } if( to & HasSep ){ if( to & PrefSep ) toenginesep=prefUseESep; else toenginesep=( to * EngSep ); } else toenginesep=fromenginesep; if( !from & HasSep ) fromenginesep=toenginesep; } QString MonCon::replace( QString str, const QString& find, const QString& repl ){ // need a separate function for this so we can handle d/f versions of Qt #if QT_VERSION<320 int pos=str.find( find ); if( pos==-1 ) return str; int ol=find.length(); QString s( str ); return s.replace( pos, ol, repl ); #else return str.replace( find, repl ); #endif } QString MonCon::convert( const QString& currentval, ConvertFlags from, ConvertFlags to ) const { // simple case: no changes if( from==to ) return currentval; //cout<<"convert s: "<