home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / comm / cyberpager-1.5.lha / CyberPager / source / library / sequence.c < prev    next >
Text File  |  1994-02-07  |  3KB  |  117 lines

  1.  /*
  2.   * return the next sequence number.  if the value returned is negative then
  3.   * an error occured while trying to obtain the next sequence number.
  4.   */
  5.  
  6. LONG __saveds __asm GetSequenceNumber(register __a0 PagerHandle_t * ph, register __d0 UWORD increment)
  7. {
  8.     BPTR fh;
  9.     BPTR lock;
  10.     LONG seq;
  11.     ULONG seqTemp;
  12.     UBYTE buffer[20];
  13.     STRPTR sequenceFileName;
  14.  
  15.     seq = -1;
  16.  
  17.     if (sequenceFileName = NameInSpool(ph, "sequence")) {
  18.         if (LockFile(ph, sequenceFileName)) {
  19.  
  20.             /*
  21.              * we need to determine if the sequence file exists.
  22.              * we do this in the following manner:  initialize
  23.              * buffer to be a null string.  then try and Lock()
  24.              * the sequence file.  if it locks, UnLock() it and
  25.              * continue on.  If it does not lock, see if IoErr()
  26.              * is telling us that the file doesn't exist.  If it
  27.              * doesn't exist, put an ASCII "1" into buffer.
  28.              * otherwise leave buffer as a null string.  then we
  29.              * try opening the sequence file in READWRITE mode.
  30.              * this should create the file for us if it doesn't
  31.              * exist.  if the lock failed and it wasn't because
  32.              * the file didn't exist then the Open() will most
  33.              * likely fail and the user will get the error
  34.              * message that we couldn't open the sequence file.
  35.              * after we open the sequence file, we check to see
  36.              * if buffer is still a null string.  if it is null,
  37.              * try reading the old value from the sequence file,
  38.              * reporting an error if we fail to read the file
  39.              * properly.  if buffer is not null (has the "1" we
  40.              * copied into it when the file doesn't exist) then
  41.              * we don't try to read from the file, but rather
  42.              * fall through to writing the updated sequence
  43.              * number to the file.
  44.              */
  45.  
  46.             buffer[0] = '\0';
  47.  
  48.             if (lock = Lock(sequenceFileName, ACCESS_READ))
  49.                 UnLock(lock);
  50.             else if (IoErr() == ERROR_OBJECT_NOT_FOUND)
  51.                 strcpy(buffer, "1");
  52.  
  53.             if (fh = Open(sequenceFileName, MODE_READWRITE)) {
  54.                 if (buffer[0] || FGets(fh, buffer, sizeof(buffer) - 1)) {
  55.                     seqTemp = atol(buffer) + increment;
  56.  
  57.                     if (seqTemp > 0x3FFFFFFF)
  58.                         seqTemp = increment;
  59.  
  60.                     if (Seek(fh, 0, OFFSET_BEGINNING) != -1) {
  61.                         if (FPrintf(fh, "%ld\n", seqTemp) == -1)
  62.                             ULog(ph, -1, "Error writing new sequence value.");
  63.                         else
  64.                             seq = seqTemp - increment;
  65.                     }
  66.                     else
  67.                         ULog(ph, -1, "Error writing new sequence value.");
  68.  
  69.                 }
  70.                 else
  71.                     ULog(ph, -1, "Error reading old sequence value.");
  72.  
  73.                 Close(fh);
  74.             }
  75.             else
  76.                 ULog(ph, -1, "Couldn't open sequence file.");
  77.  
  78.             UnLockFile(ph, sequenceFileName);
  79.         }
  80.         else
  81.             ULog(ph, -1, "Couldn't open sequence file.");
  82.  
  83.         FreeNameInSpool(sequenceFileName);
  84.     }
  85.  
  86.     return seq;
  87. }
  88.  
  89.  /*
  90.   * convert a sequence number in a numeric/character combination.  Names are
  91.   * unique and case in-sensitive.  Each position in the 6 character name will
  92.   * be 0-9 a-z, or base 36. The input buffer must be at least 7 bytes in
  93.   * length.  a pointer to this buffer is returned on exit.
  94.   */
  95.  
  96. STRPTR __saveds __asm SequenceToName(register __a0 STRPTR buffer, register __d0 LONG sequenceNumber)
  97. {
  98.     int i, j;
  99.  
  100.     sequenceNumber &= 0x3FFFFFFF;
  101.  
  102.     for (i = 5; i != -1; i--) {
  103.         j = sequenceNumber % 36;
  104.  
  105.         if (j < 10)
  106.             buffer[i] = j + '0';
  107.         else
  108.             buffer[i] = j + 'a' - 10;
  109.  
  110.         sequenceNumber /= 36;
  111.     }
  112.  
  113.     buffer[6] = 0;
  114.  
  115.     return buffer;
  116. }
  117.