st.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <stdio.h>
  2. /* function implementations */
  3. /* checks char against all elements of a char array */
  4. /* returns 1 on successful match, 0 otherwise */
  5. int in_array(char* array, char c) {
  6. char* arrayp = array;
  7. while (*arrayp != '\0') {
  8. if (*arrayp == c) return 1;
  9. arrayp++;
  10. }
  11. return 0;
  12. }
  13. /* probes line to check whether it contains a specific character or not */
  14. /* returns matched character on success, -1 otherwise */
  15. char probe_line(FILE* in, char* array, long* read) {
  16. long offset = ftell(in);
  17. char ret = -1;
  18. char c;
  19. while (!feof(in) && c != '\n') {
  20. c = getc(in);
  21. if (in_array(array, c)) {
  22. ret = c;
  23. break;
  24. }
  25. if (c != ' ') break;
  26. }
  27. *read = ftell(in);
  28. fseek(in, offset, SEEK_SET);
  29. //printf("%li off %li read %c ret\n", offset, *read, ret);
  30. return ret;
  31. }
  32. int seek_char(FILE* in, char s) {
  33. long offset = ftell(in);
  34. char c = getc(in);
  35. int chars = 1;
  36. while(!feof(in) && c != s) {
  37. c = getc(in); ++chars;
  38. }
  39. fseek(in, offset, SEEK_SET);
  40. //printf("%li off %i chars\n", offset, chars);
  41. return chars;
  42. }
  43. void skip(FILE* in, char s) {
  44. char c = getc(in);
  45. while(!feof(in) && c != s) {
  46. c = getc(in);
  47. }
  48. }