fast simple dictionary generator
[misc.git] / main.cpp
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <iconv.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <stdlib.h>
7
8 int main(int argc, char **argv)
9 {
10         int opt = -1, min_len = 0, max_len = 0;
11         char ofile[256], fchar[2], tchar[2];
12         ofile[0] = 0;
13         fchar[0] = 0;
14         tchar[0] = 0;
15         while((opt = getopt(argc, argv, "o:f:t:l:L:")) != -1)
16         {
17                 switch(opt)
18                 {
19                         case 'o':
20                         strncpy(ofile, optarg, 255);
21                         break;
22                         case 'f':
23                         strncpy(fchar, optarg, 1);
24                         break;
25                         case 't':
26                         strncpy(tchar, optarg, 1);
27                                                 break;
28                                                 case 'l':
29                                                 min_len = atoi(optarg);
30                         break;
31                                                 case 'L':
32                                                 max_len = atoi(optarg);
33                                                 break;
34                         default:
35                         printf("usage: %s -oftlL\n\t-o output file\n\t-f from char\n\t-t to char\n\t-l min seq len\n\t-L max seq len", argv[0]);
36                 }
37         }
38         if(max_len < 1)
39         {
40                 printf("error, length must be more than 0\n");
41                 return 1;
42         }
43         if(min_len > max_len)
44         {
45                 printf("error, max length must be greater or equal min_length\n");
46                 return 1;
47         }
48         if((int)fchar[0] > (int)tchar[0])
49         {
50                 printf("error, invalid range specified\n");
51                 return 1;
52         }
53         FILE *out = fopen(ofile, "w");
54         if(!out)
55         {
56                 printf("failed to open input file with error: %s\n", strerror(errno));
57                 return 1;
58         }
59         int cur_len = min_len;
60         while(cur_len <= max_len)
61         {
62                 char buf[cur_len];
63                 for(int i = 0; i < cur_len; i++)
64                         buf[i] = fchar[0];
65                 fwrite(buf, cur_len, 1, out);
66                 fwrite("\n", 1, 1, out);
67                 while(buf[0] != (tchar[0]+1))
68                 {
69                         while(buf[cur_len-1] < tchar[0])
70                         {
71                                 (int)buf[cur_len-1]++;
72                                 fwrite(buf, cur_len, 1, out);
73                                 fwrite("\n", 1, 1, out);
74                         }
75                         if(cur_len < 2)
76                                 break;
77                         if(buf[0] == tchar[0])
78                         {
79                                 bool stop = true;
80                                 for(int i = 1; i < cur_len; i++)
81                                 {
82                                         if(buf[i] != tchar[0])
83                                         {
84                                                 stop = false;
85                                                 break;
86                                         }
87                                 }
88                                 if(stop)
89                                         break;
90                         }
91                         int u = cur_len-2;
92                         for(; u>=0 && buf[u] >= tchar[0]; u--)
93                                 ;
94                         (int)buf[u]++;
95                         for(int i = u+1; i < cur_len; i++)
96                                 buf[i] = fchar[0];
97                         fwrite(buf, cur_len, 1, out);
98                         fwrite("\n", 1, 1, out);
99                 }
100                 cur_len++;
101         }
102         fclose(out);
103         return 0;
104 }