All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Torsten Bögershausen" <tboegi@web.de>
To: git@vger.kernel.org
Cc: pclouds@gmail.com, tboegi@web.de
Subject: [PATCH v2] precompompose_utf8.c: iconv_open_or_die
Date: Wed, 22 Aug 2012 22:00:07 +0200	[thread overview]
Message-ID: <201208222200.08803.tboegi@web.de> (raw)

Add the function iconv_open_or_die(), which is used in both
readdir() and precompose_argv()

Optimized away one save/restore of errnor, which is no longer nedded

Signed-off-by: Torsten Bögershausen <tboegi@web.de>
---
 compat/precompose_utf8.c | 59 ++++++++++++++++++++++--------------------------
 1 file changed, 27 insertions(+), 32 deletions(-)

diff --git a/compat/precompose_utf8.c b/compat/precompose_utf8.c
index e6c6bf2..d8b189d 100644
--- a/compat/precompose_utf8.c
+++ b/compat/precompose_utf8.c
@@ -13,6 +13,15 @@ typedef char *iconv_ibp;
 static const char *repo_encoding = "UTF-8";
 static const char *path_encoding = "UTF-8-MAC";
 
+static iconv_t iconv_open_or_die(const char* tocode, const char* fromcode)
+{
+	iconv_t my_iconv;
+	my_iconv = iconv_open(tocode, fromcode);
+	if (my_iconv == (iconv_t) -1)
+		die_errno(_("iconv_open(%s,%s) failed"), repo_encoding, path_encoding);
+	return my_iconv;
+}
+
 static size_t has_utf8(const char *s, size_t maxlen, size_t *strlen_c)
 {
 	const uint8_t *utf8p = (const uint8_t *)s;
@@ -78,9 +87,7 @@ void precompose_argv(int argc, const char **argv)
 	if (argc <= i)
 		return; /* no utf8 found */
 
-	ic_precompose = iconv_open(repo_encoding, path_encoding);
-	if (ic_precompose == (iconv_t) -1)
-		return;
+	ic_precompose = iconv_open_or_die(repo_encoding, path_encoding);
 
 	for (i = 0; i < argc; i++) {
 		size_t namelen;
@@ -106,11 +113,8 @@ PREC_DIR *precompose_utf8_opendir(const char *dirname)
 		free(prec_dir->dirent_nfc);
 		free(prec_dir);
 		return NULL;
-	} else {
-		int ret_errno = errno;
+	} else
 		prec_dir->ic_precompose = (iconv_t)-1;
-		errno = ret_errno;
-	}
 
 	return prec_dir;
 }
@@ -137,32 +141,23 @@ struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *prec_dir)
 		prec_dir->dirent_nfc->d_type = res->d_type;
 
 		if ((precomposed_unicode == 1) && has_utf8(res->d_name, (size_t)-1, NULL)) {
+			iconv_ibp cp = (iconv_ibp)res->d_name;
+			size_t inleft = namelenz;
+			char *outpos = &prec_dir->dirent_nfc->d_name[0];
+			size_t outsz = prec_dir->dirent_nfc->max_name_len;
+			size_t cnt;
+			errno = 0;
 			if (prec_dir->ic_precompose == (iconv_t)-1)
-				prec_dir->ic_precompose =
-					iconv_open(repo_encoding, path_encoding);
-			if (prec_dir->ic_precompose == (iconv_t)-1) {
-				die("iconv_open(%s,%s) failed, but needed:\n"
-						"    precomposed unicode is not supported.\n"
-						"    If you wnat to use decomposed unicode, run\n"
-						"    \"git config core.precomposeunicode false\"\n",
-						repo_encoding, path_encoding);
-			} else {
-				iconv_ibp	cp = (iconv_ibp)res->d_name;
-				size_t inleft = namelenz;
-				char *outpos = &prec_dir->dirent_nfc->d_name[0];
-				size_t outsz = prec_dir->dirent_nfc->max_name_len;
-				size_t cnt;
-				errno = 0;
-				cnt = iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);
-				if (errno || inleft) {
-					/*
-					 * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF
-					 * MacOS X avoids illegal byte sequemces.
-					 * If they occur on a mounted drive (e.g. NFS) it is not worth to
-					 * die() for that, but rather let the user see the original name
-					*/
-					namelenz = 0; /* trigger strlcpy */
-				}
+				prec_dir->ic_precompose =	iconv_open_or_die(repo_encoding, path_encoding);
+			cnt = iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);
+			if (errno || inleft) {
+				/*
+				 * iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF
+				 * MacOS X avoids illegal byte sequemces.
+				 * If they occur on a mounted drive (e.g. NFS) it is not worth to
+				 * die() for that, but rather let the user see the original name
+				 */
+				namelenz = 0; /* trigger strlcpy */
 			}
 		} else
 			namelenz = 0;
-- 
1.7.12

                 reply	other threads:[~2012-08-22 20:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201208222200.08803.tboegi@web.de \
    --to=tboegi@web.de \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.