You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2012/12/27 19:20:55 UTC
[lucy-commits] [6/12] git commit: refs/heads/cfc-sprintf - Simplify CFCFile_*_path
Simplify CFCFile_*_path
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/0bbc0a75
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/0bbc0a75
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/0bbc0a75
Branch: refs/heads/cfc-sprintf
Commit: 0bbc0a75f145ab89cd26292ea48727dfb2c77f26
Parents: ffbcad3
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Wed Dec 26 21:45:27 2012 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Wed Dec 26 21:45:27 2012 +0100
----------------------------------------------------------------------
clownfish/compiler/perl/lib/Clownfish/CFC.xs | 14 ++---
clownfish/compiler/src/CFCBindFile.c | 6 +--
clownfish/compiler/src/CFCFile.c | 55 ++++++---------------
clownfish/compiler/src/CFCFile.h | 21 ++------
clownfish/compiler/src/CFCHierarchy.c | 8 +--
5 files changed, 32 insertions(+), 72 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy/blob/0bbc0a75/clownfish/compiler/perl/lib/Clownfish/CFC.xs
----------------------------------------------------------------------
diff --git a/clownfish/compiler/perl/lib/Clownfish/CFC.xs b/clownfish/compiler/perl/lib/Clownfish/CFC.xs
index 058f3b9..3a23bd7 100644
--- a/clownfish/compiler/perl/lib/Clownfish/CFC.xs
+++ b/clownfish/compiler/perl/lib/Clownfish/CFC.xs
@@ -591,24 +591,22 @@ ALIAS:
cfh_path = 3
CODE:
{
- size_t buf_size = CFCFile_path_buf_size(self, base_dir);
- RETVAL = newSV(buf_size);
- SvPOK_on(RETVAL);
- char *buf = SvPVX(RETVAL);
+ char *buf;
switch (ix) {
case 1:
- CFCFile_c_path(self, buf, buf_size, base_dir);
+ buf = CFCFile_c_path(self, base_dir);
break;
case 2:
- CFCFile_h_path(self, buf, buf_size, base_dir);
+ buf = CFCFile_h_path(self, base_dir);
break;
case 3:
- CFCFile_cfh_path(self, buf, buf_size, base_dir);
+ buf = CFCFile_cfh_path(self, base_dir);
break;
default:
croak("unexpected ix value: %d", (int)ix);
}
- SvCUR_set(RETVAL, strlen(buf));
+ RETVAL = newSVpvn(buf, strlen(buf));
+ FREEMEM(buf);
}
OUTPUT: RETVAL
http://git-wip-us.apache.org/repos/asf/lucy/blob/0bbc0a75/clownfish/compiler/src/CFCBindFile.c
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCBindFile.c b/clownfish/compiler/src/CFCBindFile.c
index 914c1f5..ad98bb3 100644
--- a/clownfish/compiler/src/CFCBindFile.c
+++ b/clownfish/compiler/src/CFCBindFile.c
@@ -33,10 +33,8 @@ CFCBindFile_write_h(CFCFile *file, const char *dest, const char *header,
CFCUTIL_NULL_CHECK(footer);
// Make directories.
- size_t h_path_buf_size = CFCFile_path_buf_size(file, dest);
- char *h_path = (char*)MALLOCATE(h_path_buf_size);
- CFCFile_h_path(file, h_path, h_path_buf_size, dest);
- char *h_dir = CFCUtil_strdup(h_path);
+ char *h_path = CFCFile_h_path(file, dest);
+ char *h_dir = CFCUtil_strdup(h_path);
for (size_t len = strlen(h_dir); len--;) {
if (h_dir[len] == CFCUTIL_PATH_SEP_CHAR) {
h_dir[len] = 0;
http://git-wip-us.apache.org/repos/asf/lucy/blob/0bbc0a75/clownfish/compiler/src/CFCFile.c
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCFile.c b/clownfish/compiler/src/CFCFile.c
index 6e827d5..6daec86 100644
--- a/clownfish/compiler/src/CFCFile.c
+++ b/clownfish/compiler/src/CFCFile.c
@@ -142,24 +142,16 @@ CFCFile_add_block(CFCFile *self, CFCBase *block) {
}
}
-static void
-S_some_path(CFCFile *self, char *buf, size_t buf_size, const char *base_dir,
- const char *ext) {
- size_t needed = CFCFile_path_buf_size(self, base_dir);
- if (strlen(ext) > 4) {
- CFCUtil_die("ext cannot be more than 4 characters.");
- }
- if (needed > buf_size) {
- CFCUtil_die("Need buf_size of %lu, but got %lu",
- (unsigned long)needed, (unsigned long)buf_size);
- }
+static char*
+S_some_path(CFCFile *self, const char *base_dir, const char *ext) {
const char *path_part = CFCFileSpec_get_path_part(self->spec);
+ char *buf;
if (base_dir) {
- sprintf(buf, "%s" CFCUTIL_PATH_SEP "%s%s", base_dir, path_part,
- ext);
+ buf = CFCUtil_sprintf("%s" CFCUTIL_PATH_SEP "%s%s", base_dir,
+ path_part, ext);
}
else {
- sprintf(buf, "%s%s", path_part, ext);
+ buf = CFCUtil_sprintf("%s%s", path_part, ext);
}
for (size_t i = 0; buf[i] != '\0'; i++) {
#ifdef _WIN32
@@ -168,37 +160,22 @@ S_some_path(CFCFile *self, char *buf, size_t buf_size, const char *base_dir,
if (buf[i] == '\\') { buf[i] = '/'; }
#endif
}
+ return buf;
}
-size_t
-CFCFile_path_buf_size(CFCFile *self, const char *base_dir) {
- const char *path_part = CFCFileSpec_get_path_part(self->spec);
- size_t size = strlen(path_part);
- size += 4; // Max extension length.
- size += 1; // NULL-termination.
- if (base_dir) {
- size += strlen(base_dir);
- size += strlen(CFCUTIL_PATH_SEP);
- }
- return size;
-}
-
-void
-CFCFile_c_path(CFCFile *self, char *buf, size_t buf_size,
- const char *base_dir) {
- S_some_path(self, buf, buf_size, base_dir, ".c");
+char*
+CFCFile_c_path(CFCFile *self, const char *base_dir) {
+ return S_some_path(self, base_dir, ".c");
}
-void
-CFCFile_h_path(CFCFile *self, char *buf, size_t buf_size,
- const char *base_dir) {
- S_some_path(self, buf, buf_size, base_dir, ".h");
+char*
+CFCFile_h_path(CFCFile *self, const char *base_dir) {
+ return S_some_path(self, base_dir, ".h");
}
-void
-CFCFile_cfh_path(CFCFile *self, char *buf, size_t buf_size,
- const char *base_dir) {
- S_some_path(self, buf, buf_size, base_dir, ".cfh");
+char*
+CFCFile_cfh_path(CFCFile *self, const char *base_dir) {
+ return S_some_path(self, base_dir, ".cfh");
}
CFCBase**
http://git-wip-us.apache.org/repos/asf/lucy/blob/0bbc0a75/clownfish/compiler/src/CFCFile.h
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCFile.h b/clownfish/compiler/src/CFCFile.h
index 25ea0f1..e360605 100644
--- a/clownfish/compiler/src/CFCFile.h
+++ b/clownfish/compiler/src/CFCFile.h
@@ -51,30 +51,21 @@ CFCFile_destroy(CFCFile *self);
void
CFCFile_add_block(CFCFile *self, CFCBase *block);
-/** Calculate the size of the buffer needed for a call to c_path(), h_path(),
- * or cfh_path().
- */
-size_t
-CFCFile_path_buf_size(CFCFile *self, const char *base_dir);
-
/** Given a base directory, return a path name derived from the File's
* path_part with a ".c" extension.
*/
-void
-CFCFile_c_path(CFCFile *self, char *buf, size_t buf_size,
- const char *base_dir);
+char*
+CFCFile_c_path(CFCFile *self, const char *base_dir);
/** As c_path, but with a ".h" extension.
*/
-void
-CFCFile_h_path(CFCFile *self, char *buf, size_t buf_size,
- const char *base_dir);
+char*
+CFCFile_h_path(CFCFile *self, const char *base_dir);
/** As c_path, but with a ".cfh" extension.
*/
-void
-CFCFile_cfh_path(CFCFile *self, char *buf, size_t buf_size,
- const char *base_dir);
+char*
+CFCFile_cfh_path(CFCFile *self, const char *base_dir);
/** Return all blocks as an array.
*/
http://git-wip-us.apache.org/repos/asf/lucy/blob/0bbc0a75/clownfish/compiler/src/CFCHierarchy.c
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCHierarchy.c b/clownfish/compiler/src/CFCHierarchy.c
index 949bd67..e98a258 100644
--- a/clownfish/compiler/src/CFCHierarchy.c
+++ b/clownfish/compiler/src/CFCHierarchy.c
@@ -388,12 +388,8 @@ S_do_propagate_modified(CFCHierarchy *self, CFCClass *klass, int modified) {
CFCUTIL_NULL_CHECK(file);
const char *source_dir = CFCFile_get_source_dir(file);
CFCUTIL_NULL_CHECK(source_dir);
- size_t cfh_buf_size = CFCFile_path_buf_size(file, source_dir);
- char *source_path = (char*)MALLOCATE(cfh_buf_size);
- CFCFile_cfh_path(file, source_path, cfh_buf_size, source_dir);
- size_t h_buf_size = CFCFile_path_buf_size(file, self->dest);
- char *h_path = (char*)MALLOCATE(h_buf_size);
- CFCFile_h_path(file, h_path, h_buf_size, self->dest);
+ char *source_path = CFCFile_cfh_path(file, source_dir);
+ char *h_path = CFCFile_h_path(file, self->dest);
if (!CFCUtil_current(source_path, h_path)) {
modified = true;