?? arrdbase.c
字號:
/*
Copyright (c) 2003, Dan Kranz and Arnold Rom
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* The names of its contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
//#include <unistd.h>
#include <roots.h>
// ##################################################################
// read data base #
// #
// output: ReadBase = TRUE if data base was read #
// = FALSE if we had an error #
// #
// input: pointer to DataBaseStructure #
// ##################################################################
BYTE ReadBase(DataBase* db)
// ----------------------------------------------------------------------------
{ // ReadBase START
// ----------------------------------------------------------------------------
BYTE *stream,*block,*startOfStream,junkTable;
char fspec[MAX_PATH];
long cpl,i,nbtabs;
struct stat buf;
// translate the directory
if(ReadAndTranslateDirectory(db)) return 1;
// ----------------------------------------------------------------------------
// read the data base file
// ----------------------------------------------------------------------------
if(stat(strcat(strcpy(fspec,db->name),".bas"),&buf)!=-1) // empty ?
{
if(ReadBinFile(&stream, &cpl,
strcat(strcpy(fspec,db->name),".bas"))) return 1;
startOfStream=stream;
// first 5 bytes contain: rflag(10101010) cpl nline
if(*stream != 0xaa)
{
free(startOfStream);
RootsError("invalid data base file\n");
return 1;
}
cpl= *(stream+1)+0x100*(*(stream+2));
db->base.nline= *(stream+3)+0x100*(*(stream+4));
// advance pointer to first data byte
stream+=5;
// open array to hold directory specified cpl
ExpandBlock(db->base,db->base.nline);
// move data to block based on actual versus directory cpl
if(cpl == db->base.cpl)
{
memcpy(db->base.block,stream,db->base.cpl*db->base.nline);
}
else if(cpl<db->base.cpl)
{
block=db->base.block;
for(i=0; i<db->base.nline; i++)
{memcpy(block,stream,cpl); stream+=cpl; block+=db->base.cpl;}
}
else
{
block=db->base.block;
for(i=0; i<db->base.nline; i++)
{memcpy(block,stream,db->base.cpl);
stream+=cpl; block+=db->base.cpl;}
}
free(startOfStream);
}
// ----------------------------------------------------------------------------
// read the tables file
// ----------------------------------------------------------------------------
if(stat(db->name,&buf)!=-1) // skip empties
{
if(ReadBinFile(&stream, &cpl,db->name)) return 1;
startOfStream=stream;
// first 3 bytes contain: rflag(01010101) nbtabs
if(*stream != 0x55)
{
free(startOfStream);
RootsError("invalid tables file\n");
return 1;
}
nbtabs= *(stream+1)+0x100*(*(stream+2));
stream+=3;
if(nbtabs > db->nbtabs) nbtabs=db->nbtabs; // drop non-directory tabs
// --------------------------------
// move one table at the time
// --------------------------------
for(i=0; i<nbtabs; i++)
{
// first 5 bytes contain: rflag(10101010) cpl nline
if(*stream != 0xaa)
{
free(startOfStream);
RootsError("invalid tables file\n");
return 1;
}
cpl= *(stream+1)+0x100*(*(stream+2));
db->arx[i].nline= *(stream+3)+0x100*(*(stream+4));
stream+=5;
// skip epmties
if(cpl==0 || db->arx[i].nline == 0) continue;
// allow for junk table (i.e. Tony's removal)
junkTable=FALSE;
if(!db->arx[i].cpl)
{
junkTable=TRUE;
db->arx[i].cpl=cpl;
}
// audit for valid cpl
if(cpl!=db->arx[i].cpl)
{
free(startOfStream);
RootsError("data base table %d has wrong cpl\n",i+1);
return 1;
}
// move into place
ExpandBlock(db->arx[i],db->arx[i].nline);
memcpy(db->arx[i].block,stream,db->arx[i].cpl*db->arx[i].nline);
stream+=db->arx[i].cpl*db->arx[i].nline;
// get rid of junk table
if(junkTable)db->arx[i].nline=db->arx[i].cpl=0;
}
free(startOfStream);
} // read tables END
else // check validity of empty
{
if(db->base.nline && db->nbtabs)
{RootsError("** tables file for \"%s\" not found **\n",db->name);
return 1;}
}
return 0;
// ----------------------------------------------------------------------------
} // ReadBase END
// ----------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -