Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- int file_eof(FILE *in) // could use "int *ch" to avoid seeking
- {
- if(!in) return -1;
- int c;
- c = fgetc(in);
- if(c == EOF) return 1;
- c = fseek(in, -1, SEEK_CUR);
- if(c != 0)
- return -2;
- return 0;
- }
- void file_eofHandle(FILE *in)
- {
- int c = file_eof(in);
- if(c < 0)
- {
- printf("[-] file_eof() [%d]\n", c);
- exit(1);
- }
- if(c == 1)
- exit(0); // should use fclose(in), but program termination closes it
- return;
- }
- int file_previousChar(FILE *in, int *pChar)
- {
- if(in == NULL || pChar == NULL)
- return -1;
- long fPos;
- fPos = ftell(in);
- if(fPos < 2)
- { *pChar = 0;
- return 0; }
- // blah i[]
- fseek(in, -2, SEEK_CUR); // blah[]i
- *pChar = fgetc(in); // blah[]i (gets space)
- fgetc(in); // blah [i] (gets i, leaves where it left off)
- return 1;
- }
- int file_nextChar(FILE *in, int *nChar)
- {
- if(in == NULL || nChar == NULL)
- return -1;
- *nChar = fgetc(in);
- int r = fseek(in, -1, SEEK_CUR);
- if(r != 0)
- return 0;
- return 1;
- }
- int word_isLonerI(FILE *in)
- {
- if(in == NULL)
- return -1;
- int pChar, nChar;
- int pC, nC;
- pC = file_previousChar(in, &pChar);
- if(pC < 1)
- return 0;
- nC = file_nextChar(in, &nChar);
- if(nC < 1)
- return -2;
- pC = isalpha(pChar);
- if(pC == 0)
- {
- nC = isalpha(nChar);
- if(nC == 0)
- return 1;
- }
- return 0;
- }
- int main(int argc, char **argv)
- {
- FILE *in, *out;
- int c, r;
- int sBegin = 1; // beginning of sentence
- if(argc < 2)
- return 1;
- in = fopen(argv[1], "r");
- if(!in)
- return 2;
- out = stdout; // there's no need to write to a file
- // that's what piping is for
- // if using *nix system file descriptor, then stdout is 1
- while(1)
- {
- file_eofHandle(in);
- c = fgetc(in);
- if(c == '.')
- sBegin = 1;
- if(isalpha(c) != 0) // using if((c >= 0xwhatever && c <= 0xwhatever) ||
- { // (blah >= lakjsdflkj...)) is harder to read
- // and a function should be used anyway
- if(sBegin != 0)
- {
- c = toupper(c);
- fputc(c, out);
- sBegin = 0;
- }
- else if(c == 'i')
- {
- r = word_isLonerI(in);
- if(r < 0)
- {
- fprintf(stderr, "[-] seeking failed\n");
- exit(1);
- }
- if(r == 1)
- fputc((int) 'I', out);
- else
- fputc((int) 'i', out);
- }
- else
- fputc(c, out);
- }
- else
- fputc(c, out);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement