char *path_str(char *path, char *sub) { /* builds a path string with separating '/' */ char *retval; if (path[strlen(path)-1] != '/') { retval = strccat(path, "/"); } else { retval = path; } retval = strccat(retval, sub); return retval; }
char *size_str(off_t bytes) { /* generates human readable size information; we stick to the 'new' * * IEC standard, see http://en.wikipedia.org/wiki/Binary_prefix */ char *retval, *unit, *buffer; double number; buffer = malloc(MAX_SIZE_L); if (bytes >= 1099511627776) { unit = "TiB"; number = (double)bytes / 1099511627776; } else if (bytes >= 1073741824) { unit = "GiB"; number = (double)bytes / 1073741824; } else if (bytes >= 1048576) { unit = "MiB"; number = (double)bytes / 1048576; } else if (bytes >= 1024) { unit = "KiB"; number = (double)bytes / 1024; } else { unit = "B"; number = (double)bytes; } sprintf(buffer, "%.2f ", number); retval = strccat(buffer, unit); free(buffer); return retval; }
/** * This gets a line from stdin and allocates it to the specified C String. * @param[in,out] str - Pointer to a C String. * @return Returns str, filled with stdin up to \n */ char* strget (char* str) { char c = 0; str = strmalloc(); printf(">"); //Prompt user for input with 'cursor' while (c != '\n') { c = getchar(); if (c != '\n') str = strccat(str, toupper(c)); } return str; }
text* txtccat (text* str, char c) { text* t_str; if (str == NULL || str->first == NULL) return NULL; t_str = str; switch (c) { //Kill garbage characters/Treat as whitespace case '.': case ':': case ';': case ',': case '\'': case '\\': case '?': case '!': case ' ': //This parses/breaks up the words appropriately if (strlen(t_str->word) > 0) { t_str->next = alloc_textFirst((text*)t_str->first); t_str = t_str->next; } break; //If this is an EOL char, discard it, because that's the end of the action case '\0': case '\n': break; default: //Add the char to the word t_str->word = strccat(t_str->word, toupper(c)); break; } return t_str; }
/** * Gets user input from stdin and parses it into a newly allocated text. This destroys any data in the passed in text. May return NULL if allocation fails! * @param[in,out] str - Pointer to a text. Will be destroyed and reallocated in function. * @return Returns str with contents of parsed stdin. May return NULL. */ text* strgett (text* str) { // Initialization // char c = 0; //This is the character we get from stdin char p_char = ' '; //This is a parsing character that'll determine whether the last character was 'whitespace' or not int alloc_new = 0; //Determines if we should create a new word when the next character is entered. clear_text(str); //Allocate a new, empty string. // NULL Validation // if (str == NULL || str->first == NULL) return NULL; // Primary Function // printf(">"); //Prompt user for input with 'cursor' while (c != '\n') { c = getchar(); switch (c) { //Kill garbage characters/Treat as whitespace case '.': case ';': case ',': case '\'': case '\\': case '?': case '!': case '\n': case ' ': //This parses/breaks up the words appropriately if (p_char != ' ') { alloc_new = 1; p_char = ' '; } break; default: if (alloc_new == 1) { str->next = alloc_textFirst((text*)str->first); str = str->next; alloc_new = 0; } p_char = c; str->word = strccat(str->word, toupper(c)); break; } } str = str->first; /// TEST ROUTINES // //print_text(str); //printf("Word count: %d\n", txtcnt(str)); //printf("Text length: %d\n", txtlen(str)); //printf("Compare A BC DEF: %d\n", txtncmp(str, atot("A BC DEF\n"))); //atot is kind of buggy //printf("\n"); /// END TEST ROUTINES // // Default Return // return str; }
/** * Converts a C String to a text object. * @param[in] str - Pointer to a C String. * @return Returns a newly allocated text object, populated with contents of str. */ text* atot (char* str) { // Initialization // text* t_str = NULL; char c = 0; //This is the character we get from stdin char p_char = ' '; //This is a parsing character that'll determine whether the last character was 'whitespace' or not int alloc_new = 0; //Determines if we should create a new word when the next character is entered. int i = 0; //Current position at str. t_str = clear_text(t_str); //Allocate a new, empty string. // NULL Validation // if (t_str == NULL) printf("Failed to alloc atot\n"); else if (t_str->first == NULL) printf("Failed to alloc atot first\n"); if (t_str == NULL || t_str->first == NULL) return NULL; // Primary Function // while (c != '\n') { c = str[i]; switch (c) { //Kill garbage characters/Treat as whitespace case '.': case ';': case ',': case '\'': case '\\': case '?': case '!': case '\n': case ' ': //This parses/breaks up the words appropriately if (p_char != ' ') { alloc_new = 1; p_char = ' '; } break; case '\0': c = '\n'; break; default: if (alloc_new == 1) { t_str->next = alloc_textFirst((text*)t_str->first); t_str = t_str->next; alloc_new = 0; } p_char = c; t_str->word = strccat(t_str->word, toupper(c)); break; } i++; } t_str = t_str->first; /// TEST ROUTINES // /// END TEST ROUTINES // // Default Return // return t_str; }