summaryrefslogtreecommitdiff
path: root/src/version.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/version.c')
-rw-r--r--src/version.c129
1 files changed, 129 insertions, 0 deletions
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++;
+ }
+}

Generated by cgit