txtfile.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "txtfile.h"
  16. #if 0
  17. char fileBuffer[FILE_BUFFER_SIZE + 1];
  18. char *fbCurrent;
  19. int fbSize;
  20. int fbStatus;
  21. #endif
  22. TEXTFILE * OpenTextFile(char *filename, int access)
  23. {
  24. TEXTFILE *txtfile;
  25. FILE *file;
  26. int status;
  27. if (access == TEXT_OPEN_FOR_WRITE) {
  28. status = TEXT_FILE_WRITING;
  29. file = fopen(filename, "w+");
  30. }
  31. else {
  32. status = TEXT_FILE_READING;
  33. file = fopen(filename, "r");
  34. }
  35. if (file == NULL)
  36. return NULL;
  37. txtfile = (TEXTFILE *) malloc (sizeof(TEXTFILE));
  38. memset(txtfile, 0, sizeof(TEXTFILE));
  39. txtfile->file = file;
  40. txtfile->fbStatus = status;
  41. txtfile->fbCurrent = txtfile->fileBuffer;
  42. *txtfile->fbCurrent = '\0';
  43. txtfile->fbSize = 0;
  44. return txtfile;
  45. }
  46. void CloseTextFile(TEXTFILE *txtfile)
  47. {
  48. if (txtfile) {
  49. fclose(txtfile->file);
  50. free(txtfile);
  51. }
  52. }
  53. int FillTextBuffer(TEXTFILE *txtfile)
  54. {
  55. int nLeft, size;
  56. nLeft = strlen(txtfile->fbCurrent);
  57. memcpy(txtfile->fileBuffer, txtfile->fbCurrent, nLeft+1);
  58. size = fread(txtfile->fileBuffer + nLeft, 1, FILE_BUFFER_SIZE - nLeft, txtfile->file);
  59. if (size == 0)
  60. return 0;
  61. txtfile->fbCurrent = txtfile->fileBuffer;
  62. *(txtfile->fbCurrent + size + nLeft) = '\0';
  63. txtfile->fbSize = size + nLeft;
  64. return size;
  65. }
  66. int ReadTextLine(TEXTFILE *txtfile, char *linebuf)
  67. {
  68. char *p, *q;
  69. if (txtfile->fbStatus == TEXT_FILE_DONE)
  70. return -1;
  71. p = txtfile->fbCurrent;
  72. q = strchr(p, '\n');
  73. if (q)
  74. {
  75. *q = '\0';
  76. strcpy(linebuf, p);
  77. txtfile->fbCurrent = q + 1;
  78. return strlen(linebuf);
  79. }
  80. else
  81. {
  82. if (FillTextBuffer(txtfile) == 0)
  83. { /* Done with file reading,
  84. return last line
  85. */
  86. txtfile->fbStatus = TEXT_FILE_DONE;
  87. if (*txtfile->fbCurrent) {
  88. strcpy(linebuf, txtfile->fbCurrent);
  89. CloseTextFile(txtfile);
  90. return strlen(linebuf);
  91. }
  92. else {
  93. CloseTextFile(txtfile);
  94. return -1;
  95. }
  96. }
  97. else {
  98. p = txtfile->fbCurrent;
  99. q = strchr(p, '\n');
  100. if (q)
  101. {
  102. *q = '\0';
  103. strcpy(linebuf, p);
  104. txtfile->fbCurrent = q + 1;
  105. }
  106. else
  107. {
  108. strcpy(linebuf, txtfile->fbCurrent);
  109. txtfile->fbCurrent = txtfile->fbCurrent + strlen(linebuf);
  110. }
  111. }
  112. return strlen(linebuf);
  113. }
  114. }