From 49249273704ce64e3b92e386c990c212c4ee58c0 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 2 Dec 2014 17:20:48 +0100 Subject: mds-kbdc: expand tabs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/mds-kbdc/make-tree.c | 2 -- src/mds-kbdc/raw-data.c | 53 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/mds-kbdc/make-tree.c b/src/mds-kbdc/make-tree.c index 5d9558a..14db125 100644 --- a/src/mds-kbdc/make-tree.c +++ b/src/mds-kbdc/make-tree.c @@ -802,8 +802,6 @@ static int read_source_code(state_t* restrict state) /* Read the file and simplify it a bit. */ fail_if (read_source_lines(parsing_result->pathname, parsing_result->source_code) < 0); - /* TODO '\t':s should be expanded into ' ':s. */ - return 0; pfail: return -1; diff --git a/src/mds-kbdc/raw-data.c b/src/mds-kbdc/raw-data.c index 9f56210..611dc71 100644 --- a/src/mds-kbdc/raw-data.c +++ b/src/mds-kbdc/raw-data.c @@ -126,9 +126,7 @@ static char* read_file(const char* restrict pathname, size_t* restrict size) { /* Make sure the buffer is not small. */ if (buf_size - buf_ptr < 2048) - { - fail_if (xxrealloc(old, content, buf_size <<= 1, char)); - } + fail_if (xxrealloc(old, content, buf_size <<= 1, char)); /* Read a chunk of the file. */ got = read(fd, content + buf_ptr, (buf_size - buf_ptr) * sizeof(char)); if ((got < 0) && (errno == EINTR)) continue; @@ -138,7 +136,7 @@ static char* read_file(const char* restrict pathname, size_t* restrict size) } /* Shrink the buffer so it is not excessively large. */ - if (buf_ptr) /* Simplest way to handle empty files: let the have the initial allocation size. */ + if (buf_ptr) /* Simplest way to handle empty files: let the have the initial allocation size. */ fail_if (xxrealloc(old, content, buf_ptr, char)); /* Close file decriptor for the file. */ @@ -333,6 +331,50 @@ static char** line_split(char* content, size_t length) } +/** + * Translate all tab spaces into blank spaces + * + * @param content Input and output parameter for the file's content + * @param content_size Input and output parameter for the size of the file's content + * @return Zero on success, -1 on error + */ +static int expand(char** restrict content, size_t* restrict content_size) +{ + size_t extra = 0, added = 0, ptr, col, n = *content_size; + char* restrict data = *content; + + /* Calculate the new size of the file. */ + for (ptr = col = 0; ptr < n; ptr++) + if (data[ptr] == '\n') + col = 0; + else if (data[ptr] == '\t') + extra += 8 - (col % 8) - 1; + + /* Extend the allocation. */ + if (extra == 0) + return 0; + *content_size += extra; + fail_if (xrealloc(data, *content_size, char)); + *content = data; + + /* Expand tab spaces. */ + memmove(data + extra, data, n); + for (ptr = 0; ptr < n; ptr++, added--) + if (data[ptr] == '\n') + data[ptr + added++] = data[ptr + extra], col = 0; + else if (data[ptr] != '\t') + data[ptr + added++] = data[ptr + extra], col++; + else + do + data[ptr + added++] = ' '; + while (++col % 8); + + return 0; + pfail: + return -1; +} + + /** * Read lines of a source file * @@ -355,6 +397,9 @@ int read_source_lines(const char* restrict pathname, mds_kbdc_source_code_t* res content = read_file(pathname, &content_size); fail_if (content == NULL); + /* Expand tab spaces. */ + fail_if (expand(&content, &content_size)); + /* Make sure the content ends with a new line. */ if (!content_size || (content[content_size - 1] != '\n')) { -- cgit v1.2.3-70-g09d2