rulex

Форк
0
/
rulex.c 
174 строки · 4.1 Кб
1
/* Simple text markup utility */
2

3
/*
4
 * Copyright (C) 2012 Igor B. Poretsky <poretsky@mlbox.ru>
5
 * 
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 * 
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 */
16

17

18
#include <unistd.h>
19
#include <stdlib.h>
20
#include <string.h>
21
#include <stdio.h>
22
#include <locale.h>
23
#include <ctype.h>
24
#include <limits.h>
25
#include <sys/types.h>
26
#include <sys/stat.h>
27
#include <fcntl.h>
28

29
#include "lexdb.h"
30

31

32
#ifdef _WIN32
33
static const char *charset = "Russian_Russia.20866";
34
#else
35
static const char *charset = "ru_RU.koi8r";
36
#endif
37

38
static const char symbols[] =
39
  {
40
    '+', '=',
41
    0xC1, 0xC2, 0xD7, /* а, б, в, */
42
    0xC7, 0xC4, 0xC5, /* г, д, е, */
43
    0xA3, 0xD6, 0xDA, /* ё, ж, з, */
44
    0xC9, 0xCA, 0xCB, /* и, й, к, */
45
    0xCC, 0xCD, 0xCE, /* л, м, н, */
46
    0xCF, 0xD0, 0xD2, /* о, п, р, */
47
    0xD3, 0xD4, 0xD5, /* с, т, у, */
48
    0xC6, 0xC8, 0xC3, /* ф, х, ц, */
49
    0xDE, 0xDB, 0xDD, /* ч, ш, щ, */
50
    0xDF, 0xD9, 0xD8, /* ъ, ы, ь, */
51
    0xDC, 0xC0, 0xD1, /* э, ю, я */
52
    0
53
  };
54

55
static const char *alphabet;
56

57
static void *xrealloc(void *p, unsigned int n)
58
{
59
  void *u = realloc(p, n);
60
  if (u) return u;
61
  else
62
    {
63
      perror("Memory allocation error");
64
      exit(EXIT_FAILURE);
65
    }
66
}
67

68
static void *xmalloc(unsigned int n)
69
{
70
  void *u = NULL;
71
  return xrealloc(u, n);
72
}
73

74
int main(int argc, char **argv)
75
{
76
  unsigned int size = 64;
77
  char *s, *text;
78
  FILE *slog = NULL;
79
  RULEXDB *db = NULL;
80

81
  if (argc < 2)
82
    {
83
      fprintf(stderr, "Usage:\n");
84
      fprintf(stderr, "%s <rulex_db> [<logfile>]\n", argv[0]);
85
      return EXIT_FAILURE;
86
    }
87

88
  alphabet = symbols + 2;
89
  if (!setlocale(LC_CTYPE, charset))
90
    {
91
      fprintf(stderr, "Cannot set %s locale.\n", charset);
92
      fprintf(stderr, "Probably you have to generate it by command:\n");
93
      fprintf(stderr, "localedef -f KOI8-R -i ru_RU ru_RU.KOI8-R\n");
94
      return EXIT_FAILURE;
95
    }
96

97
  db = rulexdb_open(argv[1], RULEXDB_SEARCH);
98
  if (!db)
99
    {
100
      perror(argv[1]);
101
      return EXIT_FAILURE;
102
    }
103

104
  if (argc > 2)
105
    {
106
      slog = fopen(argv[2], "a");
107
      if (!slog)
108
        perror(argv[2]);
109
    }
110

111
  /* doing text filtering in the loop */
112
  text = xmalloc(size);
113
  s = text;
114
  while (fgets(s, text + size - s, stdin))
115
    {
116
      s = text + strlen(text) - 1;
117
      if (*s == '\n')
118
	{
119
          char *stressed = xmalloc(strlen(text) << 1);
120
          char *t;
121
          unsigned int n;
122
          for (s = text; *s; s++)
123
            if (isupper(*s))
124
              *s = tolower(*s);
125
          s = text;
126
          t = stressed;
127
          while (*s)
128
            {
129
              if ((n = strcspn(s, symbols)))
130
                {
131
                  strncpy(t, s, n);
132
                  s += n;
133
                  t += n;
134
                }
135
              if ((n = strspn(s, symbols)))
136
                {
137
                  if ((n <= RULEXDB_MAX_KEY_SIZE) && (n <= strspn(s, alphabet)))
138
                    {
139
                      char *key = xmalloc(RULEXDB_BUFSIZE);
140
                      strncpy(key, s, n);
141
                      key[n] = 0;
142
                      if (rulexdb_search(db, key, t, 0)
143
                          == RULEXDB_SPECIAL)
144
                        if (slog) (void)fprintf(slog, "%s\n", key);
145
                      free(key);
146
                    }
147
                  else
148
                    {
149
                      strncpy(t, s, n);
150
                      t[n] = 0;
151
                    }
152
                  s += n;
153
                  t += strlen(t);
154
                }
155
            }
156
          *t = 0;
157
          puts(stressed);
158
          free(stressed);
159
	  s = text;
160
	}
161
      else
162
	{
163
	  text = xrealloc(text, size <<= 1);
164
	  s = text + strlen(text);
165
	}
166
    }
167

168
  rulexdb_close(db);
169
  if (slog)
170
    fclose(slog);
171
  free(text);
172

173
  return EXIT_SUCCESS;
174
}
175

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.