lastread.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. JAMLIB - A JAM subroutine library
  3. Copyright (C) 1999 Björn Stenberg
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Changes made by Johan Billing 2000-04-16:
  16. - Changed source to use feof() instead of errno == EPASTEOF
  17. - Changed source to use structrw to read and write structures
  18. */
  19. /***********************************************************************
  20. **
  21. ** LASTREAD.C -- Lastread pointer handling
  22. **
  23. ** Author: Bj”rn Stenberg ([email protected])
  24. **
  25. ***********************************************************************/
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include "jam.h"
  29. #include "structrw.h"
  30. /***********************************************************************
  31. **
  32. ** File global variables
  33. **
  34. ***********************************************************************/
  35. /***********************************************************************
  36. **
  37. ** JAM_ReadLastRead - Read LastRead record
  38. **
  39. ***********************************************************************/
  40. int JAM_ReadLastRead( s_JamBase* Base_PS,
  41. uint32_t User_I,
  42. s_JamLastRead* Record_PS )
  43. {
  44. s_JamLastRead Record_S;
  45. int Pos_I;
  46. if (!Record_PS)
  47. return JAM_BAD_PARAM;
  48. if ( fseek( Base_PS->LrdFile_PS, 0, SEEK_SET ) ) {
  49. Base_PS->Errno_I = errno;
  50. return JAM_IO_ERROR;
  51. }
  52. for ( Pos_I = 0; ; Pos_I++ ) {
  53. if ( 1 > freadjamlastread(Base_PS->LrdFile_PS,&Record_S) ) {
  54. if ( feof(Base_PS->LrdFile_PS) )
  55. return JAM_NO_USER;
  56. Base_PS->Errno_I = errno;
  57. return JAM_IO_ERROR;
  58. }
  59. if ( Record_S.UserID == User_I ) {
  60. Base_PS->LastUserPos_I = Pos_I;
  61. Base_PS->LastUserId_I = User_I;
  62. *Record_PS = Record_S;
  63. return 0;
  64. }
  65. }
  66. return 0;
  67. }
  68. /***********************************************************************
  69. **
  70. ** JAM_WriteLastRead - Write LastRead record
  71. **
  72. ***********************************************************************/
  73. int JAM_WriteLastRead( s_JamBase* Base_PS,
  74. uint32_t User_I,
  75. s_JamLastRead* Record_PS )
  76. {
  77. s_JamLastRead Record_S;
  78. int Pos_I;
  79. if (!Record_PS)
  80. return JAM_BAD_PARAM;
  81. /* if the last read is stored */
  82. if ( User_I == Base_PS->LastUserId_I ) {
  83. Pos_I = Base_PS->LastUserPos_I * sizeof( s_JamLastRead );
  84. if ( fseek( Base_PS->LrdFile_PS, Pos_I, SEEK_SET ) ) {
  85. Base_PS->Errno_I = errno;
  86. return JAM_IO_ERROR;
  87. }
  88. /* be safe, check it */
  89. if ( 1 > freadjamlastread(Base_PS->LrdFile_PS,&Record_S) ) {
  90. Base_PS->Errno_I = errno;
  91. return JAM_IO_ERROR;
  92. }
  93. /* is it where we expected it to be? */
  94. if ( User_I == Record_S.UserID ) {
  95. if ( fseek( Base_PS->LrdFile_PS, Pos_I, SEEK_SET ) ) {
  96. Base_PS->Errno_I = errno;
  97. return JAM_IO_ERROR;
  98. }
  99. if ( 1 > fwritejamlastread(Base_PS->LrdFile_PS,Record_PS) ) {
  100. Base_PS->Errno_I = errno;
  101. return JAM_IO_ERROR;
  102. }
  103. fflush(Base_PS -> LrdFile_PS);
  104. return 0;
  105. }
  106. }
  107. /* no last position, or position incorrect */
  108. if ( fseek( Base_PS->LrdFile_PS, 0, SEEK_SET ) ) {
  109. Base_PS->Errno_I = errno;
  110. return JAM_IO_ERROR;
  111. }
  112. for ( Pos_I = 0; ; Pos_I++ ) {
  113. if ( 1 > freadjamlastread(Base_PS->LrdFile_PS,&Record_S) ) {
  114. if ( feof(Base_PS->LrdFile_PS) ) {
  115. /* user not in file, append a new record */
  116. if ( fseek( Base_PS->LrdFile_PS, 0, SEEK_END ) ) {
  117. Base_PS->Errno_I = errno;
  118. return JAM_IO_ERROR;
  119. }
  120. break;
  121. }
  122. Base_PS->Errno_I = errno;
  123. return JAM_IO_ERROR;
  124. }
  125. /* found the user? */
  126. if ( Record_S.UserID == User_I ) {
  127. if ( fseek( Base_PS->LrdFile_PS, Pos_I * sizeof(s_JamLastRead),
  128. SEEK_SET ) ) {
  129. Base_PS->Errno_I = errno;
  130. return JAM_IO_ERROR;
  131. }
  132. break;
  133. }
  134. }
  135. if ( 1 > fwritejamlastread(Base_PS->LrdFile_PS,Record_PS) ) {
  136. Base_PS->Errno_I = errno;
  137. return JAM_IO_ERROR;
  138. }
  139. fflush( Base_PS->LrdFile_PS );
  140. return 0;
  141. }