diff options
Diffstat (limited to 'src/common.c')
-rw-r--r-- | src/common.c | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/src/common.c b/src/common.c index 4ae509a..311e259 100644 --- a/src/common.c +++ b/src/common.c @@ -1,3 +1,19 @@ +/** + * Copyright (C) 2015 Aaron Ball <nullspoon@iohq.net> + * + * 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 "common.h" /** @@ -11,24 +27,37 @@ * @return int The character count of the output char array */ int itoa(int i, char* out) { - // To keep track of where we are in the array + char buf[256]; int n = 0; + + // To keep track of where we are in the array while(i > 9) { - printf(" %d\n", i); // This is some crazy simple math here - out[n] = (i%10) + 48; + buf[n] = (i%10) + 48; // It's an int, so the last number will be stripped i = i/10; n++; } // Have to do this one more time to ensure we get the last number on // zero-ended ints - out[n] = (i%10) + 48; + buf[n] = (i%10) + 48; // Account for the last char n++; // Annnd the null byte + buf[n] = '\0'; + + + // Now we have to reverse the digit order, because it was read in right to + // left, not left to right (which populating an array requires ltr). + int reversen = n - 1; + int cursor = 0; + // Reverse the order + while(buf[cursor] != '\0') { + out[reversen] = buf[cursor]; + cursor++; + reversen--; + } out[n] = '\0'; - n++; + return n; } - |