diff options
-rw-r--r-- | empty | 0 | ||||
-rw-r--r-- | main.cpp | 104 |
2 files changed, 104 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a04b0db --- /dev/null +++ b/main.cpp @@ -0,0 +1,104 @@ +#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 > 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; +} |