diff options
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | api.c | 36 | ||||
-rw-r--r-- | api.h | 29 | ||||
-rw-r--r-- | main.c | 11 | ||||
-rw-r--r-- | string-tick.c | 47 | ||||
-rw-r--r-- | string-tick.h | 49 |
6 files changed, 99 insertions, 76 deletions
diff --git a/README.md b/README.md index 10a9096d9228..8e936b51c93d 100644 --- a/README.md +++ b/README.md @@ -99,4 +99,5 @@ and portfolio distribution (tried to implement, but IEX API is unpredictable; wa * Check without re-encrypting in case sigint * Format prices with commas (set locale) * Function to add new attributes to portfolio if not found -* Improve code documentation
\ No newline at end of file +* Improve code documentation +* Round doubles
\ No newline at end of file @@ -1,22 +1,5 @@ #include "api.h" -String* api_string_init(void) { - String* pString = (String*) malloc(sizeof(String)); - if (pString != NULL) { - pString->len = 0; - pString->data = (char*) malloc(sizeof(char)); - if (pString->data == NULL) { - fprintf(stderr, "malloc() failed\n"); - exit(EXIT_FAILURE); - } - pString->data[0] = '\0'; - } else { - fprintf(stderr, "malloc() failed\n"); - exit(EXIT_FAILURE); - } - return pString; -} - Info* api_info_init(void) { Info* pInfo = malloc(sizeof(Info)); if (pInfo != NULL) { @@ -404,25 +387,6 @@ char* google_shorten_link(const char* url_string) { return short_url; } -char* strip_char(char* string, char c) { - size_t len = strlen(string); - int i, j; - for (i = 0, j = 0; j < (int) len; i++, j++) { - while (string[j] == c) - j++; - string[i] = string[j]; - } - string[i] = '\0'; - return string; -} - -void api_string_destroy(String** phString) { - String* pString = *phString; - free(pString->data); - free(*phString); - *phString = NULL; -} - void api_info_destroy(Info** phInfo) { Info* pInfo = *phInfo; free(pInfo->name); @@ -17,13 +17,7 @@ #include <stddef.h> #include <curl/curl.h> #include <json-c/json_tokener.h> - -struct string { - char* data; - size_t len; -}; - -typedef struct string String; +#include "string-tick.h" struct info { char* name; // Name of security (ex. Apple Inc.) @@ -40,13 +34,6 @@ typedef struct info Info; typedef struct json_object Json; /** - * Creates and returns a STRING - * object with size 1 and no data - * @return STRING object - */ -String* api_string_init(void); - -/** * Creates and returns an Info object * @return Info object */ @@ -154,20 +141,6 @@ Info* coinmarketcap_get_info(const char* ticker_name_string); char* google_shorten_link(const char* url_string); /** - * Returns the input string, stripped of the given char - * @param string the string to strip the char from - * @param c the char to strip - * @return input string - */ -char* strip_char(char* string, char c); - -/** - * Destroys String object and frees memory - * @param phString the String to destroy - */ -void api_string_destroy(String** phString); - -/** * Destroys Info object and frees memory * @param phInfo the Info to destroy */ @@ -1,17 +1,6 @@ -#include <ctype.h> #include "api.h" #include "portfolio.h" -void strtolower(char* str) { - for (int i = 0; str[i] != '\0'; i++) - str[i] = (char) tolower(str[i]); -} - -void strtoupper(char* str) { - for (int i = 0; str[i] != '\0'; i++) - str[i] = (char) toupper(str[i]); -} - int main(int argc, char* argv[]) { if (argc < 2) { printf("Invalid arguments. Type \"man tick\" for help.\n"); diff --git a/string-tick.c b/string-tick.c new file mode 100644 index 000000000000..c5323f76afce --- /dev/null +++ b/string-tick.c @@ -0,0 +1,47 @@ +#include "string-tick.h" + +String* api_string_init(void) { + String* pString = (String*) malloc(sizeof(String)); + if (pString != NULL) { + pString->len = 0; + pString->data = (char*) malloc(sizeof(char)); + if (pString->data == NULL) { + fprintf(stderr, "malloc() failed\n"); + exit(EXIT_FAILURE); + } + pString->data[0] = '\0'; + } else { + fprintf(stderr, "malloc() failed\n"); + exit(EXIT_FAILURE); + } + return pString; +} + +void strtolower(char* str) { + for (int i = 0; str[i] != '\0'; i++) + str[i] = (char) tolower(str[i]); +} + +void strtoupper(char* str) { + for (int i = 0; str[i] != '\0'; i++) + str[i] = (char) toupper(str[i]); +} + +char* strip_char(char* string, char c) { + size_t len = strlen(string); + int i, j; + for (i = 0, j = 0; j < (int) len; i++, j++) { + while (string[j] == c) + j++; + string[i] = string[j]; + } + string[i] = '\0'; + return string; +} + +void api_string_destroy(String** phString) { + String* pString = *phString; + free(pString->data); + free(*phString); + *phString = NULL; +}
\ No newline at end of file diff --git a/string-tick.h b/string-tick.h new file mode 100644 index 000000000000..21e0c0c67e7c --- /dev/null +++ b/string-tick.h @@ -0,0 +1,49 @@ +#ifndef STRING_TICK_H +#define STRING_TICK_H + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +struct string { + char* data; + size_t len; +}; + +typedef struct string String; + +/** + * Creates and returns a STRING + * object with size 1 and no data + * @return STRING object + */ +String* api_string_init(void); + +/** + * Changes letters in a string to lowercase + * @param str the string + */ +void strtolower(char* str); + +/** + * Changes letters in a string to uppercase + * @param str the string + */ +void strtoupper(char* str); + +/** + * Returns the input string, stripped of the given char + * @param string the string to strip the char from + * @param c the char to strip + * @return input string + */ +char* strip_char(char* string, char c); + +/** + * Destroys String object and frees memory + * @param phString the String to destroy + */ +void api_string_destroy(String** phString); + +#endif
\ No newline at end of file |