Use a strbuf for building up section header and key/value pair strings.
Avoids horrible 1-byte write(2) calls and cleans up the logic a bit. Signed-off-by: Kristian Høgsberg <krh@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Kristian Høgsberg committed
Dec 14, 2007 at 15:59 UTC
cb891a5989c41ed479db9eb4b0a69cef8bb98ea8
1 file changed
+38
-53
config.c
+38
-53
@@ -610,46 +610,36 @@ static int write_error(void)
610
611
static int store_write_section(int fd, const char* key)
612
{
613
- const char *dot = strchr(key, '.');
614
- int len1 = store.baselen, len2 = -1;
613
+ const char *dot;
614
+ int i, success;
615
+ struct strbuf sb;
616
616
- dot = strchr(key, '.');
617
+ strbuf_init(&sb, 0);
618
+ dot = memchr(key, '.', store.baselen);
619
if (dot) {
618
- int dotlen = dot - key;
619
- if (dotlen < len1) {
620
- len2 = len1 - dotlen - 1;
621
- len1 = dotlen;
620
+ strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
621
+ for (i = dot - key + 1; i < store.baselen; i++) {
622
+ if (key[i] == '"')
623
+ strbuf_addch(&sb, '\\');
624
+ strbuf_addch(&sb, key[i]);
625
}
626
+ strbuf_addstr(&sb, "\"]\n");
627
+ } else {
628
+ strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
629
}
630
625
- if (write_in_full(fd, "[", 1) != 1 ||
626
- write_in_full(fd, key, len1) != len1)
627
- return 0;
628
- if (len2 >= 0) {
629
- if (write_in_full(fd, " \"", 2) != 2)
630
- return 0;
631
- while (--len2 >= 0) {
632
- unsigned char c = *++dot;
633
- if (c == '"')
634
- if (write_in_full(fd, "\\", 1) != 1)
635
- return 0;
636
- if (write_in_full(fd, &c, 1) != 1)
637
- return 0;
638
- }
639
- if (write_in_full(fd, "\"", 1) != 1)
640
- return 0;
641
- }
642
- if (write_in_full(fd, "]\n", 2) != 2)
643
- return 0;
631
+ success = write_in_full(fd, sb.buf, sb.len) == sb.len;
632
+ strbuf_release(&sb);
633
645
- return 1;
634
+ return success;
635
}
636
637
static int store_write_pair(int fd, const char* key, const char* value)
638
{
650
- int i;
651
- int length = strlen(key+store.baselen+1);
652
- int quote = 0;
639
+ int i, success;
640
+ int length = strlen(key + store.baselen + 1);
641
+ const char *quote = "";
642
+ struct strbuf sb;
643
644
/*
645
* Check to see if the value needs to be surrounded with a dq pair.
@@ -659,43 +649,38 @@ static int store_write_pair(int fd, const char* key, const char* value)
649
* configuration parser.
650
*/
651
if (value[0] == ' ')
662
- quote = 1;
652
+ quote = "\"";
653
for (i = 0; value[i]; i++)
654
if (value[i] == ';' || value[i] == '#')
665
- quote = 1;
666
- if (i && value[i-1] == ' ')
667
- quote = 1;
655
+ quote = "\"";
656
+ if (i && value[i - 1] == ' ')
657
+ quote = "\"";
658
+
659
+ strbuf_init(&sb, 0);
660
+ strbuf_addf(&sb, "\t%.*s = %s",
661
+ length, key + store.baselen + 1, quote);
662
669
- if (write_in_full(fd, "\t", 1) != 1 ||
670
- write_in_full(fd, key+store.baselen+1, length) != length ||
671
- write_in_full(fd, " = ", 3) != 3)
672
- return 0;
673
- if (quote && write_in_full(fd, "\"", 1) != 1)
674
- return 0;
663
for (i = 0; value[i]; i++)
664
switch (value[i]) {
665
case '\n':
678
- if (write_in_full(fd, "\\n", 2) != 2)
679
- return 0;
666
+ strbuf_addstr(&sb, "\\n");
667
break;
668
case '\t':
682
- if (write_in_full(fd, "\\t", 2) != 2)
683
- return 0;
669
+ strbuf_addstr(&sb, "\\t");
670
break;
671
case '"':
672
case '\\':
687
- if (write_in_full(fd, "\\", 1) != 1)
688
- return 0;
673
+ strbuf_addch(&sb, '\\');
674
default:
690
- if (write_in_full(fd, value+i, 1) != 1)
691
- return 0;
675
+ strbuf_addch(&sb, value[i]);
676
break;
677
}
694
- if (quote && write_in_full(fd, "\"", 1) != 1)
695
- return 0;
696
- if (write_in_full(fd, "\n", 1) != 1)
697
- return 0;
698
- return 1;
678
+ strbuf_addf(&sb, "%s\n", quote);
679
+
680
+ success = write_in_full(fd, sb.buf, sb.len) == sb.len;
681
+ strbuf_release(&sb);
682
+
683
+ return success;
684
}
685
686
static ssize_t find_beginning_of_line(const char* contents, size_t size,