use gnu indent to enforce coding style and adapt source

- indent options are listed in ./.indent.pro
 - use test case to run indent
This commit is contained in:
josch
2014-09-20 10:59:54 +02:00
parent d8e00119b4
commit 097a855751
43 changed files with 1325 additions and 1278 deletions

View File

@@ -16,7 +16,6 @@
*
*/
#include <ctype.h>
#include <stdarg.h>
#include <stddef.h>
@@ -27,86 +26,90 @@
#include "strings.h"
#include "stringv.h"
char*
strdup_printf(char* fmt, ...)
char *strdup_printf(char *fmt, ...)
{
// Good for glibc 2.1 and above. Fedora5 is 2.4.
// Good for glibc 2.1 and above. Fedora5 is 2.4.
char *ret_str = NULL;
va_list ap;
int bytes_to_allocate;
char *ret_str = NULL;
va_list ap;
int bytes_to_allocate;
va_start(ap, fmt);
bytes_to_allocate = vsnprintf(ret_str, 0, fmt, ap);
va_end(ap);
va_start(ap, fmt);
bytes_to_allocate = vsnprintf(ret_str, 0, fmt, ap);
va_end(ap);
// Add one for '\0'
bytes_to_allocate++;
// Add one for '\0'
bytes_to_allocate++;
ret_str = (char*)malloc(bytes_to_allocate * sizeof(char));
ret_str = (char *)malloc(bytes_to_allocate * sizeof(char));
va_start(ap, fmt);
bytes_to_allocate = vsnprintf(ret_str, bytes_to_allocate, fmt, ap);
va_end(ap);
va_start(ap, fmt);
bytes_to_allocate = vsnprintf(ret_str, bytes_to_allocate, fmt, ap);
va_end(ap);
return ret_str;
return ret_str;
}
char*
strdup_join(char *string1,char *string2)
char *strdup_join(char *string1, char *string2)
{
char *new_string;
size_t string1_len;
size_t string2_len;
char *new_string;
size_t string1_len;
size_t string2_len;
if(string1 == NULL || string2 == NULL) return NULL;
if (string1 == NULL || string2 == NULL)
return NULL;
string1_len = strlen(string1);
string2_len = strlen(string2);
new_string = (char*)malloc(string1_len + string2_len + 1);
new_string = (char *)malloc(string1_len + string2_len + 1);
strncpy(new_string,string1,string1_len);
strncat(new_string,string2,string2_len);
strncpy(new_string, string1, string1_len);
strncat(new_string, string2, string2_len);
new_string[string1_len + string2_len] = '\0';
return new_string;
}
char*
strdup_subst(char *string,char *token,char *subst, unsigned int max)
char *strdup_subst(char *string, char *token, char *subst,
unsigned int max)
{
size_t string_len = 0;
size_t subst_len = 0;
size_t token_len = 0;
size_t total_len = 0;
size_t copy_len = 0;
size_t token_count;
char *str_new = NULL;
char **vectors;
char **rewind;
size_t string_len = 0;
size_t subst_len = 0;
size_t token_len = 0;
size_t total_len = 0;
size_t copy_len = 0;
size_t token_count;
char *str_new = NULL;
char **vectors;
char **rewind;
if(string == NULL) return NULL;
if (string == NULL)
return NULL;
if(token == NULL || subst == NULL) return NULL;
if (token == NULL || subst == NULL)
return NULL;
string_len = strlen(string);
token_len = strlen(token);
// return on conditions that we could never handle.
if(token_len > string_len) return NULL;
if(token[0] == '\0') return NULL;
if (token_len > string_len)
return NULL;
if (token[0] == '\0')
return NULL;
vectors = stringv_find(string,token,max);
if(vectors == NULL) return NULL;
vectors = stringv_find(string, token, max);
if (vectors == NULL)
return NULL;
rewind = vectors;
// count the number of tokens found in the string
token_count = stringv_len(vectors);
if(token_count > max) token_count = max;
if (token_count > max)
token_count = max;
// start with the original string size;
total_len = string_len;
@@ -117,24 +120,22 @@ strdup_subst(char *string,char *token,char *subst, unsigned int max)
// add back the total number of subst chars to be inserted
total_len += (subst_len * token_count);
str_new = (char*)malloc((total_len + 1) * sizeof(char));
str_new = (char *)malloc((total_len + 1) * sizeof(char));
str_new[0] = '\0';
while(*vectors != NULL)
{
while (*vectors != NULL) {
// calculate distance to the next token from current position
copy_len = *vectors - string;
if(copy_len > 0)
{
strncat(str_new,string,copy_len);
if (copy_len > 0) {
strncat(str_new, string, copy_len);
string += copy_len;
// when total_len == 0 the process is complete
total_len -= copy_len;
}
strncat(str_new,token,token_len);
strncat(str_new, token, token_len);
// when total_len == 0 the process is complete
total_len -= token_len;
@@ -143,33 +144,31 @@ strdup_subst(char *string,char *token,char *subst, unsigned int max)
}
// might have one more copy operation to complete
if(total_len > 0)
{
strcat(str_new,string);
if (total_len > 0) {
strcat(str_new, string);
}
// todo: can't free vectors directly cuz it was incremented
free(rewind);
return str_new;
}
void
string_chomp(char *string)
void string_chomp(char *string)
{
size_t len;
char *pos;
size_t len;
char *pos;
if(string == NULL) return;
if (string == NULL)
return;
len = strlen(string);
if(len == 0) return;
if (len == 0)
return;
pos = &string[len - 1];
while(isspace((int)*pos) != 0)
{
while (isspace((int)*pos) != 0) {
*pos = '\0';
pos--;
}