home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
pendem.zip
/
BOOKFL.C
< prev
next >
Wrap
Text File
|
1994-07-21
|
24KB
|
832 lines
/* ==========================================================================
* Sarka Martinez (407) 982- 5580, Pen Developer Magazine July/August '94 page 73
*
* Copyright.
*
* OCO Source Materials
*
* 5706-110 (C) Copyright IBM Corp. 1993
*
* ======================================================================== */
/* ======================================================================
* IBM CONFIDENTIAL
* ======================================================================
*
* product name.
*
* Filespec. BOOKFL.C
*
* Author. Sarka Martinez
*
* Date-Written. 05-11-1993
*
* Copyright. Copyright (C) IBM Corporation 1993
* LICENSED MATERIAL-PROGRAM PROPERTY OF IBM
*
* Change History.
*
* Rel Programmer Date Description
* ----- ------------- ------- --------------------------------
* 1.00 Sarka Martinez 051193 Demo Stage
*
* =================================================================== */
#define INCL_DOS
#define INCL_WIN
#define INCL_DOSDATETIME
#define INCL_GPI
#include <os2.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <io.h>
#include <string.h> // C-runtime String routines
#include <stdio.h> // C-Standard I/O functions
#include <stdlib.h>
#include "book.h"
SHORT FindNameFileLoc(INT *hf, CARDINFO *CardInfo, DATAFILE *FileD);
SHORT GetName(INT *hf, CHAR *RecId, USHORT len);
SHORT FindRecIdFileLoc(INT *hf, CARDINFO *CardInfo, DATAFILE *FileD);
SHORT GetRecID(INT *hf, CHAR *RecId, USHORT len);
SHORT UpdateFile(INT *hf, CARDINFO *CardInfo, USHORT len, DATAFILE *FileD);
SHORT CreateNewFile(INT *hf, CARDINFO *CardInfo);
SHORT FindFileSec(INT *hf, DATAFILE *FileD, CHAR *RecId);
SHORT DelFile(INT *hf, USHORT len, LONG old_pos, DATAFILE *FileD);
CARDINFO *Names;
extern HAB hab;
extern CHAR *BookPath;
// read the .dat file and insert all info into the global data struct...
USHORT GetAllRecords(HWND hwnd, USHORT *numRecs)
{
USHORT fieldLength, fileLength;
DATAFILE *FileD;
CHAR file[45], Bmpfile[45];
INT hf;
HPS hps; /* Handle to Presentation space */
*numRecs = 0;
FileD = (DATAFILE *)malloc(sizeof(DATAFILE));
FileD->FileData = (CHAR *)malloc(FILE_MAXSIZE+1);
sprintf(file, "%s\\book.dat", BookPath);
hf = open(file, O_RDONLY);
if (hf == -1 || hf == 0)
return 1;
FileD->TotLen = read(hf, FileD->FileData, FILE_MAXSIZE);
close(hf);
if ((FileD->TotLen == 0) || (FileD->TotLen == -1))
return 1;
fileLength = 0;
if ((Names = (CARDINFO *) malloc(MAXNAMES*sizeof(CARDINFO))) == NULL) {
DosBeep(500,100);
return 2;
}
while (fileLength < FileD->TotLen)
{
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 8; fieldLength++, fileLength++)
Names[*numRecs].RecId[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].RecId[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 30; fieldLength++, fileLength++)
Names[*numRecs].LName[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].LName[fieldLength] = '\0';
fileLength++; // skip the return line char.
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 30; fieldLength++, fileLength++)
Names[*numRecs].FName[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].FName[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 1; fieldLength++, fileLength++)
Names[*numRecs].Initial[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].Initial[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 100; fieldLength++, fileLength++)
Names[*numRecs].Company[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].Company[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 100; fieldLength++, fileLength++)
Names[*numRecs].Division[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].Division[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 201; fieldLength++, fileLength++)
Names[*numRecs].StrAddr[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].StrAddr[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 30; fieldLength++, fileLength++)
Names[*numRecs].City[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].City[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 2; fieldLength++, fileLength++)
Names[*numRecs].State[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].State[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 10; fieldLength++, fileLength++)
Names[*numRecs].Zip[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].Zip[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 15; fieldLength++, fileLength++)
Names[*numRecs].Phone[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].Phone[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 15; fieldLength++, fileLength++)
Names[*numRecs].Fax[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].Fax[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 8; fieldLength++, fileLength++)
Names[*numRecs].PagerID[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].PagerID[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 8; fieldLength++, fileLength++)
Names[*numRecs].MailUserId[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].MailUserId[fieldLength] = '\0';
fileLength++;
for (fieldLength = 0; FileD->FileData[fileLength] != '\n' && fieldLength < 8; fieldLength++, fileLength++)
Names[*numRecs].MailNodeId[fieldLength] = FileD->FileData[fileLength];
Names[*numRecs].MailNodeId[fieldLength] = '\0';
fileLength++;
// read in the logo bitmap
sprintf(Bmpfile, "%s\\%s.bmp", BookPath, Names[*numRecs].RecId);
hps = WinGetPS (hwnd);
Names[*numRecs].hbmBitmap = ReadBitmapFile(Bmpfile, &hps);
WinReleasePS (hps);
(*numRecs)++;
}
}
HBITMAP ReadBitmapFile(PSZ PictureDataFile, HPS *hpsMem)
{
HDC hdc;
PBYTE lpFileBuf;
SIZEL lSize;
HBITMAP pbm;
FILE *fp;
SEL Sel;
USHORT Row;
USHORT MaxLines;
USHORT BytesPerLine;
SIZEL sz;
static struct
{
BITMAPFILEHEADER bmfh; // Data from loaded bitmap file
RGB palette[256];
} bm;
/* open bitmap file */
if ((fp = fopen(PictureDataFile, "rb")) == NULL)
return NULLHANDLE;
/* read bitmap file header */
fread(&bm.bmfh, sizeof(BITMAPFILEHEADER), 1, fp);
/* make sure it really is a bitmap */
if ( bm.bmfh.usType != BFT_BMAP || bm.bmfh.bmp.cBitCount > 24 )
{
fclose(fp);
return NULLHANDLE;
}
/* skip past BMP file palette */
fread( bm.palette, (1 << bm.bmfh.bmp.cBitCount) * sizeof(RGB), 1, fp);
/* Create a memory DC */
hdc = DevOpenDC(hab, OD_MEMORY, "*", 0L, NULL, NULLHANDLE);
if (hdc == DEV_ERROR)
{
fclose(fp);
return NULLHANDLE;
}
/* Create a memory PS */
lSize.cx = 0L;
lSize.cy = 0L;
*hpsMem = GpiCreatePS(hab, hdc, &lSize, PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC);
if (*hpsMem == GPI_ERROR)
{
fclose(fp);
return NULLHANDLE;
}
/* Create a bitmap */
pbm = GpiCreateBitmap(*hpsMem, (PBITMAPINFOHEADER2)&bm.bmfh.bmp,
0L, NULL, NULLHANDLE);
/* Associate the bitmap to the memory presentation space */
GpiSetBitmap(*hpsMem, pbm);
#define BYTES_IN_LONGWORD 4
#define BITS_IN_4_BYTES 32
/* calculate the number of bytes per scan line */
BytesPerLine = ((( (USHORT)bm.bmfh.bmp.cBitCount * (USHORT)bm.bmfh.bmp.cx +
(BITS_IN_4_BYTES - 1)) / BITS_IN_4_BYTES) * bm.bmfh.bmp.cPlanes
* BYTES_IN_LONGWORD);
if (BytesPerLine == 0)
{
fclose(fp);
return NULLHANDLE;
}
/* allocate memory for reading bitmap file */
// initialize
for ( Sel = 0, MaxLines = (USHORT) (32768L / BytesPerLine);
// test
(lpFileBuf = (PBYTE)malloc((USHORT)(MaxLines * BytesPerLine)))
// DosAllocSeg((USHORT)(MaxLines * BytesPerLine), (PSEL)&Sel, NULL)
&& (MaxLines > 1);
// decriment
MaxLines /= 2 );
// if (!Sel)
// {
// fclose(fp);
// return NULLHANDLE; // no memory
// }
// lpFileBuf = (PBYTE)MAKEP(Sel, 0); // make a far pointer
/* read the file and set the bitmap bits as we go */
Row = 0;
if (MaxLines > (SHORT) bm.bmfh.bmp.cy)
MaxLines = (SHORT) bm.bmfh.bmp.cy;
while (Row < (SHORT) bm.bmfh.bmp.cy)
{
fread( lpFileBuf, (USHORT)(BytesPerLine * MaxLines), 1, fp );
GpiSetBitmapBits(*hpsMem, (LONG) Row, (LONG) MaxLines, lpFileBuf,
(PBITMAPINFO2) &bm.bmfh.bmp);
Row += MaxLines;
if ((SHORT) bm.bmfh.bmp.cy - Row < MaxLines)
MaxLines = (SHORT) bm.bmfh.bmp.cy - Row;
}
// DosFreeSeg(Sel);
free(lpFileBuf);
fclose(fp);
sz.cx = bm.bmfh.bmp.cx;
sz.cy = bm.bmfh.bmp.cy;
GpiSetBitmapDimension(pbm, &sz);
GpiSetBitmap(*hpsMem, NULLHANDLE);
GpiDestroyPS(*hpsMem);
DevCloseDC(hdc);
return pbm;
}
// prepare to update the .dat file
VOID PrepUpdRec(CARDINFO *CardInfo, CHAR *bmp_file, USHORT bmp_length)
{
DATAFILE *FileD;
CHAR file[45];
INT hf;
SHORT rc;
FileD = (DATAFILE *)malloc(sizeof(DATAFILE));
FileD->FileData = (CHAR *)malloc(FILE_MAXSIZE+1);
sprintf(file, "%s\\book.dat", BookPath);
hf = open(file, O_RDONLY);
if (hf == -1 || hf == 0)
return;
FileD->TotLen = read(hf, FileD->FileData, FILE_MAXSIZE);
close(hf);
hf = open(file, O_RDWR);
rc = FindRecIdFileLoc(&hf, CardInfo, FileD);
close(hf);
// copy the .bmp over
sprintf(file, "%s\\%s.bmp", BookPath, CardInfo->RecId);
hf = open(file, O_WRONLY | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
if (hf == -1 || hf == 0)
return;
lseek(hf, 0L, SEEK_SET);
write( hf, bmp_file, bmp_length);
close(hf);
free(FileD->FileData);
free(FileD);
}
// prepare to add a new record to the .dat file
VOID PrepAddToRec(CARDINFO *CardInfo, CHAR *bmp_file, USHORT bmp_length)
{
DATAFILE *FileD;
CHAR file[45];
INT hf;
SHORT rc;
FileD = (DATAFILE *)malloc(sizeof(DATAFILE));
FileD->FileData = (CHAR *)malloc(FILE_MAXSIZE+1);
sprintf(file, "%s\\book.dat", BookPath);
hf = open(file, O_RDONLY);
if (hf == -1 || hf == 0) { // file does not exist, so creat it
hf = open(file, O_WRONLY | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
lseek(hf, 0L, SEEK_SET);
CreateNewFile(&hf, CardInfo);
close(hf);
}
else {
FileD->TotLen = read(hf, FileD->FileData, FILE_MAXSIZE);
close(hf);
hf = open(file, O_RDWR);
// find where to add this name, .dat file stays in alphabetic order.
rc = FindNameFileLoc(&hf, CardInfo, FileD);
close(hf);
}
// copy the .bmp over
sprintf(file, "%s\\%s.bmp", BookPath, CardInfo->RecId);
hf = open(file, O_WRONLY | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
if (hf == -1 || hf == 0)
return;
lseek(hf, 0L, SEEK_SET);
write( hf, bmp_file, bmp_length);
close(hf);
free(FileD->FileData);
free(FileD);
}
// Find and Compare the Last Name fields inorder to insert a new record in the
// correct area, file is in alphabetic order...
// as of this time there is no support for people w/ the same last name...
// the new record will simply be inserted before the old one.
SHORT FindNameFileLoc(INT *hf, CARDINFO *CardInfo, DATAFILE *FileD)
{
CHAR str[1], currName[31];
SHORT rc=0, line=0, i=0, len=0, prevlen=0;
// skip the RecId field & get to the Last Name field
while (!eof(*hf) && (line < 1) && (len < 101)) {
rc = read(*hf, str, 1);
if (rc == -1)
return rc;
len++;
if (str[0] == '\n')
line = 1;
}
// at Last name field, now read it in
// len is 0 because you want to add it to the front of the file
len = GetName(hf, currName, 0);
if (len == -1)
return -1;
// if the string is not higher in the alphabet
// hardcoded for codepage 437
if (WinCompareStrings(hab, (ULONG)437, (ULONG)1,
CardInfo->LName, currName, 0L) != WCS_GT) {
rc = UpdateFile(hf, CardInfo, len, FileD);
if (rc)
return 1;
return 0;
}
line = 0;
/* find Next User Id line */
while (!eof(*hf)) {
if (line == 13)
prevlen = len; // add begining of record so save that point.
while (!eof(*hf) && (line < 15) && (i < 101)) {
rc = read(*hf, str, 1);
if (rc == -1)
return rc;
i++;
len++;
if (str[0] == '\n') {
line++;
i=0;
}
}
if (i >= 101)
return 1;
if (!eof(*hf)) {
/* find Last name */
len = GetName(hf, currName, len);
if (len == -1)
return -1;
if (WinCompareStrings(hab, (ULONG)437, (ULONG)1,
CardInfo->LName, currName, 0L) != WCS_GT) {
// want to add it before this record so use prevlen...
rc = UpdateFile(hf, CardInfo, prevlen, FileD);
if (rc)
return 1;
return 0;
}
line=0;
prevlen=0;
i=0;
}
else /* entry was not found */
return 1;
}
return 1; /* entry was not found */
}
// read in the Last Name field in the file
SHORT GetName(INT *hf, CHAR *LName, USHORT len)
{
CHAR str[1];
SHORT rc=0, i=0;
/* find Last Name*/
while (!eof(*hf) && (i < 31)) {
rc = read(*hf, str, 1);
if (rc == -1)
return rc;
len++;
if (str[0] != '\n')
LName[i] = str[0];
else {
LName[i] = '\0';
return len;
}
i++;
}
if (!eof(*hf)) {
LName[i] = '\0';
return len;
}
else
return -1;
}
// Find and Compare the RecordID fields inorder update the correct record
SHORT FindRecIdFileLoc(INT *hf, CARDINFO *CardInfo, DATAFILE *FileD)
{
CHAR str[1], currRec[9];
SHORT rc=0, line=0, i=0, len=0;
len = GetRecID(hf, currRec, len);
if (len == -1)
return -1;
if (strcmp(currRec, CardInfo->RecId) == 0) {
rc = UpdateFile(hf, CardInfo, len, FileD);
if (rc)
return 1;
return 0;
}
/* find Next User Id line */
while (!eof(*hf)) {
while (!eof(*hf) && (line < 15) && (i < 101)) {
rc = read(*hf, str, 1);
if (rc == -1)
return rc;
i++;
len++;
if (str[0] == '\n') {
line++;
i=0;
}
}
if (i >= 101)
return 1;
if (!eof(*hf)) {
/* find User Id */
len = GetRecID(hf, currRec, len);
if (len == -1)
return -1;
if (strcmp(currRec, CardInfo->RecId) == 0) {
rc = UpdateFile(hf, CardInfo, len, FileD);
if (rc)
return 1;
return 0;
}
line=0;
i=0;
}
else /* entry was not found */
return 1;
}
return 1; /* entry was not found */
}
// read in the record id field in the .dat file
SHORT GetRecID(INT *hf, CHAR *RecId, USHORT len)
{
CHAR str[1];
SHORT rc=0, i=0;
/* find User Id */
while (!eof(*hf) && (i < 9)) {
rc = read(*hf, str, 1);
if (rc == -1)
return rc;
len++;
if (str[0] != '\n')
RecId[i] = str[0];
else {
RecId[i] = '\0';
return len;
}
i++;
}
if (!eof(*hf)) {
RecId[i] = '\0';
return len;
}
else
return -1;
}
// actually update the .dat file
SHORT UpdateFile(INT *hf, CARDINFO *CardInfo, USHORT len, DATAFILE *FileD)
{
CHAR currRec[9], *Data;
SHORT i=0, rc=0, lines=0;
LONG pos=0;
write(*hf, CardInfo->LName, strlen(CardInfo->LName));
write(*hf, "\n", 1);
write(*hf, CardInfo->FName, strlen(CardInfo->FName));
write(*hf, "\n", 1);
write(*hf, CardInfo->Initial, strlen(CardInfo->Initial));
write(*hf, "\n", 1);
write(*hf, CardInfo->Company, strlen(CardInfo->Company));
write(*hf, "\n", 1);
write(*hf, CardInfo->Division, strlen(CardInfo->Division));
write(*hf, "\n", 1);
write(*hf, CardInfo->StrAddr, strlen(CardInfo->StrAddr));
write(*hf, "\n", 1);
write(*hf, CardInfo->City, strlen(CardInfo->City));
write(*hf, "\n", 1);
write(*hf, CardInfo->State, strlen(CardInfo->State));
write(*hf, "\n", 1);
write(*hf, CardInfo->Zip, strlen(CardInfo->Zip));
write(*hf, "\n", 1);
write(*hf, CardInfo->Phone, strlen(CardInfo->Phone));
write(*hf, "\n", 1);
write(*hf, CardInfo->Fax, strlen(CardInfo->Fax));
write(*hf, "\n", 1);
write(*hf, CardInfo->PagerID, strlen(CardInfo->PagerID));
write(*hf, "\n", 1);
write(*hf, CardInfo->MailUserId, strlen(CardInfo->MailUserId));
write(*hf, "\n", 1);
write(*hf, CardInfo->MailNodeId, strlen(CardInfo->MailNodeId));
rc = write(*hf, "\n", 1);
if (rc == -1)
return 1;
/* append the rest of the old file on end */
while(len != FileD->TotLen)
len++;
Data = &(FileD->FileData[len]);
rc = write(*hf, Data, (FileD->TotLen - len));
if (rc == -1)
return 1;
/* force and End of File */
pos = tell(*hf);
chsize(*hf, pos);
return 0;
}
// create a brand new .dat file
SHORT CreateNewFile(INT *hf, CARDINFO *CardInfo)
{
CHAR currRec[9], *Data;
SHORT i=0, rc=0, lines=0;
LONG pos=0;
DATETIME TimeData;
DosGetDateTime(&TimeData);
sprintf(CardInfo->RecId, "%d%d%d%d", TimeData.day, TimeData.month,
TimeData.hours, TimeData.minutes);
write(*hf, CardInfo->RecId, strlen(CardInfo->RecId));
write(*hf, "\n", 1);
write(*hf, CardInfo->LName, strlen(CardInfo->LName));
write(*hf, "\n", 1);
write(*hf, CardInfo->FName, strlen(CardInfo->FName));
write(*hf, "\n", 1);
write(*hf, CardInfo->Initial, strlen(CardInfo->Initial));
write(*hf, "\n", 1);
write(*hf, CardInfo->Company, strlen(CardInfo->Company));
write(*hf, "\n", 1);
write(*hf, CardInfo->Division, strlen(CardInfo->Division));
write(*hf, "\n", 1);
write(*hf, CardInfo->StrAddr, strlen(CardInfo->StrAddr));
write(*hf, "\n", 1);
write(*hf, CardInfo->City, strlen(CardInfo->City));
write(*hf, "\n", 1);
write(*hf, CardInfo->State, strlen(CardInfo->State));
write(*hf, "\n", 1);
write(*hf, CardInfo->Zip, strlen(CardInfo->Zip));
write(*hf, "\n", 1);
write(*hf, CardInfo->Phone, strlen(CardInfo->Phone));
write(*hf, "\n", 1);
write(*hf, CardInfo->Fax, strlen(CardInfo->Fax));
write(*hf, "\n", 1);
write(*hf, CardInfo->PagerID, strlen(CardInfo->PagerID));
write(*hf, "\n", 1);
write(*hf, CardInfo->MailUserId, strlen(CardInfo->MailUserId));
write(*hf, "\n", 1);
write(*hf, CardInfo->MailNodeId, strlen(CardInfo->MailNodeId));
rc = write(*hf, "\n", 1);
if (rc == -1)
return 1;
/* force and End of File */
pos = tell(*hf);
chsize(*hf, pos);
return 0;
}
// prepare to delete a record out of the .dat file
VOID PrepDelRec(CHAR *RecId)
{
DATAFILE *FileD;
CHAR file[45];
INT hf;
SHORT rc;
FileD = (DATAFILE *)malloc(sizeof(DATAFILE));
FileD->FileData = (CHAR *)malloc(FILE_MAXSIZE+1);
sprintf(file, "%s\\book.dat", BookPath);
hf = open(file, O_RDONLY);
if (hf == -1 || hf == 0)
return;
FileD->TotLen = read(hf, FileD->FileData, FILE_MAXSIZE);
close(hf);
hf = open(file, O_RDWR);
rc = FindFileSec(&hf, FileD, RecId);
if (rc || hf == -1 || hf == 0)
return;
// erase the .bmp
sprintf(file, "%s\\%s.bmp", BookPath, RecId);
unlink(file);
free(FileD->FileData);
free(FileD);
}
// Compare the RecordID fields inorder delete the correct record
SHORT FindFileSec(INT *hf, DATAFILE *FileD, CHAR *RecId)
{
CHAR str[1], currRec[9];
SHORT rc=0, line=0, i=0, len=0, tmp_len;
LONG pos;
/* know the location before the user's ID */
tmp_len=len;
pos = tell(*hf);
/* find User Id */
len = GetRecID(hf, currRec, len);
if (len == -1)
return -1;
if (strcmp(currRec, RecId) == 0) {
rc = DelFile(hf, tmp_len, pos, FileD);
if (rc)
return 1;
return 0;
}
/* find Next Record Id line */
while (!eof(*hf)) {
while (!eof(*hf) && (line < 15) && (i < 101)) {
rc = read(*hf, str, 1);
if (rc == -1)
return rc;
i++;
len++;
if (str[0] == '\n') {
line++;
i=0;
}
}
if (i >= 101)
return 1;
if (!eof(*hf)) {
/* know the location before the RecordID */
tmp_len=len;
pos = tell(*hf);
/* find Record Id */
len = GetRecID(hf, currRec, len);
if (len == -1)
return -1;
if (strcmp(currRec, RecId) == 0) {
rc = DelFile(hf, tmp_len, pos, FileD);
if (rc)
return 1;
return 0;
}
line=0;
i=0;
}
else /* entry was not found */
return 1;
}
return 1; /* entry was not found */
}
// actually delete a section of the .dat file inorder to remove a record
SHORT DelFile(INT *hf, USHORT len, LONG old_pos, DATAFILE *FileD)
{
CHAR *Data;
SHORT rc=0, i=0, line=0;
LONG pos=0;
/* find Next User Id line */
while ((FileD->TotLen > len) && (line < 15) && (i < 101)) {
i++;
len++;
if (FileD->FileData[len] == '\n') {
line++;
i=0;
}
}
if (i > 101)
return 1;
/* get past the last new line */
len++;
/* dont add any thing to the end of file just close it */
if (FileD->TotLen <= len) {
chsize(*hf, old_pos);
}
else {
/* move back to position before the user's ID */
lseek(*hf, old_pos, SEEK_SET);
Data = &(FileD->FileData[len]);
rc = write(*hf, Data, (FileD->TotLen - len));
if (rc == -1)
return 1;
/* force and End of File */
pos = tell(*hf);
chsize(*hf, pos);
}
close(*hf);
return 0;
}