From: Mapi B. <ma...@us...> - 2009-06-22 21:58:24
|
Update of /cvsroot/easycalc/PPCport/compat In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv23401 Added Files: DataManager.cpp DataManager.h Lang.cpp Lang.h PalmOS.h dbutil.cpp Log Message: 2nd upload, getting closer --- NEW FILE: Lang.cpp --- /***************************************************************************** * EasyCalc -- a scientific calculator * Copyright (C) 2008 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, version 2, * as published by the Free Software Foundation. * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *****************************************************************************/ /* Lang.cpp : Lang and LibLang objects for handling language translations. *****************************************************************************/ #include "StdAfx.h" #include "compat/Lang.h" #include <locale.h> #ifdef UNICODE #define _tsetlocale _wsetlocale #else #define _tsetlocale setlocale #define mbstowcs(a,b,c) ((a == NULL) ? (strlen(b)) : ((strcpy(a,b) == NULL) ? -1 : c)) #endif #include <stdlib.h> /*------------------------------------------------------------------------------- - The Lang object Constructor and Destructor. - -------------------------------------------------------------------------------*/ Lang::Lang(const TCHAR *name) { localeInfo = NULL; langName.assign(name); // Try to set the locale with the provided string first. if ((localeInfo = _tsetlocale(LC_ALL, name)) != NULL) { } else { // Not recognized by windows. // Map using the Palm EasyCalc PilRC names if (_tcscmp(name, _T("cs")) == 0) { codePage = 1250; localeInfo = _tsetlocale(LC_ALL, _T("Czech")); } else if (_tcscmp(name, _T("de")) == 0) { codePage = 1252; localeInfo = _tsetlocale(LC_ALL, _T("German")); } else if (_tcscmp(name, _T("en")) == 0) { codePage = 1252; localeInfo = _tsetlocale(LC_ALL, _T("English")); } else if (_tcscmp(name, _T("fr")) == 0) { codePage = 1252; localeInfo = _tsetlocale(LC_ALL, _T("French")); } else if (_tcscmp(name, _T("it")) == 0) { codePage = 1252; localeInfo = _tsetlocale(LC_ALL, _T("Italian")); } else if (_tcscmp(name, _T("ja")) == 0) { codePage = 932; localeInfo = _tsetlocale(LC_ALL, _T("Japanese_Japan.932")); // This may not load correctly on western systems with Japan non installed, // so try a bypass .., which should set at least the code page correcly. if (localeInfo == NULL) { localeInfo = _tsetlocale(LC_ALL, _T(".932")); } } else if (_tcscmp(name, _T("pt")) == 0) { codePage = 1252; localeInfo = _tsetlocale(LC_ALL, _T("Portuguese")); } else if (_tcscmp(name, _T("ru_koi8")) == 0) { // Note that koi8-u, for Ukrainia, is 21866 codePage = 20866; localeInfo = _tsetlocale(LC_ALL, _T("Russian_Russia.20866")); } else if (_tcscmp(name, _T("ru_win")) == 0) { codePage = 1251; localeInfo = _tsetlocale(LC_ALL, _T("Russian")); } else if (_tcscmp(name, _T("sp")) == 0) { codePage = 1252; localeInfo = _tsetlocale(LC_ALL, _T("Spanish")); } if (localeInfo == NULL) { // Cannot recognize the language, use a character set by default // for converting from what is read to wide characters. codePage = 1252; localeInfo = _tsetlocale(LC_ALL, _T("English")); } } // Since the string returned by setlocale can be overwritten, save a copy localeInfo = _tcsdup(localeInfo); } Lang::~Lang(void) { langName.clear(); translation.clear(); } /*------------------------------------------------------------------------------- - Lang object methods. - -------------------------------------------------------------------------------*/ /******************************************************************************** * Insert a translation pair. * ********************************************************************************/ bool Lang::insert(const TCHAR *key, const TCHAR *value) { String s1 (key); String s2 (value); std::pair<stdext::hash_map<String,String>::iterator, bool> res; res = translation.insert(Pair(s1, s2)); return (res.second); } /******************************************************************************** * Get the text of a translation entry. * ********************************************************************************/ const TCHAR *Lang::get(TCHAR *key) { String s1 (key); stdext::hash_map<String,String>::const_iterator res; res = translation.find(s1); if (res == translation.end()) return (NULL); return (res->second.c_str()); } /*------------------------------------------------------------------------------- - The LibLang object Constructor and Destructor. - -------------------------------------------------------------------------------*/ #define SPACECHARS " \t" #define EOLCHARS "\r\n" #define ESCAPECHARS "\\" #define STRINGDELIMITERS "\"" #define ST1COMMENTCHARS "/" #define ST2COMMENTCHARS "*" #define EQUALCHARS "=" #define TRANSLATION 1 #define LANGNAME 2 #define BEGIN 3 #define STRING1_OR_END 4 #define EQUAL 6 #define STRING2 7 #define LEX_IN_NONE 0 #define LEX_IN_WORD 1 #define LEX_IN_STRING 2 #define LEX_IN_ESCAPE 3 #define LEX_END_STRING 4 #define LEX_BETWEEN_STRINGS 5 #define LEX_IN_STARTCOMMENT 6 #define LEX_IN_COMMENT 7 #define LEX_IN_ENDCOMMENT 8 #define TOK_NONE 0 #define TOK_WORD 1 #define TOK_STRING 2 #define TOK_EQUAL 3 LibLang::LibLang(FILE *langFile) { wcstr = NULL; allocLen = nbLang = 0; firstCalled = false; // Parse the lang.rcp file until EOF. // Simple parser, line oriented. // As per http://www.dtek.chalmers.se/groups/pilot/doc/pilrc.htm // TRANSLATION <Language.s> //BEGIN // <STRINGTRANSLATIONS> //END //Where <STRINGTRANSLATIONS> is one or more of: // <Original.s> = <Translated.s> //Note: a '\' at end of line means "line continued on next line". // <...> means a string between "". // /* ... */ delimit comments, which can be on multiple lines. // No nested comment support. int c, clast; int nextToken = TRANSLATION; // Start looking for TRANSLATION int token, lexState; bool complete; std::string s; std::string s1; Lang *lang = NULL; while (!feof(langFile)) { // Get a new token from the lexical "level". s.clear(); lexState = LEX_IN_NONE; // Start the lexical level in no mode token = TOK_NONE; // No lexical token recognized yet complete = false; while (!complete && (c = fgetc(langFile)) != EOF) { switch (lexState) { case LEX_IN_NONE: if ((strchr(SPACECHARS, c) != NULL) || (strchr(EOLCHARS, c) != NULL)) break; else if (strchr(STRINGDELIMITERS, c) != NULL) { // We're getting the start of a string token = TOK_STRING; lexState = LEX_IN_STRING; } else if (strchr(ST1COMMENTCHARS, c) != NULL) { // We're getting the start of a comment clast = c; lexState = LEX_IN_STARTCOMMENT; } else if (strchr(EQUALCHARS, c) != NULL) { token = TOK_EQUAL; s.append(1, c); complete = true; } else { // Anything else is a word, by default token = TOK_WORD; s.append(1, c); lexState = LEX_IN_WORD; } break; case LEX_IN_WORD: if ((strchr(SPACECHARS, c) != NULL) || (strchr(EOLCHARS, c) != NULL)) { complete = true; } else if ((strchr(STRINGDELIMITERS, c) != NULL) || (strchr(ST1COMMENTCHARS, c) != NULL) || (strchr(EQUALCHARS, c) != NULL)) { ungetc (c, langFile); // Give back the character for the next // lexical round. complete = true; } else { // Anything else is part of the word s.append(1, c); } break; case LEX_IN_STRING: if (strchr(ESCAPECHARS, c) != NULL) { // Escape character, keep the next one, // whatever it is (except end of line) s.append(1, c); lexState = LEX_IN_ESCAPE; } else if (strchr(STRINGDELIMITERS, c) != NULL) { // We're getting the end of the string lexState = LEX_END_STRING; } else if (strchr(EOLCHARS, c) != NULL) { // End of line reached, stop the string there complete = true; } else { s.append(1, c); } break; case LEX_IN_ESCAPE: if (strchr(EOLCHARS, c) != NULL) { // End of line reached, stop the string there complete = true; } else { s.append(1, c); lexState = LEX_IN_STRING; } break; case LEX_END_STRING: if (strchr(ESCAPECHARS, c) != NULL) { // Escape character, meaning the string // goes on on next line. lexState = LEX_BETWEEN_STRINGS; } else if (strchr(SPACECHARS, c) == NULL) { // Consume spaces between '"' and '\' // Not space and not '\' ungetc (c, langFile); // Give back the character for the next // lexical round. complete = true; } break; case LEX_BETWEEN_STRINGS: if (strchr(STRINGDELIMITERS, c) != NULL) { // We're getting the start of the string continuation lexState = LEX_IN_STRING; } break; case LEX_IN_STARTCOMMENT: if (strchr(ST2COMMENTCHARS, c) != NULL) { lexState = LEX_IN_COMMENT; } else { // No start of comment, so go back to a word by default s.append(1, clast); s.append(1, c); lexState = LEX_IN_WORD; } break; case LEX_IN_COMMENT: if (strchr(ST2COMMENTCHARS, c) != NULL) { lexState = LEX_IN_ENDCOMMENT; // Possible end of comment } break; case LEX_IN_ENDCOMMENT: if (strchr(ST1COMMENTCHARS, c) != NULL) { lexState = LEX_IN_NONE; // Back to normal } else if (strchr(ST2COMMENTCHARS, c) == NULL) { lexState = LEX_IN_COMMENT; // Not '*', go back to comment mode } break; } } // The lexical level is done, analyze at "grammar" level if (token != TOK_NONE) switch (nextToken) { case TRANSLATION: if (token == TOK_WORD) { const char *str = s.c_str(); char *strup = _strdup(str); _strupr(strup); if (strcmp(strup, "TRANSLATION") == 0) { nextToken = LANGNAME; } free(strup); } break; case LANGNAME: if (token == TOK_STRING) { // We've got a new language translation nextToken = BEGIN; // Create or retrieve a language object const TCHAR *langName = convert(s.c_str()); lang = (Lang *) getLang (langName); if (lang == NULL) { // Create a new one lang = new Lang (langName); insertLang(lang); } } else nextToken = TRANSLATION; break; case BEGIN: if (token == TOK_WORD) { const char *str = s.c_str(); char *strup = _strdup(str); _strupr(strup); if (strcmp(strup, "BEGIN") == 0) { nextToken = STRING1_OR_END; } else nextToken = TRANSLATION; free(strup); } else nextToken = TRANSLATION; break; case STRING1_OR_END: if (token == TOK_WORD) { const char *str = s.c_str(); char *strup = _strdup(str); _strupr(strup); if (strcmp(strup, "END") == 0) { nextToken = TRANSLATION; } free(strup); } else if (token == TOK_STRING) { s1.assign(s); nextToken = EQUAL; } break; case EQUAL: if (token == TOK_EQUAL) { nextToken = STRING2; } else nextToken = STRING1_OR_END; break; case STRING2: if (token == TOK_STRING) { // We have a new entry for translation TCHAR *str1 = _tcsdup(convert(s1.c_str())); const TCHAR *str2 = convert(s.c_str()); lang->insert(str1, str2); free(str1); } nextToken = STRING1_OR_END; break; } } // All this has manipulated the locale several times ... // Set it to english by default. setLang(_T("en")); } LibLang::~LibLang(void) { libLang.clear(); } /*------------------------------------------------------------------------------- - LibLang object methods. - -------------------------------------------------------------------------------*/ /******************************************************************************** * Insert a new Lang object in library. * ********************************************************************************/ bool LibLang::insertLang(Lang *lang) { std::pair<stdext::hash_map<String,Lang*>::iterator, bool> res; res = libLang.insert(LibPair(lang->langName, lang)); return (res.second); } /******************************************************************************** * Get a Lang object by its name. * ********************************************************************************/ const Lang *LibLang::getLang(const TCHAR *langName) { String s1 (langName); stdext::hash_map<String,Lang*>::const_iterator res; res = libLang.find(s1); if (res == libLang.end()) return (NULL); return (res->second); } /******************************************************************************** * Set current language. * * Returns 0 if ok, something else if not. * ********************************************************************************/ int LibLang::setLang(TCHAR *langName) { curLang = (Lang *) getLang(langName); TCHAR *localeInfo = _tsetlocale(LC_ALL, curLang->localeInfo); return (localeInfo == NULL); } /******************************************************************************** * Get current language. * ********************************************************************************/ const TCHAR *LibLang::getLang(void) { return (curLang->langName.c_str()); } /******************************************************************************** * Translate a token with current language. * ********************************************************************************/ const TCHAR *LibLang::translate(TCHAR *token) { return (curLang->get(token)); } /******************************************************************************** * Convert from chars in a locale to TCHAR. * ********************************************************************************/ const TCHAR *LibLang::convert(const char *str) { size_t len = mbstowcs(NULL, str, 0); size_t len2 = len * 2 + 2; if (len2 > allocLen) { if (wcstr != NULL) free(wcstr); allocLen = len2; wcstr = (TCHAR *) malloc(allocLen); } len2 = mbstowcs(wcstr, str, len+1); if (len2 < 0) return (NULL); else return (wcstr); } /******************************************************************************** * Get the number of language translations held in library. * ********************************************************************************/ int LibLang::getNbLang(void) { return (nbLang); } /******************************************************************************** * Get the first language in library. * ********************************************************************************/ const TCHAR *LibLang::getFirst(void) { firstCalled = true; libIterator = libLang.begin(); if (libIterator != libLang.end()) return (libIterator->first.c_str()); return (NULL); } /******************************************************************************** * Get the next language in library. If first time called, return the first * * entry. * ********************************************************************************/ const TCHAR *LibLang::getNext(void) { if (!firstCalled) return getFirst(); if (libIterator != libLang.end()) libIterator++; else return (NULL); if (libIterator != libLang.end()) return (libIterator->first.c_str()); return (NULL); } --- NEW FILE: DataManager.cpp --- #include "StdAfx.h" #include "compat/PalmOS.h" #include "DataManager.h" #include "defuns.h" DataManager::DataManager(void) { in_state = false; attr = 0; type = 0; creator = 0; version = 0; numRecords = 0; head = NULL; } DataManager::~DataManager(void) { } static DataManager *stateMgrHistDB; // This one comes from EasyCalc.cpp void registerDmDatabase (UINT16 cardNo, const TCHAR *nameP, LocalID dbId) { stateMgrHistDB = (DataManager *) dbId; } LocalID DmFindDatabase (UINT16 cardNo, const TCHAR *nameP) { return ((LocalID) stateMgrHistDB); } int DmDatabaseInfo ( UINT16 cardNo, LocalID dbID, TCHAR *nameP, UINT16 *attributesP, UINT16 *versionP, UINT32 *crDateP, UINT32 *modDateP, UINT32 *bckUpDateP, UINT32 *modNumP, LocalID *appInfoIDP, LocalID *sortInfoIDP, UINT32 *typeP, UINT32 *creatorP) { if (dbID == (LocalID) stateMgrHistDB) { *attributesP = stateMgrHistDB->attr; *versionP = (UINT16) (stateMgrHistDB->version); *typeP = stateMgrHistDB->type; *creatorP = stateMgrHistDB->creator; } return (0); } int DmSetDatabaseInfo ( UINT16 cardNo, LocalID dbID, const TCHAR *nameP, UINT16 *attributesP, UINT16 *versionP, UINT32 *crDateP, UINT32 *modDateP, UINT32 *bckUpDateP, UINT32 *modNumP, LocalID appInfoIDP, LocalID sortInfoIDP, UINT32 *typeP, UINT32 *creatorP) { if (dbID == (LocalID) stateMgrHistDB) { stateMgrHistDB->attr = *attributesP; stateMgrHistDB->version = *versionP; } return (0); } DmOpenRef DmOpenDatabase (UINT cardNo, LocalID dbID, UINT16 mode) { if (dbID == (LocalID) stateMgrHistDB) { return (stateMgrHistDB); } return (NULL); } int DmGetLastErr (void) { return (0); } int DmCreateDatabase ( UINT16 cardNo, const TCHAR *nameP, UINT32 creator, UINT32 type, bool resDB) { if (wcscmp (nameP, HISTORYDBNAME) == 0) { stateMgrHistDB->creator = creator; stateMgrHistDB->type = type; } return (0); } Err DmCloseDatabase (DmOpenRef dbP) { return (0); } int DmDeleteDatabase (UINT16 cardNo, LocalID dbID) { return (0); } UINT16 DmNumRecords (DmOpenRef dbP) { return (dbP->numRecords); } MemHandle DmNewRecord (DmOpenRef dbP, UInt16 *atP, UInt32 size) { if (dbP == stateMgrHistDB) { char *p = (char *) malloc ((size+8) & 0xFFFFFFFC); // Add 4 bytes, round up to next 4 bytes limit if (p == NULL) return (NULL); DataRecord *prec = NULL; DataRecord *temp = stateMgrHistDB->head; if (*atP > stateMgrHistDB->numRecords) *atP = stateMgrHistDB->numRecords; for (int i=0 ; (i<*atP) && (temp != NULL) ; i++) temp = (prec = temp)->next; stateMgrHistDB->numRecords++; if (prec == NULL) { // Insert at head temp = stateMgrHistDB->head = new DataRecord(size, p, stateMgrHistDB->head); } else { temp = prec->next = new DataRecord(size, p, temp); } // Put the father record address at beginning of the allocated space *((UInt32 *) p) = (UInt32) temp; return (p+4); } return (NULL); } Err DmRemoveRecord (DmOpenRef dbP, UInt16 index) { if (dbP == stateMgrHistDB) { DataRecord *prec = NULL; DataRecord *temp = stateMgrHistDB->head; if (index >= stateMgrHistDB->numRecords) return (-1); for (int i=0 ; (i<index) && (temp != NULL) ; i++) temp = (prec = temp)->next; stateMgrHistDB->numRecords--; if (prec == NULL) { // Delete at head stateMgrHistDB->head = stateMgrHistDB->head->next; } else { prec->next = temp->next; } free (temp->ptr); delete temp; return (0); } return (-1); } Err DmWrite ( void *recordP, UInt32 offset, const void *srcP, UInt32 bytes) { char *p = (char *) recordP; // Find the record to which this data belongs DataRecord *r = (DataRecord *) (* ((UInt32 *) (((UInt32) recordP) - 4))); if (r->magic != RECORD_MAGIC) return (-1); if (offset + bytes > r->size) return (-2); memcpy (p+offset, srcP, bytes); return (0); } MemHandle DmQueryRecord (DmOpenRef dbP, UInt16 index) { if (dbP == stateMgrHistDB) { DataRecord *temp = stateMgrHistDB->head; if (index >= stateMgrHistDB->numRecords) return (NULL); for (int i=0 ; (i<index) && (temp != NULL) ; i++) temp = temp->next; return (temp->ptr+4); } return (NULL); } //Err DmReleaseRecord (DmOpenRef dbP, UInt16 index, Boolean dirty) { // return (0); //} DataRecord::DataRecord(void) { magic = RECORD_MAGIC; size = 0; ptr = NULL; next = NULL; } DataRecord::DataRecord(UINT32 s, void *p, DataRecord *n) { size = s; ptr = (char *) p; next = n; } DataRecord::~DataRecord(void) { } --- NEW FILE: Lang.h --- /***************************************************************************** * EasyCalc -- a scientific calculator * Copyright (C) 2008 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, version 2, * as published by the Free Software Foundation. * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *****************************************************************************/ /* Lang.h : Lang and LibLang objects for handling language translations. *****************************************************************************/ #pragma once #include <stdio.h> #include <string> #include <string.h> #include <hash_map> typedef std::wstring String; typedef std::pair<String,String> Pair; // Object to contain one language class Lang { protected: int codePage; stdext::hash_map<String,String> translation; public: String langName; TCHAR *localeInfo; Lang(const TCHAR *name); ~Lang(void); bool insert(const TCHAR *key, const TCHAR *value); const TCHAR *get(TCHAR *key); }; // Library of languages, as read from the concatenated lang.rcp file. typedef std::pair<String,Lang*> LibPair; class LibLang { protected: int nbLang; bool firstCalled; Lang *curLang; TCHAR *wcstr; size_t allocLen; stdext::hash_map<String,Lang*> libLang; stdext::hash_map<String,Lang*>::const_iterator libIterator; bool insertLang(Lang *lang); const Lang *getLang(const TCHAR *langName); const TCHAR *convert(const char *str); public: LibLang(FILE *langFile); ~LibLang(void); int getNbLang(void); const TCHAR *getFirst(void); const TCHAR *getNext(void); int setLang(TCHAR *langName); const TCHAR *getLang(void); const TCHAR *translate(TCHAR *key); }; --- NEW FILE: PalmOS.h --- /***************************************************************************** * EasyCalc -- a scientific calculator * Copyright (C) 2008 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, version 2, * as published by the Free Software Foundation. * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *****************************************************************************/ #ifndef PALM_OS_H #define PALM_OS_H 1 #include <types.h> #define Int8 INT8 #define UInt8 UINT8 #define Int16 INT16 #define UInt16 UINT16 #define Int32 INT32 #define UInt32 UINT32 #define Char TCHAR #define Boolean bool #define Coord int #define Err int #define FieldPtr int #define IndexedColorType int #define StrCopy wcscpy #define StrLen wcslen #define StrNCompare wcsncmp #define StrCompare wcscmp #define MemPtrNew malloc #define MemPtrFree free typedef void *MemHandle; typedef void *WinHandle; typedef void *ListPtr; #define MemHandleLock(a) ((MemHandle) a) #define MemHandleUnlock(a) #define noListSelection -1 typedef struct PointType { Coord x; Coord y; } PointType; typedef struct RectangleType { PointType topLeft; PointType extent; } RectangleType; typedef RectangleType *RectanglePtr; #include "DataManager.h" #define ErrFatalDisplayIf(a,b) #define SYS_TRAP(a) #endif --- NEW FILE: DataManager.h --- #pragma once #ifndef DATA_MANAGER_H #define DATA_MANAGER_H 1 #include "PalmOS.h" #define dmModeReadWrite 1 #define dmHdrAttrBackup 2 #define RECORD_MAGIC 0x52454344 class DataRecord { public: UInt32 magic; UInt32 size; char *ptr; DataRecord *next; DataRecord(void); DataRecord(UINT32 s, void *p, DataRecord *n); ~DataRecord(void); }; class DataManager { public: bool in_state; UInt16 attr; UInt32 type; UInt32 creator; UInt16 version; UInt16 numRecords; DataRecord *head; DataManager(void); ~DataManager(void); }; typedef DataManager *DmOpenRef; typedef void *LocalID; void registerDmDatabase (UINT16 cardNo, const TCHAR *nameP, LocalID dbId); LocalID DmFindDatabase (UINT16 cardNo, const TCHAR *nameP); Err DmDatabaseInfo ( UInt16 cardNo, LocalID dbID, Char *nameP, UInt16 *attributesP, UInt16 *versionP, UInt32 *crDateP, UInt32 *modDateP, UInt32 *bckUpDateP, UInt32 *modNumP, LocalID appInfoIDP, LocalID sortInfoIDP, UInt32 *typeP, UInt32 *creatorP ); Err DmSetDatabaseInfo ( UInt16 cardNo, LocalID dbID, const Char *nameP, UInt16 *attributesP, UInt16 *versionP, UInt32 *crDateP, UInt32 *modDateP, UInt32 *bckUpDateP, UInt32 *modNumP, LocalID appInfoIDP, LocalID sortInfoIDP, UInt32 *typeP, UInt32 *creatorP ); DmOpenRef DmOpenDatabase (UInt16 cardNo, LocalID dbID, UInt16 mode); Err DmGetLastErr (void); Err DmCreateDatabase ( UInt16 cardNo, const Char *nameP, UInt32 creator, UInt32 type, Boolean resDB); Err DmCloseDatabase (DmOpenRef dbP); Err DmDeleteDatabase (UInt16 cardNo, LocalID dbID); UInt16 DmNumRecords (DmOpenRef dbP); MemHandle DmNewRecord (DmOpenRef dbP, UInt16 *atP, UInt32 size); Err DmWrite ( void *recordP, UInt32 offset, const void *srcP, UInt32 bytes ); Err DmRemoveRecord (DmOpenRef dbP, UInt16 index); MemHandle DmQueryRecord (DmOpenRef dbP, UInt16 index); //Err DmReleaseRecord (DmOpenRef dbP, UInt16 index, Boolean dirty); #define DmReleaseRecord(a,b,c) #endif --- NEW FILE: dbutil.cpp --- /* * $Id: dbutil.cpp,v 1.1 2009/06/22 21:57:16 mapibid Exp $ * * Scientific Calculator for Palms. * Copyright (C) 1999,2000,2007 Ondrej Palkovsky * * 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., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * * You can contact me at 'on...@pe...'. */ #include "StdAfx.h" #include "compat/PalmOS.h" #include "defuns.h" static Err db_open_byid(LocalID dbid, DmOpenRef *dbref) { *dbref = DmOpenDatabase(CARDNO, dbid, dmModeReadWrite); if (*dbref) return DmGetLastErr(); return 0; } /*********************************************************************** * * FUNCTION: open_db * * DESCRIPTION: Open a database for EasyCalc. Creates a new one, * if the database does not exist or if the old database * has the wrong version * * PARAMETERS: * * RETURN: 0 - on success * other - on error * ***********************************************************************/ Int16 open_db(const Char *name, UInt16 dbversion, UInt32 crid, UInt32 dbtype, DmOpenRef *dbref) { Err err; LocalID dbid; UInt16 version; UInt32 creator; UInt32 type; UInt16 attr = 0; dbid = DmFindDatabase(CARDNO, name); if (dbid) { /* Database exists */ DmDatabaseInfo(CARDNO, dbid, NULL, /* name */ &attr, /* attrib */ &version, /* version */ NULL, /* crDate */ NULL, /* modDate */ NULL, /* bckUpDate */ NULL, /* modNum */ NULL, /* appinfoID */ NULL, /* sortInfoID */ &type, /* Type */ &creator); /* Creator */ if (version == dbversion && creator == crid && type == dbtype) { if ((attr & dmHdrAttrBackup) == 0) { attr |= dmHdrAttrBackup; DmSetDatabaseInfo(CARDNO, dbid, NULL, /* name */ &attr, /* attrib */ &version, /* version */ NULL, /* crDate */ NULL, /* modDate */ NULL, /* bckupDate */ NULL, /* modNum */ NULL, /* appinfoID */ NULL, /* sortInfoID */ NULL, /* Type */ NULL); /* Creator */ } return db_open_byid(dbid, dbref); } /* Database exists, but with incorrect version */ err = DmDeleteDatabase(CARDNO, dbid); if (err) return err; } /* Database doesn't exist or old version */ err = DmCreateDatabase(CARDNO, name, crid, dbtype, false); if (err) return err; dbid = DmFindDatabase(CARDNO, name); version = dbversion; attr |= dmHdrAttrBackup; DmSetDatabaseInfo(CARDNO, dbid, NULL, /* name */ &attr, /* attrib */ &version, /* version */ NULL, /* crDate */ NULL, /* modDate */ NULL, /* bckUpDate */ NULL, /* modNum */ NULL, /* appinfoID */ NULL, /* sortInfoID */ NULL, /* Type */ NULL); /* Creator */ return db_open_byid(dbid, dbref); } |