home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gold Fish 2
/
goldfish_vol2_cd1.bin
/
files
/
comm
/
misc
/
cyberpager
/
source
/
library
/
sequence.c
< prev
next >
Wrap
Text File
|
1994-02-07
|
3KB
|
117 lines
/*
* return the next sequence number. if the value returned is negative then
* an error occured while trying to obtain the next sequence number.
*/
LONG __saveds __asm GetSequenceNumber(register __a0 PagerHandle_t * ph, register __d0 UWORD increment)
{
BPTR fh;
BPTR lock;
LONG seq;
ULONG seqTemp;
UBYTE buffer[20];
STRPTR sequenceFileName;
seq = -1;
if (sequenceFileName = NameInSpool(ph, "sequence")) {
if (LockFile(ph, sequenceFileName)) {
/*
* we need to determine if the sequence file exists.
* we do this in the following manner: initialize
* buffer to be a null string. then try and Lock()
* the sequence file. if it locks, UnLock() it and
* continue on. If it does not lock, see if IoErr()
* is telling us that the file doesn't exist. If it
* doesn't exist, put an ASCII "1" into buffer.
* otherwise leave buffer as a null string. then we
* try opening the sequence file in READWRITE mode.
* this should create the file for us if it doesn't
* exist. if the lock failed and it wasn't because
* the file didn't exist then the Open() will most
* likely fail and the user will get the error
* message that we couldn't open the sequence file.
* after we open the sequence file, we check to see
* if buffer is still a null string. if it is null,
* try reading the old value from the sequence file,
* reporting an error if we fail to read the file
* properly. if buffer is not null (has the "1" we
* copied into it when the file doesn't exist) then
* we don't try to read from the file, but rather
* fall through to writing the updated sequence
* number to the file.
*/
buffer[0] = '\0';
if (lock = Lock(sequenceFileName, ACCESS_READ))
UnLock(lock);
else if (IoErr() == ERROR_OBJECT_NOT_FOUND)
strcpy(buffer, "1");
if (fh = Open(sequenceFileName, MODE_READWRITE)) {
if (buffer[0] || FGets(fh, buffer, sizeof(buffer) - 1)) {
seqTemp = atol(buffer) + increment;
if (seqTemp > 0x3FFFFFFF)
seqTemp = increment;
if (Seek(fh, 0, OFFSET_BEGINNING) != -1) {
if (FPrintf(fh, "%ld\n", seqTemp) == -1)
ULog(ph, -1, "Error writing new sequence value.");
else
seq = seqTemp - increment;
}
else
ULog(ph, -1, "Error writing new sequence value.");
}
else
ULog(ph, -1, "Error reading old sequence value.");
Close(fh);
}
else
ULog(ph, -1, "Couldn't open sequence file.");
UnLockFile(ph, sequenceFileName);
}
else
ULog(ph, -1, "Couldn't open sequence file.");
FreeNameInSpool(sequenceFileName);
}
return seq;
}
/*
* convert a sequence number in a numeric/character combination. Names are
* unique and case in-sensitive. Each position in the 6 character name will
* be 0-9 a-z, or base 36. The input buffer must be at least 7 bytes in
* length. a pointer to this buffer is returned on exit.
*/
STRPTR __saveds __asm SequenceToName(register __a0 STRPTR buffer, register __d0 LONG sequenceNumber)
{
int i, j;
sequenceNumber &= 0x3FFFFFFF;
for (i = 5; i != -1; i--) {
j = sequenceNumber % 36;
if (j < 10)
buffer[i] = j + '0';
else
buffer[i] = j + 'a' - 10;
sequenceNumber /= 36;
}
buffer[6] = 0;
return buffer;
}