aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-kbdc/raw-data.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-12-02 17:20:48 +0100
committerMattias Andrée <maandree@operamail.com>2014-12-02 17:20:48 +0100
commit49249273704ce64e3b92e386c990c212c4ee58c0 (patch)
tree0dd45fb687539b7fea62a098b6a5f94773cc7d9b /src/mds-kbdc/raw-data.c
parentmds-kbdc: parse --force flag (diff)
downloadmds-49249273704ce64e3b92e386c990c212c4ee58c0.tar.gz
mds-49249273704ce64e3b92e386c990c212c4ee58c0.tar.bz2
mds-49249273704ce64e3b92e386c990c212c4ee58c0.tar.xz
mds-kbdc: expand tabs
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/mds-kbdc/raw-data.c')
-rw-r--r--src/mds-kbdc/raw-data.c53
1 files changed, 49 insertions, 4 deletions
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. */
@@ -334,6 +332,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
*
* @param pathname The pathname of the 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'))
{