summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 1ce25be64f1de7cc0dda595d55add6b2059d8548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <unistd.h>
#include <stdio.h>
#include <iconv.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
        int opt = -1, min_len = 0, max_len = 0;
        char ofile[256], fchar[2], tchar[2];
        ofile[0] = 0;
        fchar[0] = 0;
        tchar[0] = 0;
        while((opt = getopt(argc, argv, "o:f:t:l:L:")) != -1)
        {
                switch(opt)
                {
                        case 'o':
                        strncpy(ofile, optarg, 255);
                        break;
                        case 'f':
                        strncpy(fchar, optarg, 1);
                        break;
                        case 't':
                        strncpy(tchar, optarg, 1);
						break;
						case 'l':
						min_len = atoi(optarg);
                        break;
						case 'L':
						max_len = atoi(optarg);
						break;
                        default:
                        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]);
                }
        }
	if(max_len < 1)
	{
		printf("error, length must be more than 0\n");
		return 1;
	}
	if(min_len == 0)
		min_len = 1;
	if(min_len > max_len)
	{
		printf("error, max length must be greater or equal min_length\n");
		return 1;
	}
	if((int)fchar[0] > (int)tchar[0])
	{
		printf("error, invalid range specified\n");
		return 1;
	}
	FILE *out = fopen(ofile, "w");
	if(!out)
	{
		printf("failed to open input file with error: %s\n", strerror(errno));
		return 1;
	}
	int cur_len = min_len;
	while(cur_len <= max_len)
	{
		char buf[cur_len];
		for(int i = 0; i < cur_len; i++)
			buf[i] = fchar[0];
		fwrite(buf, cur_len, 1, out);
		fwrite("\n", 1, 1, out);
		while(buf[0] != (tchar[0]+1))
		{
			while(buf[cur_len-1] < tchar[0])
			{
				(int)buf[cur_len-1]++;
				fwrite(buf, cur_len, 1, out);
				fwrite("\n", 1, 1, out);
			}
			if(cur_len < 2)
				break;
			if(buf[0] == tchar[0])
			{
				bool stop = true;
				for(int i = 1; i < cur_len; i++)
				{
					if(buf[i] != tchar[0])
					{
						stop = false;
						break;
					}
				}
				if(stop)
					break;
			}
			int u = cur_len-2;
			for(; u>=0 && buf[u] >= tchar[0]; u--)
				;
			(int)buf[u]++;
			for(int i = u+1; i < cur_len; i++)
				buf[i] = fchar[0];
			fwrite(buf, cur_len, 1, out);
			fwrite("\n", 1, 1, out);
		}
		cur_len++;
	}
	fclose(out);
	return 0;
}