Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

ZIDFile.cxx

Go to the documentation of this file.
00001 /*
00002   Copyright (C) 2006-2007 Werner Dittmann
00003 
00004   This program is free software: you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation, either version 3 of the License, or
00007   (at your option) any later version.
00008 
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details.
00013 
00014   You should have received a copy of the GNU General Public License
00015   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 */
00017 
00018 /*
00019  * Authors: Werner Dittmann <Werner.Dittmann@t-online.de>
00020  */
00021 // #define UNIT_TEST
00022 
00023 #include <time.h>
00024 #include <stdlib.h>
00025 
00026 #include <libzrtpcpp/ZIDFile.h>
00027 
00028 static ZIDFile* instance;
00029 
00030 ZIDFile::~ZIDFile() {
00031 }
00032 
00033 ZIDFile* ZIDFile::getInstance() {
00034 
00035     if (instance == NULL) {
00036         instance = new ZIDFile();
00037     }
00038     return instance;
00039 }
00040 
00041 int ZIDFile::open(char *name) {
00042     zidrecord_t rec;
00043     unsigned int* ip;
00044 
00045     // check for an already active ZID file
00046     if (zidFile != NULL) {
00047         return 0;
00048     }
00049     if ((zidFile = fopen(name, "rb+")) == NULL) {
00050         zidFile = fopen(name, "wb+");
00051         // New file, generate an associated random ZID and save
00052         // it as first record
00053         if (zidFile != NULL) {
00054             ip = (unsigned int*)associatedZid;
00055             memset(&rec, 0, sizeof(zidrecord_t));
00056             srandom(time(NULL));
00057             *ip++ = random();
00058             *ip++ = random();
00059             *ip = random();
00060             memcpy(rec.identifier, associatedZid, IDENTIFIER_LEN);
00061             fseek(zidFile, 0L, SEEK_SET);
00062             rec.ownZid = 1;
00063             fwrite(&rec, sizeof(zidrecord_t), 1, zidFile);
00064             fflush(zidFile);
00065         }
00066     }
00067     else {
00068         fseek(zidFile, 0L, SEEK_SET);
00069         if (fread(&rec, sizeof(zidrecord_t), 1, zidFile) != 1) {
00070             fclose(zidFile);
00071             zidFile = NULL;
00072             return -1;
00073         }
00074         if (rec.ownZid != 1) {
00075             fclose(zidFile);
00076             zidFile = NULL;
00077             return -1;
00078         }
00079         memcpy(associatedZid, rec.identifier, IDENTIFIER_LEN);
00080     }
00081     return ((zidFile == NULL) ? -1 : 1);
00082 }
00083 
00084 void ZIDFile::close() {
00085 
00086     if (zidFile != NULL) {
00087         fclose(zidFile);
00088         zidFile = NULL;
00089     }
00090 }
00091 
00092 unsigned int ZIDFile::getRecord(ZIDRecord *zidRecord) {
00093     unsigned long pos;
00094     zidrecord_t rec;
00095     int numRead;
00096 
00097     fseek(zidFile, (long)(sizeof(zidrecord_t)), SEEK_SET);
00098 
00099     do {
00100         pos = ftell(zidFile);
00101         numRead = fread(&rec, sizeof(zidrecord_t), 1, zidFile);
00102 
00103         // skip invalid records
00104         while(rec.ownZid == 1 && rec.recValid == 0 && numRead == 1) {
00105             numRead = fread(&rec, sizeof(zidrecord_t), 1, zidFile);
00106         }
00107 
00108         // if we are at end of file, then no record found. Create new
00109         // record and write its data at end of file.
00110         if (numRead == 0) {
00111             memset(&rec, 0, sizeof(zidrecord_t));
00112             memcpy(rec.identifier, zidRecord->record.identifier, IDENTIFIER_LEN);
00113             rec.recValid = 1;
00114             fwrite(&rec, sizeof(zidrecord_t), 1, zidFile);
00115             break;
00116         }
00117     } while (memcmp(zidRecord->record.identifier, rec.identifier, IDENTIFIER_LEN) != 0);
00118 
00119     // Copy the read data into the record structure
00120     memcpy(&zidRecord->record, &rec, sizeof(zidrecord_t));
00121 
00122     //  remember position of record in file for save operation
00123     zidRecord->position = pos;
00124     return 1;
00125 }
00126 
00127 unsigned int ZIDFile::saveRecord(ZIDRecord *zidRecord) {
00128 
00129     fseek(zidFile, zidRecord->position, SEEK_SET);
00130     fwrite(&zidRecord->record, sizeof(zidrecord_t), 1, zidFile);
00131     return 1;
00132 }
00133 
00134 #ifdef UNIT_TEST
00135 
00136 int main(int argc, char *argv[]) {
00137 
00138     ZIDFile *zid = ZIDFile::getInstance();
00139 
00140     zid->open("testzid");
00141     ZIDRecord zr3("123456789012");
00142     zid->getRecord(&zr3);
00143     zid->saveRecord(&zr3);
00144     ZIDRecord zr4("210987654321");
00145     zid->getRecord(&zr4);
00146     zid->saveRecord(&zr4);
00147 
00148     zr3.setNewRs1("11122233344455566677788899900012");
00149     zid->saveRecord(&zr3);
00150     zr3.setNewRs1("00099988877766655544433322211121");
00151     zid->saveRecord(&zr3);
00152 
00153     zid->close();
00154 
00155     // Reopen, manipulate 2nd record
00156     zid->open("testzid");
00157 
00158     ZIDRecord zr5("210987654321");
00159     zid->getRecord(&zr5);
00160 
00161     zr5.setNewRs1("aaa22233344455566677788899900012");
00162     zid->saveRecord(&zr5);
00163     zr5.setNewRs1("bbb99988877766655544433322211121");
00164     zid->saveRecord(&zr5);
00165 
00166     zid->close();
00167 
00168     // reopen, manipulate 2nd record again
00169     zid->open("testzid");
00170 
00171     ZIDRecord zr6("210987654321");
00172     zid->getRecord(&zr6);
00173 
00174     zr6.setNewRs1("ccc22233344455566677788899900012");
00175     zr6.setNewRs1("ddd99988877766655544433322211121");
00176     zid->saveRecord(&zr6);
00177 
00178     zid->close();
00179 
00180 }
00181 
00182 #endif
00183 

© sourcejam.com 2005-2008