]> mj.ucw.cz Git - xsv.git/commitdiff
Use unlocked stdio, it is faster
authorMartin Mares <mj@ucw.cz>
Tue, 24 Jul 2012 12:02:56 +0000 (14:02 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 24 Jul 2012 12:02:56 +0000 (14:02 +0200)
TODO
xsv.c

diff --git a/TODO b/TODO
index 71e530a825b854fd48d6c160a0cfd8de837c2c28..93a27da1a7c6b4553c32979682f62ace84fabfc9 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,2 +1 @@
 - call ferror()
 - call ferror()
-- unlocked stdio?
diff --git a/xsv.c b/xsv.c
index 5dfa80f15cad1903defda8213204fb6d3509af56..6456410b4eebd32162912cae3de45203d66e8cde 100644 (file)
--- a/xsv.c
+++ b/xsv.c
@@ -4,6 +4,8 @@
  *     (c) 2012 Martin Mares <mj@ucw.cz>
  */
 
  *     (c) 2012 Martin Mares <mj@ucw.cz>
  */
 
+#define _GNU_SOURCE
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -145,7 +147,7 @@ static void warn(struct format *fmt, char *msg, ...)
 static int next_line(void)
 {
        for (;;) {
 static int next_line(void)
 {
        for (;;) {
-               int c = getchar();
+               int c = getchar_unlocked();
                if (c == '\r')
                        continue;
                if (c < 0)
                if (c == '\r')
                        continue;
                if (c < 0)
@@ -181,7 +183,7 @@ static int csv_read(void)
 {
        int quoted = 0;
        for (;;) {
 {
        int quoted = 0;
        for (;;) {
-               int c = getchar();
+               int c = getchar_unlocked();
                int i = line_count(&in_line);
 restart:
                if (c == '\r')
                int i = line_count(&in_line);
 restart:
                if (c == '\r')
@@ -196,7 +198,7 @@ restart:
                }
                if (quoted) {
                        if (c == in_format->quote) {
                }
                if (quoted) {
                        if (c == in_format->quote) {
-                               c = getchar();
+                               c = getchar_unlocked();
                                if (c != in_format->quote) {
                                        quoted = 0;
                                        goto restart;
                                if (c != in_format->quote) {
                                        quoted = 0;
                                        goto restart;
@@ -239,21 +241,21 @@ static void csv_write(void)
                        }
                }
                if (i)
                        }
                }
                if (i)
-                       putchar(out_format->fs);
+                       putchar_unlocked(out_format->fs);
                if (need_quotes)
                if (need_quotes)
-                       putchar(out_format->quote);
+                       putchar_unlocked(out_format->quote);
                for (int j=0; j < f->len; j++) {
                        int c = line[f->start_pos + j];
                        if (c == out_format->fs && !need_quotes)
                                warn(out_format, "Field separator found inside field and quoting is turned off.");
                        if (c == out_format->quote)
                for (int j=0; j < f->len; j++) {
                        int c = line[f->start_pos + j];
                        if (c == out_format->fs && !need_quotes)
                                warn(out_format, "Field separator found inside field and quoting is turned off.");
                        if (c == out_format->quote)
-                               putchar(c);
-                       putchar(c);
+                               putchar_unlocked(c);
+                       putchar_unlocked(c);
                }
                if (need_quotes)
                }
                if (need_quotes)
-                       putchar(out_format->quote);
+                       putchar_unlocked(out_format->quote);
        }
        }
-       putchar('\n');
+       putchar_unlocked('\n');
 }
 
 /*** White-space back-end ***/
 }
 
 /*** White-space back-end ***/
@@ -354,13 +356,13 @@ static void table_write(void)
                }
                unsigned char *p = line_nth(&in_line, f->start_pos);
                for (int j = 0; j < f->len; j++)
                }
                unsigned char *p = line_nth(&in_line, f->start_pos);
                for (int j = 0; j < f->len; j++)
-                       putchar(p[j]);
+                       putchar_unlocked(p[j]);
                while (fw < cw) {
                while (fw < cw) {
-                       putchar(' ');
+                       putchar_unlocked(' ');
                        fw++;
                }
        }
                        fw++;
                }
        }
-       putchar('\n');
+       putchar_unlocked('\n');
 }
 
 /*** Temporary file back-end ***/
 }
 
 /*** Temporary file back-end ***/
@@ -370,21 +372,21 @@ static int tmp_read(void)
        FILE *tf = in_format->tmp_file;
 
        for (;;) {
        FILE *tf = in_format->tmp_file;
 
        for (;;) {
-               int c = fgetc(tf);
+               int c = getc_unlocked(tf);
                if (c < 0)
                        return 0;
                if (c == 0xff)
                        return 1;
                if (c == 0xfe) {
                if (c < 0)
                        return 0;
                if (c == 0xff)
                        return 1;
                if (c == 0xfe) {
-                       c = fgetc(tf);
-                       c = (c << 8) | fgetc(tf);
-                       c = (c << 8) | fgetc(tf);
-                       c = (c << 8) | fgetc(tf);
+                       c = getc_unlocked(tf);
+                       c = (c << 8) | getc_unlocked(tf);
+                       c = (c << 8) | getc_unlocked(tf);
+                       c = (c << 8) | getc_unlocked(tf);
                }
                new_field(line_count(&in_line));
                in_field->len = c;
                while (c--) {
                }
                new_field(line_count(&in_line));
                in_field->len = c;
                while (c--) {
-                       int x = fgetc(tf);
+                       int x = getc_unlocked(tf);
                        if (x < 0) {
                                warn(in_format, "Truncated temporary file");
                                return 0;
                        if (x < 0) {
                                warn(in_format, "Truncated temporary file");
                                return 0;
@@ -401,18 +403,18 @@ static void tmp_write(void)
        for (int i = 0; i < fields_count(&in_fields); i++) {
                struct field *f = fields_nth(&in_fields, i);
                if (f->len < 0xfe)
        for (int i = 0; i < fields_count(&in_fields); i++) {
                struct field *f = fields_nth(&in_fields, i);
                if (f->len < 0xfe)
-                       fputc(f->len, tf);
+                       putc_unlocked(f->len, tf);
                else {
                else {
-                       fputc(0xfe, tf);
-                       fputc((f->len >> 24) & 0xff, tf);
-                       fputc((f->len >> 16) & 0xff, tf);
-                       fputc((f->len >> 8) & 0xff, tf);
-                       fputc(f->len & 0xff, tf);
+                       putc_unlocked(0xfe, tf);
+                       putc_unlocked((f->len >> 24) & 0xff, tf);
+                       putc_unlocked((f->len >> 16) & 0xff, tf);
+                       putc_unlocked((f->len >> 8) & 0xff, tf);
+                       putc_unlocked(f->len & 0xff, tf);
                }
 
                unsigned char *p = line_nth(&in_line, f->start_pos);
                for (int j = 0; j < f->len; j++)
                }
 
                unsigned char *p = line_nth(&in_line, f->start_pos);
                for (int j = 0; j < f->len; j++)
-                       fputc(*p++, tf);
+                       putc_unlocked(*p++, tf);
 
                intarray_t *w = &out_format->column_widths;
                while (i >= intarray_count(w))
 
                intarray_t *w = &out_format->column_widths;
                while (i >= intarray_count(w))
@@ -421,7 +423,7 @@ static void tmp_write(void)
                if (*intarray_nth(w, i) < fw)
                        *intarray_nth(w, i) = fw;
        }
                if (*intarray_nth(w, i) < fw)
                        *intarray_nth(w, i) = fw;
        }
-       fputc(0xff, tf);
+       putc_unlocked(0xff, tf);
 }
 
 /*** Transforms ***/
 }
 
 /*** Transforms ***/