Blame onefile/word-splitter.c

a5e8d6
a5e8d6
a5e8d6
#include <ctype.h></ctype.h>
a5e8d6
#include <string.h></string.h>
a5e8d6
#include <stdio.h></stdio.h>
a5e8d6
#include <helianthus.h></helianthus.h>
a5e8d6
a5e8d6
a5e8d6
typedef struct {
a5e8d6
  int mode;
a5e8d6
  char c[4];
a5e8d6
} UChar;
a5e8d6
a5e8d6
char *ca[] = { "а", "е", "ё", "и", "о", "у", "ы", "э", "ю", "я",
a5e8d6
               "А", "Е", "Ё", "И", "О", "У", "Ы", "Э", "Ю", "Я" };
a5e8d6
char *cb[] = { "б", "в", "г", "д", "ж", "з", "й", "к", "л", "м", "н", "п", "р", "с", "т", "ф", "х", "ц", "ч", "ш", "щ",
a5e8d6
               "Б", "В", "Г", "Д", "Ж", "З", "Й", "К", "Л", "М", "Н", "П", "Р", "С", "Т", "Ф", "Х", "Ц", "Ч", "Ш", "Щ" };
a5e8d6
char *cc[] = { "ъ", "ь",
a5e8d6
               "Ъ", "Ь" };
a5e8d6
a5e8d6
a5e8d6
void splitWord(UChar *word, FILE *fo) {
a5e8d6
  UChar *pos = word;
a5e8d6
  UChar *pa = NULL;
a5e8d6
  int count = 0;
a5e8d6
  while(word->c[0]) {
a5e8d6
    if (word->mode == 1) {
a5e8d6
      if (pa) {
a5e8d6
        int cnt = count / 2;
a5e8d6
        while(pos <= pa) fputs((pos++)->c, fo);
a5e8d6
        while(cnt > 0) {
a5e8d6
          fputs((pos++)->c, fo);
a5e8d6
          if (pos->mode != 3) --cnt;
a5e8d6
        }
a5e8d6
        //fprintf(fo, "[%d]", count);
a5e8d6
        fputs("\u030D\u0329", fo);
a5e8d6
        //fputs("|", fo);
a5e8d6
      }
a5e8d6
      pa = word;
a5e8d6
      count = 0;
a5e8d6
    } else
a5e8d6
    if (pa && word->mode != 3) {
a5e8d6
      ++count;
a5e8d6
    }
a5e8d6
    ++word;
a5e8d6
  }
a5e8d6
  while(pos < word) fputs((pos++)->c, fo);
a5e8d6
}
a5e8d6
a5e8d6
a5e8d6
int findStr(char *str, char **list, int count) {
a5e8d6
  while(count-- > 0)
a5e8d6
    if (strcmp(str, *list++) == 0) return 1;
a5e8d6
  return FALSE;
a5e8d6
}
a5e8d6
a5e8d6
a5e8d6
void readUChar(UChar *uc, FILE *fi) {
a5e8d6
  memset(uc, 0, sizeof(*uc));
a5e8d6
  int c = fgetc(fi);
a5e8d6
  int cnt = 1;
a5e8d6
  if (c > 128 + 64 + 32 + 16) cnt = 4; else
a5e8d6
  if (c > 128 + 64 + 32) cnt = 3; else
a5e8d6
  if (c > 128 + 64) cnt = 2; else
a5e8d6
  if (c <= 0) return;
a5e8d6
a5e8d6
  uc->c[0] = c;
a5e8d6
  for(int i = 1; i < cnt; ++i)
a5e8d6
    uc->c[i] = fgetc(fi);
a5e8d6
a5e8d6
  if (findStr(uc->c, ca, sizeof(ca)/sizeof(*ca))) uc->mode = 1; else
a5e8d6
  if (findStr(uc->c, cb, sizeof(cb)/sizeof(*cb))) uc->mode = 2; else
a5e8d6
  if (findStr(uc->c, cc, sizeof(cc)/sizeof(*cc))) uc->mode = 3;
a5e8d6
  //printf("readUChar: m%d [%s], %d\n", uc->mode, uc->c, (int)uc->c[0]);
a5e8d6
}
a5e8d6
a5e8d6
a5e8d6
void splitFile(FILE *fi, FILE *fo) {
a5e8d6
  UChar word[1024];
a5e8d6
  UChar *pc = word, *pe = word + sizeof(word)/sizeof(*word), uc = {};
a5e8d6
  while(1) {
a5e8d6
    readUChar(&uc, fi);
a5e8d6
    if (uc.mode && pc < pe) {
a5e8d6
      memcpy(pc++, &uc, sizeof(uc));
a5e8d6
    } else {
a5e8d6
      if (pc > word) {
a5e8d6
        pc->c[0] = 0;
a5e8d6
        splitWord(word, fo);
a5e8d6
        pc = word;
a5e8d6
      }
a5e8d6
      if (!uc.c[0]) break;
a5e8d6
      fputs(uc.c, fo);
a5e8d6
    }
a5e8d6
  }
a5e8d6
}
a5e8d6
a5e8d6
a5e8d6
void init() {
a5e8d6
  FILE *fi = fopen("data/text-to-split.txt", "r");
a5e8d6
  FILE *fo = fopen("data/output/splitted-text.txt", "w");
a5e8d6
  if (fi && fo) splitFile(fi, fo);
a5e8d6
  if (fi) fclose(fi);
a5e8d6
  if (fo) fclose(fo);
a5e8d6
}
a5e8d6
a5e8d6
a5e8d6
void draw() {
a5e8d6
  text(10, 10, "loaded from: data/text-to-split.txt\n"
a5e8d6
               "saved to: data/output/splitted-text.txt");
a5e8d6
}
a5e8d6
a5e8d6
a5e8d6
int main() {
a5e8d6
  windowSetResizable(TRUE);
a5e8d6
  windowSetInit(&init);
a5e8d6
  windowSetDraw(&draw);
a5e8d6
  windowRun();
a5e8d6
  return 0;
a5e8d6
}
a5e8d6