You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2011/02/19 23:33:35 UTC

[lucy-commits] svn commit: r1072453 - in /incubator/lucy/trunk/clownfish: lib/Clownfish.xs lib/Clownfish/File.pm src/CFCFile.c src/CFCFile.h

Author: marvin
Date: Sat Feb 19 22:33:35 2011
New Revision: 1072453

URL: http://svn.apache.org/viewvc?rev=1072453&view=rev
Log:
Move include guard code for CFCFile from Perl to C.

Modified:
    incubator/lucy/trunk/clownfish/lib/Clownfish.xs
    incubator/lucy/trunk/clownfish/lib/Clownfish/File.pm
    incubator/lucy/trunk/clownfish/src/CFCFile.c
    incubator/lucy/trunk/clownfish/src/CFCFile.h

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish.xs?rev=1072453&r1=1072452&r2=1072453&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish.xs Sat Feb 19 22:33:35 2011
@@ -216,6 +216,9 @@ ALIAS:
     set_modified       = 1
     get_modified       = 2
     get_source_class   = 4
+    guard_name         = 6
+    guard_start        = 8
+    guard_close        = 10 
 PPCODE:
 {
     START_SET_OR_GET_SWITCH
@@ -230,6 +233,21 @@ PPCODE:
                 retval = newSVpv(value, strlen(value));
             }
             break;
+        case 6: {
+                const char *value = CFCFile_guard_name(self);
+                retval = newSVpv(value, strlen(value));
+            }
+            break;
+        case 8: {
+                const char *value = CFCFile_guard_start(self);
+                retval = newSVpv(value, strlen(value));
+            }
+            break;
+        case 10: {
+                const char *value = CFCFile_guard_close(self);
+                retval = newSVpv(value, strlen(value));
+            }
+            break;
     END_SET_OR_GET_SWITCH
 }
 

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/File.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/File.pm?rev=1072453&r1=1072452&r2=1072453&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/File.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/File.pm Sat Feb 19 22:33:35 2011
@@ -68,29 +68,6 @@ sub classes {
     return \@classes;
 }
 
-# Return a string used for an include guard, unique per file.
-sub guard_name {
-    my $self       = shift;
-    my $guard_name = "H_" . uc( $self->get_source_class );
-    $guard_name =~ s/\W+/_/g;
-    return $guard_name;
-}
-
-# Return a string opening the include guard.
-sub guard_start {
-    my $self       = shift;
-    my $guard_name = $self->guard_name;
-    return "#ifndef $guard_name\n#define $guard_name 1\n";
-}
-
-# Return a string closing the include guard.  Other classes count on being
-# able to match this string.
-sub guard_close {
-    my $self       = shift;
-    my $guard_name = $self->guard_name;
-    return "#endif /\* $guard_name \*/\n";
-}
-
 sub c_path   { return $_[0]->_some_path( $_[1], '.c' ) }
 sub h_path   { return $_[0]->_some_path( $_[1], '.h' ) }
 sub cfh_path { return $_[0]->_some_path( $_[1], '.cfh' ) }

Modified: incubator/lucy/trunk/clownfish/src/CFCFile.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCFile.c?rev=1072453&r1=1072452&r2=1072453&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCFile.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCFile.c Sat Feb 19 22:33:35 2011
@@ -33,6 +33,9 @@ struct CFCFile {
     CFCBase base;
     int modified;
     char *source_class;
+    char *guard_name;
+    char *guard_start;
+    char *guard_close;
 };
 
 CFCFile*
@@ -50,12 +53,45 @@ CFCFile_init(CFCFile *self, const char *
     CFCUTIL_NULL_CHECK(source_class);
     self->modified = false;
     self->source_class = CFCUtil_strdup(source_class);
+
+    // Derive include guard name, plus C code for opening and closing the
+    // guard.
+    size_t len = strlen(source_class);
+    self->guard_name = (char*)malloc(len + sizeof("H_") + 1);
+    self->guard_start = (char*)malloc(len * 2 + 40);
+    self->guard_close = (char*)malloc(len + 20);
+    if (!self->guard_name || !self->guard_start || !self->guard_close) {
+        croak("malloc failed");
+    }
+    memcpy(self->guard_name, "H_", 2);
+    size_t i, j;
+    for (i = 0, j = 2; i < len; i++, j++) {
+        char c = source_class[i];
+        if (c == ':') {
+            self->guard_name[j] = '_';
+            i++;
+        }
+        else {
+            self->guard_name[j] = toupper(source_class[i]);
+        }
+    }
+    self->guard_name[j] = '\0';
+    int check = sprintf(self->guard_start, "#ifndef %s\n#define %s 1\n", 
+        self->guard_name, self->guard_name);
+    if (check < 0) { croak("sprintf failed"); }
+    check = sprintf(self->guard_close, "#endif /* %s */\n", 
+        self->guard_name);
+    if (check < 0) { croak("sprintf failed"); }
+
     return self;
 }
 
 void
 CFCFile_destroy(CFCFile *self)
 {
+    free(self->guard_name);
+    free(self->guard_start);
+    free(self->guard_close);
     free(self->source_class);
     CFCBase_destroy((CFCBase*)self);
 }
@@ -78,3 +114,21 @@ CFCFile_get_source_class(CFCFile *self)
     return self->source_class;
 }
 
+const char*
+CFCFile_guard_name(CFCFile *self)
+{
+    return self->guard_name;
+}
+
+const char*
+CFCFile_guard_start(CFCFile *self)
+{
+    return self->guard_start;
+}
+
+const char*
+CFCFile_guard_close(CFCFile *self)
+{
+    return self->guard_close;
+}
+

Modified: incubator/lucy/trunk/clownfish/src/CFCFile.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCFile.h?rev=1072453&r1=1072452&r2=1072453&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCFile.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCFile.h Sat Feb 19 22:33:35 2011
@@ -37,5 +37,14 @@ CFCFile_get_modified(CFCFile *self);
 const char*
 CFCFile_get_source_class(CFCFile *self);
 
+const char*
+CFCFile_guard_name(CFCFile *self);
+
+const char*
+CFCFile_guard_start(CFCFile *self);
+
+const char*
+CFCFile_guard_close(CFCFile *self);
+
 #endif /* H_CFCFILE */