[6127] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * IsisDb.cpp
|
---|
| 4 | * Copyright (C) 2003 UNESCO
|
---|
| 5 | *
|
---|
| 6 | * A component of the Greenstone digital library software
|
---|
| 7 | * from the New Zealand Digital Library Project at the
|
---|
| 8 | * University of Waikato, New Zealand.
|
---|
| 9 | *
|
---|
| 10 | * This program is free software; you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * This program is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with this program; if not, write to the Free Software
|
---|
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | *
|
---|
| 24 | *********************************************************************/
|
---|
| 25 |
|
---|
| 26 | ////////////////////////////////////////////////////////////
|
---|
| 27 | // ISISDB: Isis Master file methods
|
---|
| 28 | ////////////////////////////////////////////////////////////
|
---|
| 29 | #include "stdafx.h"
|
---|
| 30 | #include "IsisDb.h"
|
---|
| 31 | #include "XRFFile.h"
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | //--------------------------------------------------------------------------------
|
---|
| 36 | // IsisDb::IsisDb()
|
---|
| 37 | //
|
---|
| 38 | // Constructor, Makes an ISIS Data Base object that isn't connected to a Master
|
---|
| 39 | // file, Cross Reference file and Fdt.
|
---|
| 40 | //-------------------------------------------------------------------------------
|
---|
| 41 | IsisDb::IsisDb()
|
---|
| 42 | {
|
---|
| 43 | bReadOnly_ = false;
|
---|
| 44 | try
|
---|
| 45 | {
|
---|
| 46 | mf_ = new MfFile;
|
---|
| 47 | xf_ = new XrfFile;
|
---|
| 48 | fdt_ = new CFdt;
|
---|
| 49 | }
|
---|
| 50 | catch(std::bad_alloc)
|
---|
| 51 | {
|
---|
| 52 | throw CIsisDbException(ISIS_BAD_ALLOC, __FILE__, __LINE__);
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | //////////////////////////////
|
---|
| 57 |
|
---|
| 58 | IsisDb::~IsisDb( )
|
---|
| 59 | {
|
---|
| 60 | delete mf_; delete xf_; delete fdt_;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | //////////////////////////////
|
---|
| 65 |
|
---|
| 66 | int IsisDb::CreateDb(const TCHAR *fname)
|
---|
| 67 | {
|
---|
| 68 | name = fname;
|
---|
| 69 | MkNames();
|
---|
| 70 | int rc = ISIS_NO_ERROR;
|
---|
| 71 |
|
---|
| 72 | // We should not try to create a mf_ which already exist!
|
---|
| 73 | if (CFileBase::Exists(mfname_))
|
---|
| 74 | {
|
---|
| 75 | throw CIsisDbException(ISIS_MF_ALREADY_EXIST_ERROR, __FILE__, __LINE__);
|
---|
| 76 | }
|
---|
| 77 | else
|
---|
| 78 | {
|
---|
| 79 | try
|
---|
| 80 | {
|
---|
| 81 | rc = mf_->CreateMf(mfname_, USERMST);
|
---|
| 82 | rc = xf_->CreateXrf(xrfname_);
|
---|
| 83 | rc = fdt_->Load(fdtname_);
|
---|
| 84 | }
|
---|
| 85 | catch(CIsisDbException)
|
---|
| 86 | {
|
---|
| 87 | throw CIsisDbException(ISIS_MF_OPEN_ERROR, __FILE__, __LINE__);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | return rc;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 | //////////////////////////////
|
---|
| 96 |
|
---|
| 97 | void IsisDb::CloseDb()
|
---|
| 98 | {
|
---|
| 99 | mf_->CloseMf();
|
---|
| 100 | xf_->Close();
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | //////////////////////////////
|
---|
| 104 |
|
---|
| 105 | void IsisDb::MkNames()
|
---|
| 106 | // Make file names
|
---|
| 107 | {
|
---|
| 108 | const _TCHAR *s = name.c_str();
|
---|
| 109 | _TCHAR *ps = new _TCHAR[_tcslen(s)+1];
|
---|
| 110 | _tcscpy(ps, s);
|
---|
| 111 | _TCHAR * tmp = _tcsrchr(ps, _T('.'));
|
---|
| 112 | assert(tmp != NULL);
|
---|
| 113 | // Terminate the string at the last dot
|
---|
| 114 | *tmp = _T('\0');
|
---|
| 115 |
|
---|
| 116 | _tcscpy(mfname_,ps);
|
---|
| 117 | _tcscat(mfname_,_T(".mst"));
|
---|
| 118 | _tcscpy(xrfname_,ps);
|
---|
| 119 | _tcscat(xrfname_,_T(".xrf"));
|
---|
| 120 | _tcscpy(fdtname_,ps);
|
---|
| 121 | _tcscat(fdtname_,_T(".fdt"));
|
---|
| 122 | _tcscpy(idxname_,ps);
|
---|
| 123 | _tcscat(idxname_,_T(".idx"));
|
---|
| 124 | delete[] ps;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | //////////////////////////////
|
---|
| 128 |
|
---|
| 129 | int IsisDb::OpenDb(const TCHAR *fname, FileSystem::AccessMode mode
|
---|
| 130 | /* = FileSystem::FILE_READWRITE */)
|
---|
| 131 | {
|
---|
| 132 | name = fname;
|
---|
| 133 | MkNames();
|
---|
| 134 | int rc;
|
---|
| 135 | try
|
---|
| 136 | {
|
---|
| 137 | // if (FileSystem::IsReadOnly(mfname_))
|
---|
| 138 | // mode = FileSystem::FILE_READONLY;
|
---|
| 139 | rc = mf_->OpenMf(mfname_, mode);
|
---|
| 140 | rc = xf_->OpenXrf(xrfname_, mode);
|
---|
| 141 | rc = fdt_->Load(fdtname_);
|
---|
| 142 | }
|
---|
| 143 | catch(CIsisDbException)
|
---|
| 144 | {
|
---|
| 145 | throw CIsisDbException(ISIS_MF_OPEN_ERROR, __FILE__, __LINE__);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | return rc;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | //----------------------------------------------------------------------------------
|
---|
| 152 | // int IsisDb::ReadDbRecord(long mfn, MfRecord &m)
|
---|
| 153 | //
|
---|
| 154 | // This function reads Master File record with MFN "mfn" into m
|
---|
| 155 | //
|
---|
| 156 | // Returns the status of the record:
|
---|
| 157 | // Active=0, LogicalyDeleted=1, PhysicalyDeleted=2, EndOfFile=-1, NewRecord = 3
|
---|
| 158 | //----------------------------------------------------------------------------------
|
---|
| 159 | int IsisDb::ReadDbRecord(long mfn, MfRecord &m)
|
---|
| 160 | {
|
---|
| 161 | long mfb;
|
---|
| 162 | int mfp;
|
---|
| 163 | int status;
|
---|
| 164 | bool rc;
|
---|
| 165 |
|
---|
| 166 | try
|
---|
| 167 | {
|
---|
| 168 | status = xf_->GetMfp(mfn, mfb, mfp);
|
---|
| 169 | if (status == Active || status == LogicalyDeleted)
|
---|
| 170 | if (!mf_->ReadMfRecord(mfb, mfp, m))
|
---|
| 171 | status = -1; // Error in reading the record!
|
---|
| 172 | }
|
---|
| 173 | catch(CIsisDbException)
|
---|
| 174 | {
|
---|
| 175 | throw CIsisDbException(ISIS_MF_OPEN_ERROR, __FILE__, __LINE__);
|
---|
| 176 | }
|
---|
| 177 | return status;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | //--------------------------------------------------------------------------------
|
---|
| 181 | // void CreateDbRecord(MfRecord &m)
|
---|
| 182 | //
|
---|
| 183 | // Create a new record at the end of the master file. The Master file address is
|
---|
| 184 | // set into the pair mfb/mfp and the XRF file is updated.
|
---|
| 185 | //---------------------------------------------------------------------------------
|
---|
| 186 | void IsisDb::CreateDbRecord(MfRecord &m)
|
---|
| 187 | {
|
---|
| 188 |
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | //--------------------------------------------------------------------------------
|
---|
| 192 | // void CreateDbRecordWithMfn(MfRecord &m)
|
---|
| 193 | //
|
---|
| 194 | // Create a new record at the end of the master file. The Master file address is
|
---|
| 195 | // set into the pair mfb/mfp and the XRF file is updated.
|
---|
| 196 | //---------------------------------------------------------------------------------
|
---|
| 197 | void IsisDb::CreateDbRecordWithMfn(MfRecord &m)
|
---|
| 198 | {
|
---|
| 199 |
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | //----------------------------------------------------------------------------------
|
---|
| 204 | // void UpdateDbRecord(MfRecord &m)
|
---|
| 205 | //
|
---|
| 206 | // Update of existing records. Whenever a record is updated, the system writes
|
---|
| 207 | // the record back to the master file. Where it is written depends on the status
|
---|
| 208 | // of the record when it was initially read.
|
---|
| 209 | //
|
---|
| 210 | //----------------------------------------------------------------------------------
|
---|
| 211 | void IsisDb::UpdateDbRecord(MfRecord &m)
|
---|
| 212 | {
|
---|
| 213 | }
|
---|
| 214 | //----------------------------------------------------------------------------------
|
---|
| 215 | // int IsisDb::DeleteRecord(long mfn)
|
---|
| 216 | //
|
---|
| 217 | // This function logically deletes a Master File record with MFN "mfn".
|
---|
| 218 | // On XRF, xrfmfb is negative and on MST, status is set to 1
|
---|
| 219 | //
|
---|
| 220 | // Returns true if successful
|
---|
| 221 | //----------------------------------------------------------------------------------
|
---|
| 222 | bool IsisDb::DeleteRecord(long mfn)
|
---|
| 223 | {
|
---|
| 224 |
|
---|
| 225 | return false;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | long IsisDb::GetNextActiveMfn(long mfn /* = -1 */)
|
---|
| 229 | {
|
---|
| 230 | if (mfn == -1)
|
---|
| 231 | mfn = 1;
|
---|
| 232 | else
|
---|
| 233 | mfn++;
|
---|
| 234 | long mfb;
|
---|
| 235 | int mfp;
|
---|
| 236 | int iRet = xf_->GetMfp(mfn, mfb, mfp);
|
---|
| 237 | while (iRet != Active && iRet != EndOfFile)
|
---|
| 238 | {
|
---|
| 239 | mfn++;
|
---|
| 240 | iRet = xf_->GetMfp(mfn, mfb, mfp);
|
---|
| 241 | }
|
---|
| 242 | if (iRet == EndOfFile)
|
---|
| 243 | return 0;
|
---|
| 244 | else
|
---|
| 245 | return mfn;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | _TCHAR *IsisDb::GetIdxFileName()
|
---|
| 249 | {
|
---|
| 250 | return idxname_;
|
---|
| 251 | }
|
---|
| 252 |
|
---|