diff options
author | Aaron Ball <nullspoon@oper.io> | 2018-06-21 08:18:57 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2018-06-21 08:18:57 -0600 |
commit | 2f01dfe39ff2f88f5e3293888f6bb4717de8ee42 (patch) | |
tree | ed43b774b04ac5549a0eea182c55c85efdc4adf2 /src | |
download | versioner-2f01dfe39ff2f88f5e3293888f6bb4717de8ee42.tar.gz versioner-2f01dfe39ff2f88f5e3293888f6bb4717de8ee42.tar.xz |
Currently, works with numbered versions only. Contains basic error
checking [but needs more]. Can increment or decrement the specified
digit.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 100 | ||||
-rw-r--r-- | src/version.c | 129 | ||||
-rw-r--r-- | src/version.h | 33 |
3 files changed, 262 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..7c2caf6 --- /dev/null +++ b/src/main.c @@ -0,0 +1,100 @@ +/** + * Versioner is a simple program to programatically manage version strings. + * Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "version.h" + +#define ACTION_INCREMENT 1 +#define ACTION_DECREMENT 2 + +struct parsedargs { + int digit; + int action; + char* verstr; +}; + + +void usage() { + printf( +"Versioner is a simple program to manage version strings.\n" +"\n" +"Usage:\n" +" versioner <version> <action> [options]\n" +"\n" +"Arguments:\n" +" -i,--increment <digit> Increment the specified digit\n" +" -d,--decrement <digit> Decrement the specified digit\n" +" -h,--help Print this helptext\n" +"\n" +); +} + + +int parseargs(int argc, char* argv[], struct parsedargs* args) { + int i = 1; + for(; i < argc; i++) { + if(strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--increment") == 0) { + i++; + // Subtract 1 because human input is usually 1 indexed rather than 0 + args->digit = atoi(argv[i]) - 1; + args->action = ACTION_INCREMENT; + } else if(strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--decrement") == 0) { + i++; + // Subtract 1 because human input is usually 1 indexed rather than 0 + args->digit = atoi(argv[i]) - 1; + args->action = ACTION_DECREMENT; + } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { + usage(); + exit(0); + } else { + // Assume unknown switch is the version to work with + args->verstr = argv[i]; + } + } + return 0; +} + + +int main(int argc, char* argv[]) { + struct parsedargs parsedargs; + parsedargs.action = -1; + parsedargs.digit = -1; + parsedargs.verstr = NULL; + + int parseargv = parseargs(argc, argv, &parsedargs); + if(parseargv == -1) + return 1; + + struct version ver; + version_new(&ver); + + // Ingest the string. + version_read_str(&ver, parsedargs.verstr); + + // Act! + if(parsedargs.action == ACTION_INCREMENT) + version_increment(&ver, parsedargs.digit); + else if(parsedargs.action == ACTION_DECREMENT) + version_decrement(&ver, parsedargs.digit); + + // Dump the version to stdout + version_print(&ver); + return 0; +} diff --git a/src/version.c b/src/version.c new file mode 100644 index 0000000..f692ac6 --- /dev/null +++ b/src/version.c @@ -0,0 +1,129 @@ +/** + * Versioner is a simple program to programatically manage version strings. + * Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "version.h" + +#ifndef DIGITMAX +#define DIGITMAX 64 +#endif + + +void version_new(struct version* ver) { + int i = 0; + + // Initialize all digits as null + for(; i<DIGITMAX; i++) { + ver->digits[i] = -1; + } + ver->count = i; +} + + +void version_read_str(struct version* ver, char* str) { + char buf[64]; // Buffer in which to store digit chars + int bufi = 0; // Digit string buffer cursor + int digit = 0; // Version digit (for version->digits) + int i = 0; // Cursor for reading through string version + + for(; i < strlen(str); i++) { + if(str[i] == '.') { + buf[bufi + 1] = '\0'; + ver->digits[digit] = atoi(buf); + bufi = 0; // reset buffer index + digit++; // Increment digit index + continue; + } + + buf[bufi] = str[i]; + bufi++; + } + + // Only append if last digit isn't a period + // While this is a version syntax error, we should handle and correct it + if(str[i - 1] != '.') { + buf[bufi] = '\0'; + ver->digits[digit] = atoi(buf); + } + + // Set ver->count to digit +1 (digit is 0 indexed) for a human-readable count + ver->count = digit + 1; +} + + +void version_print(struct version* ver) { + int i = 0; + char out[DIGITMAX * 2]; + // Initialize + out[0] = '\0'; + + // Loop + for(; i<DIGITMAX; i++) { + if(ver->digits[i] == -1) { + break; + } + sprintf(out, "%s%d.", out, ver->digits[i]); + } + out[strlen(out) - 1] = '\0'; + printf("%s\n", out); +} + + + +/** + * version_increment: + * Increments the specified digit in the provided version struct. + * + * @ver Version struct within which to increment a digit + * @digit Which digit to increment (0 indexed) + */ +void version_increment(struct version* ver, int digit) { + int i; // digit buffer for incrementing without modifying the source argument + i = digit + 1; + + // Increment the digit + ver->digits[digit]++; + + // Set all following digits to 0 + while(i < ver->count) { + ver->digits[i] = 0; + i++; + } +} + + +/** + * version_decrement: + * Decrements the specified digit in the provided version struct. + * NOTE: This function is basically useless. Why would anyone ever want to + * programatically decrement a verion or want more than 640K of ram? + * + * @ver Version struct within which to decrement a digit + * @digit Which digit to increment (0 indexed) + */ +void version_decrement(struct version* ver, int digit) { + int i; // digit buffer for incrementing without modifying the source argument + i = digit + 1; + + // Decrement the digit + ver->digits[digit]--; + + // Set all following digits to 0 + while(i < ver->count) { + ver->digits[i] = 0; + i++; + } +} diff --git a/src/version.h b/src/version.h new file mode 100644 index 0000000..26a6f40 --- /dev/null +++ b/src/version.h @@ -0,0 +1,33 @@ +/** + * Versioner is a simple program to programatically manage version strings. + * Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct version { + int digits[64]; + int count; + //char* extra; +}; + +void version_new(struct version*); +void str_to_ver(char*, struct version*); +void version_print(struct version*); +void version_read_str(struct version*, char*); +void version_increment(struct version*, int); +void version_decrement(struct version*, int); |