enhanced whitespace remover

This commit is contained in:
taiyu 2015-08-18 03:22:31 -07:00
parent e9c3a9016f
commit abd0afb03a
4 changed files with 32 additions and 33 deletions

View file

@ -2,8 +2,8 @@
#define _SWAY_STRINGOP_H #define _SWAY_STRINGOP_H
#include "list.h" #include "list.h"
char *strip_whitespace(char *str, int *trimmed_start); void strip_whitespace(char *str);
char *strip_comments(char *str); void strip_comments(char *str);
list_t *split_string(const char *str, const char *delims); list_t *split_string(const char *str, const char *delims);
void free_flat_list(list_t *list); void free_flat_list(list_t *list);
char *code_strchr(const char *string, char delimiter); char *code_strchr(const char *string, char delimiter);

View file

@ -490,7 +490,7 @@ static char **split_directive(char *line, int *argc) {
if (!*line) return parts; if (!*line) return parts;
int in_string = 0, in_character = 0; int in_string = 0, in_character = 0;
int i, j, _; int i, j;
for (i = 0, j = 0; line[i]; ++i) { for (i = 0, j = 0; line[i]; ++i) {
if (line[i] == '\\') { if (line[i] == '\\') {
++i; ++i;
@ -503,7 +503,7 @@ static char **split_directive(char *line, int *argc) {
char *item = malloc(i - j + 1); char *item = malloc(i - j + 1);
strncpy(item, line + j, i - j); strncpy(item, line + j, i - j);
item[i - j] = '\0'; item[i - j] = '\0';
item = strip_whitespace(item, &_); strip_whitespace(item);
if (item[0] == '\0') { if (item[0] == '\0') {
free(item); free(item);
} else { } else {
@ -521,7 +521,7 @@ static char **split_directive(char *line, int *argc) {
char *item = malloc(i - j + 1); char *item = malloc(i - j + 1);
strncpy(item, line + j, i - j); strncpy(item, line + j, i - j);
item[i - j] = '\0'; item[i - j] = '\0';
item = strip_whitespace(item, &_); strip_whitespace(item);
if (*argc == capacity) { if (*argc == capacity) {
capacity++; capacity++;
parts = realloc(parts, sizeof(char *) * capacity); parts = realloc(parts, sizeof(char *) * capacity);

View file

@ -186,10 +186,9 @@ bool read_config(FILE *file, bool is_active) {
int temp_depth = 0; // Temporary: skip all config sections with depth int temp_depth = 0; // Temporary: skip all config sections with depth
while (!feof(file)) { while (!feof(file)) {
int _;
char *line = read_line(file); char *line = read_line(file);
line = strip_comments(line); strip_comments(line);
line = strip_whitespace(line, &_); strip_whitespace(line);
if (!line[0]) { if (!line[0]) {
goto _continue; goto _continue;
} }

View file

@ -1,37 +1,38 @@
#include "stringop.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <strings.h>
#include <ctype.h>
#include "stringop.h"
#include "string.h" #include "string.h"
#include "list.h" #include "list.h"
#include <strings.h>
/* Note: This returns 8 characters for trimmed_start per tab character. */ /* Note: This returns 8 characters for trimmed_start per tab character. */
char *strip_whitespace(char *_str, int *trimmed_start) { void strip_whitespace(char *str) {
*trimmed_start = 0; int shift = 0;
if (*_str == '\0') int bpair = 1;
return _str; int in_str = 0, in_ch = 0;
char *strold = _str; while (*str) {
while (*_str == ' ' || *_str == '\t') { str[-shift] = str[0];
if (*_str == '\t') { if (*str == '"' && !in_ch) {
*trimmed_start += 8; in_str = !in_str;
} else if (*str == '\'' && !in_str) {
in_ch = !in_ch;
} else if (!in_ch && !in_str) {
if (isblank(*str)) {
if (bpair) {
++shift;
}
bpair=1;
} else { } else {
*trimmed_start += 1; bpair = 0;
} }
_str++;
} }
char *str = malloc(strlen(_str) + 1); ++str;
strcpy(str, _str); }
free(strold); str[-shift-bpair] = 0;
int i;
for (i = 0; str[i] != '\0'; ++i);
do {
i--;
} while (i >= 0 && (str[i] == ' ' || str[i] == '\t'));
str[i + 1] = '\0';
return str;
} }
char *strip_comments(char *str) { void strip_comments(char *str) {
int in_string = 0, in_character = 0; int in_string = 0, in_character = 0;
int i = 0; int i = 0;
while (str[i] != '\0') { while (str[i] != '\0') {
@ -47,7 +48,6 @@ char *strip_comments(char *str) {
} }
++i; ++i;
} }
return str;
} }
list_t *split_string(const char *str, const char *delims) { list_t *split_string(const char *str, const char *delims) {