This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
magicka/jamlib/lastread.c

175 lines
4.5 KiB
C
Raw Normal View History

2016-03-22 02:08:00 +00:00
/*
2016-03-25 00:58:57 +00:00
JAMLIB - A JAM subroutine library
Copyright (C) 1999 Bj<EFBFBD>rn Stenberg
2016-03-22 02:08:00 +00:00
2016-03-25 00:58:57 +00:00
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
2016-03-22 02:08:00 +00:00
2016-03-25 00:58:57 +00:00
This library 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
Lesser General Public License for more details.
2016-03-22 02:08:00 +00:00
2016-03-25 00:58:57 +00:00
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2016-03-22 02:08:00 +00:00
2016-03-25 00:58:57 +00:00
Changes made by Johan Billing 2000-04-16:
- Changed source to use feof() instead of errno == EPASTEOF
- Changed source to use structrw to read and write structures
*/
2016-03-22 02:08:00 +00:00
/***********************************************************************
2016-03-25 00:58:57 +00:00
**
** LASTREAD.C -- Lastread pointer handling
**
** Author: Bj<EFBFBD>rn Stenberg (bjorn.stenberg@sth.frontec.se)
**
***********************************************************************/
2016-03-22 02:08:00 +00:00
#include <stdio.h>
#include <errno.h>
2016-03-25 00:58:57 +00:00
#include "jam.h"
2016-03-22 02:08:00 +00:00
#include "structrw.h"
/***********************************************************************
2016-03-25 00:58:57 +00:00
**
** File global variables
**
***********************************************************************/
/***********************************************************************
**
** JAM_ReadLastRead - Read LastRead record
**
***********************************************************************/
2016-03-22 02:08:00 +00:00
int JAM_ReadLastRead( s_JamBase* Base_PS,
2016-03-25 00:58:57 +00:00
uint32_t User_I,
s_JamLastRead* Record_PS )
2016-03-22 02:08:00 +00:00
{
s_JamLastRead Record_S;
int Pos_I;
if (!Record_PS)
return JAM_BAD_PARAM;
if ( fseek( Base_PS->LrdFile_PS, 0, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
for ( Pos_I = 0; ; Pos_I++ ) {
2016-03-25 00:58:57 +00:00
if ( 1 > freadjamlastread(Base_PS->LrdFile_PS,&Record_S) ) {
if ( feof(Base_PS->LrdFile_PS) )
2016-03-22 02:08:00 +00:00
return JAM_NO_USER;
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
if ( Record_S.UserID == User_I ) {
Base_PS->LastUserPos_I = Pos_I;
Base_PS->LastUserId_I = User_I;
*Record_PS = Record_S;
return 0;
}
}
return 0;
}
/***********************************************************************
2016-03-25 00:58:57 +00:00
**
** JAM_WriteLastRead - Write LastRead record
**
***********************************************************************/
int JAM_WriteLastRead( s_JamBase* Base_PS,
uint32_t User_I,
s_JamLastRead* Record_PS )
2016-03-22 02:08:00 +00:00
{
s_JamLastRead Record_S;
int Pos_I;
if (!Record_PS)
return JAM_BAD_PARAM;
2016-03-25 00:58:57 +00:00
/* if the last read is stored */
2016-03-22 02:08:00 +00:00
if ( User_I == Base_PS->LastUserId_I ) {
Pos_I = Base_PS->LastUserPos_I * sizeof( s_JamLastRead );
if ( fseek( Base_PS->LrdFile_PS, Pos_I, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* be safe, check it */
if ( 1 > freadjamlastread(Base_PS->LrdFile_PS,&Record_S) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
2016-03-25 00:58:57 +00:00
/* is it where we expected it to be? */
2016-03-22 02:08:00 +00:00
if ( User_I == Record_S.UserID ) {
2016-03-25 00:58:57 +00:00
2016-03-22 02:08:00 +00:00
if ( fseek( Base_PS->LrdFile_PS, Pos_I, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
2016-03-25 00:58:57 +00:00
if ( 1 > fwritejamlastread(Base_PS->LrdFile_PS,Record_PS) ) {
2016-03-22 02:08:00 +00:00
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
2016-03-25 00:58:57 +00:00
fflush(Base_PS -> LrdFile_PS);
2016-03-22 02:08:00 +00:00
return 0;
2016-03-25 00:58:57 +00:00
}
}
2016-03-22 02:08:00 +00:00
/* no last position, or position incorrect */
if ( fseek( Base_PS->LrdFile_PS, 0, SEEK_SET ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
for ( Pos_I = 0; ; Pos_I++ ) {
2016-03-25 00:58:57 +00:00
if ( 1 > freadjamlastread(Base_PS->LrdFile_PS,&Record_S) ) {
if ( feof(Base_PS->LrdFile_PS) ) {
2016-03-22 02:08:00 +00:00
/* user not in file, append a new record */
if ( fseek( Base_PS->LrdFile_PS, 0, SEEK_END ) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
break;
}
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
/* found the user? */
if ( Record_S.UserID == User_I ) {
2016-03-25 00:58:57 +00:00
if ( fseek( Base_PS->LrdFile_PS, Pos_I * sizeof(s_JamLastRead),
SEEK_SET ) ) {
2016-03-22 02:08:00 +00:00
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
break;
}
}
if ( 1 > fwritejamlastread(Base_PS->LrdFile_PS,Record_PS) ) {
Base_PS->Errno_I = errno;
return JAM_IO_ERROR;
}
fflush( Base_PS->LrdFile_PS );
return 0;
}