From: Austin Clements Date: Wed, 25 Jul 2012 02:34:43 +0000 (+2000) Subject: [PATCH 03/13] sprinter: Add a string_len method X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=93e8b149a63b1554617ee44f74dabca27dea8b28;p=notmuch-archives.git [PATCH 03/13] sprinter: Add a string_len method --- diff --git a/3d/e4e1605dcedb03a2178d643f080052a34e4537 b/3d/e4e1605dcedb03a2178d643f080052a34e4537 new file mode 100644 index 000000000..b4aab3866 --- /dev/null +++ b/3d/e4e1605dcedb03a2178d643f080052a34e4537 @@ -0,0 +1,175 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id EE3A4431FAE + for ; Tue, 24 Jul 2012 19:35:18 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id Sh4aC+d8oGpS for ; + Tue, 24 Jul 2012 19:35:17 -0700 (PDT) +Received: from dmz-mailsec-scanner-7.mit.edu (DMZ-MAILSEC-SCANNER-7.MIT.EDU + [18.7.68.36]) + by olra.theworths.org (Postfix) with ESMTP id 2B0EC431E82 + for ; Tue, 24 Jul 2012 19:35:07 -0700 (PDT) +X-AuditID: 12074424-b7f2a6d0000008bf-7d-500f5b59312e +Received: from mailhub-auth-2.mit.edu ( [18.7.62.36]) + by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP + id 90.9B.02239.95B5F005; Tue, 24 Jul 2012 22:35:05 -0400 (EDT) +Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) + by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id q6P2Z4Wu005060; + Tue, 24 Jul 2012 22:35:04 -0400 +Received: from drake.dyndns.org + (209-6-116-242.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com + [209.6.116.242]) (authenticated bits=0) + (User authenticated as amdragon@ATHENA.MIT.EDU) + by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q6P2Z2T7025119 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Tue, 24 Jul 2012 22:35:03 -0400 (EDT) +Received: from amthrax by drake.dyndns.org with local (Exim 4.77) + (envelope-from ) + id 1StrRG-0004Yn-4C; Tue, 24 Jul 2012 22:35:02 -0400 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH 03/13] sprinter: Add a string_len method +Date: Tue, 24 Jul 2012 22:34:43 -0400 +Message-Id: <1343183693-17134-4-git-send-email-amdragon@mit.edu> +X-Mailer: git-send-email 1.7.10 +In-Reply-To: <1343183693-17134-1-git-send-email-amdragon@mit.edu> +References: <1343183693-17134-1-git-send-email-amdragon@mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFtrNIsWRmVeSWpSXmKPExsUixG6nohsZzR9gcPq6lMXehnZGi+s3ZzI7 + MHks3rSfzePZqlvMAUxRXDYpqTmZZalF+nYJXBn3pu1kKjgkWXFso2cD433hLkZODgkBE4kF + y04yQ9hiEhfurWfrYuTiEBLYxyhxbsdTZghnA6PEx2dLGSGcR0wSXw49hnLmMkq03u1gA+ln + E9CQ2LZ/OSOILSIgLbHz7mxWEJtZwExi7s+JQDUcHMICFhL/D4WChFkEVCWOzr7JDhLmFXCQ + ePG5DuIKeYmn9/vAJnIKOEqcX7IQbKIQUEnP4mvsExj5FzAyrGKUTcmt0s1NzMwpTk3WLU5O + zMtLLdI118vNLNFLTSndxAgOIxeVHYzNh5QOMQpwMCrx8P56whcgxJpYVlyZe4hRkoNJSZS3 + Ipg/QIgvKT+lMiOxOCO+qDQntfgQowQHs5IIb1EYUI43JbGyKrUoHyYlzcGiJM57PeWmv5BA + emJJanZqakFqEUxWhoNDSYJ3fxRQo2BRanpqRVpmTglCmomDE2Q4D9Dw3SA1vMUFibnFmekQ + +VOMilLivHNBEgIgiYzSPLheWJy/YhQHekUYop0HmCLgul8BDWYCGvw8jA9kcEkiQkqqgTGz + R9g9LZ3FvLvr8IlfrKYfUx182FeU/fUu0r98vmn6Qc1JP3xadolUyaz7Kel0fM4Z/ZpXmpc7 + DkU7ZM5cY+t6N+b8xBMMnPN93xwNm2+n9L6dO3uZqYnawe6MhJTmxFlmDDX+Hxrm6nQWlr+y + r38s9/Vr7KqX1yKSE8L47veLCz6u+3feUYmlOCPRUIu5qDgRAEkD8YjOAgAA +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Wed, 25 Jul 2012 02:35:19 -0000 + +This method allows callers to output strings with specific lengths. +It's useful both for strings with embedded NULs (which JSON can +represent, though parser support is apparently spotty), and +non-terminated strings. +--- + sprinter-json.c | 11 +++++++++-- + sprinter-text.c | 11 +++++++++-- + sprinter.h | 1 + + 3 files changed, 19 insertions(+), 4 deletions(-) + +diff --git a/sprinter-json.c b/sprinter-json.c +index 4649655..2587ca6 100644 +--- a/sprinter-json.c ++++ b/sprinter-json.c +@@ -89,7 +89,7 @@ json_end (struct sprinter *sp) + } + + static void +-json_string (struct sprinter *sp, const char *val) ++json_string_len (struct sprinter *sp, const char *val, size_t len) + { + static const char *const escapes[] = { + ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b", +@@ -98,7 +98,7 @@ json_string (struct sprinter *sp, const char *val) + struct sprinter_json *spj = json_begin_value (sp); + + fputc ('"', spj->stream); +- for (; *val; ++val) { ++ for (; len; ++val, --len) { + unsigned char ch = *val; + if (ch < ARRAY_SIZE (escapes) && escapes[ch]) + fputs (escapes[ch], spj->stream); +@@ -111,6 +111,12 @@ json_string (struct sprinter *sp, const char *val) + } + + static void ++json_string (struct sprinter *sp, const char *val) ++{ ++ json_string_len (sp, val, strlen (val)); ++} ++ ++static void + json_integer (struct sprinter *sp, int val) + { + struct sprinter_json *spj = json_begin_value (sp); +@@ -166,6 +172,7 @@ sprinter_json_create (const void *ctx, FILE *stream) + .begin_list = json_begin_list, + .end = json_end, + .string = json_string, ++ .string_len = json_string_len, + .integer = json_integer, + .boolean = json_boolean, + .null = json_null, +diff --git a/sprinter-text.c b/sprinter-text.c +index b208840..dfa54b5 100644 +--- a/sprinter-text.c ++++ b/sprinter-text.c +@@ -25,14 +25,20 @@ struct sprinter_text { + }; + + static void +-text_string (struct sprinter *sp, const char *val) ++text_string_len (struct sprinter *sp, const char *val, size_t len) + { + struct sprinter_text *sptxt = (struct sprinter_text *) sp; + + if (sptxt->current_prefix != NULL) + fprintf (sptxt->stream, "%s:", sptxt->current_prefix); + +- fputs(val, sptxt->stream); ++ fwrite (val, len, 1, sptxt->stream); ++} ++ ++static void ++text_string (struct sprinter *sp, const char *val) ++{ ++ text_string_len (sp, val, strlen (val)); + } + + static void +@@ -105,6 +111,7 @@ sprinter_text_create (const void *ctx, FILE *stream) + .begin_list = text_begin_list, + .end = text_end, + .string = text_string, ++ .string_len = text_string_len, + .integer = text_integer, + .boolean = text_boolean, + .null = text_null, +diff --git a/sprinter.h b/sprinter.h +index 6680d41..826a852 100644 +--- a/sprinter.h ++++ b/sprinter.h +@@ -28,6 +28,7 @@ typedef struct sprinter { + * For string, the char * must be UTF-8 encoded. + */ + void (*string) (struct sprinter *, const char *); ++ void (*string_len) (struct sprinter *, const char *, size_t); + void (*integer) (struct sprinter *, int); + void (*boolean) (struct sprinter *, notmuch_bool_t); + void (*null) (struct sprinter *); +-- +1.7.10 +