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/07/09 16:10:21 UTC

[lucy-commits] svn commit: r1144673 - in /incubator/lucy/trunk/clownfish: include/CFC.h lib/Clownfish.xs lib/Clownfish/Binding/Core.pm lib/Clownfish/Binding/Core/File.pm src/CFCBindFile.c src/CFCBindFile.h

Author: marvin
Date: Sat Jul  9 14:10:21 2011
New Revision: 1144673

URL: http://svn.apache.org/viewvc?rev=1144673&view=rev
Log:
Port CFCBindFile to C.

Added:
    incubator/lucy/trunk/clownfish/src/CFCBindFile.c   (with props)
    incubator/lucy/trunk/clownfish/src/CFCBindFile.h
      - copied, changed from r1144672, incubator/lucy/trunk/clownfish/include/CFC.h
Modified:
    incubator/lucy/trunk/clownfish/include/CFC.h
    incubator/lucy/trunk/clownfish/lib/Clownfish.xs
    incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core.pm
    incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/File.pm

Modified: incubator/lucy/trunk/clownfish/include/CFC.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/include/CFC.h?rev=1144673&r1=1144672&r2=1144673&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/include/CFC.h (original)
+++ incubator/lucy/trunk/clownfish/include/CFC.h Sat Jul  9 14:10:21 2011
@@ -32,6 +32,7 @@
 
 #include "CFCBindAliases.h"
 #include "CFCBindClass.h"
+#include "CFCBindFile.h"
 #include "CFCBindFunction.h"
 #include "CFCBindMethod.h"
 

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish.xs?rev=1144673&r1=1144672&r2=1144673&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish.xs Sat Jul  9 14:10:21 2011
@@ -1803,3 +1803,14 @@ CODE:
     RETVAL = S_sv_eat_c_string(CFCBindClass_to_c_header(self));
 OUTPUT: RETVAL
 
+MODULE = Clownfish   PACKAGE = Clownfish::Binding::Core::File
+
+void
+_write_h(file, dest, header, footer)
+    CFCFile *file;
+    const char *dest;
+    const char *header;
+    const char *footer;
+PPCODE:
+    CFCBindFile_write_h(file, dest, header, footer);
+

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core.pm?rev=1144673&r1=1144672&r2=1144673&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core.pm Sat Jul  9 14:10:21 2011
@@ -18,6 +18,9 @@ use warnings;
 
 package Clownfish::Binding::Core;
 use Clownfish::Util qw( a_isa_b verify_args );
+use Clownfish::Binding::Core::Function;
+use Clownfish::Binding::Core::Method;
+use Clownfish::Binding::Core::Class;
 use Clownfish::Binding::Core::File;
 use Clownfish::Binding::Core::Aliases;
 use File::Spec::Functions qw( catfile );

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/File.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/File.pm?rev=1144673&r1=1144672&r2=1144673&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/File.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/File.pm Sat Jul  9 14:10:21 2011
@@ -17,12 +17,7 @@ use strict;
 use warnings;
 
 package Clownfish::Binding::Core::File;
-use Clownfish::Util qw( a_isa_b verify_args make_path is_dir );
-use Clownfish::Binding::Core::Class;
-use File::Spec::Functions qw( catfile splitpath );
-use Scalar::Util qw( blessed );
-use Fcntl;
-use Carp;
+use Clownfish::Util qw( verify_args );
 
 my %write_h_PARAMS = (
     file   => undef,
@@ -34,69 +29,9 @@ my %write_h_PARAMS = (
 sub write_h {
     my ( undef, %args ) = @_;
     verify_args( \%write_h_PARAMS, %args ) or confess $@;
-    my $file = $args{file};
-    confess("Not a Clownfish::File")
-        unless a_isa_b( $file, "Clownfish::File" );
-    my $h_path = $file->h_path( $args{dest} );
-
-    # Unlink then open file.
-    my ( undef, $out_dir, undef ) = splitpath($h_path);
-    make_path($out_dir) unless is_dir($out_dir);
-    confess("Can't make dir '$out_dir'") unless is_dir($out_dir);
-    unlink $h_path;
-    sysopen( my $fh, $h_path, O_CREAT | O_EXCL | O_WRONLY )
-        or confess("Can't open '$h_path' for writing");
-
-    # Create the include-guard strings.
-    my $include_guard_start = $file->guard_start;
-    my $include_guard_close = $file->guard_close;
-
-    # Aggregate block content.
-    my $content = "";
-    for my $block ( @{ $file->blocks } ) {
-        if ( a_isa_b( $block, 'Clownfish::Parcel' ) ) { }
-        elsif ( a_isa_b( $block, 'Clownfish::Class' ) ) {
-            my $class_binding
-                = Clownfish::Binding::Core::Class->new( client => $block, );
-            $content .= $class_binding->to_c_header . "\n";
-        }
-        elsif ( a_isa_b( $block, 'Clownfish::CBlock' ) ) {
-            $content .= $block->get_contents . "\n";
-        }
-        else {
-            confess("Invalid block: $block");
-        }
-    }
-
-    print $fh <<END_STUFF;
-$args{header}
-
-$include_guard_start
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-$content
-
-#ifdef __cplusplus
-}
-#endif
-
-$include_guard_close
-
-$args{footer}
-
-END_STUFF
+    _write_h( @args{qw( file dest header footer )} );
 }
 
-my %write_c_PARAMS = (
-    file   => undef,
-    dest   => undef,
-    header => undef,
-    footer => undef,
-);
-
 1;
 
 __END__

Added: incubator/lucy/trunk/clownfish/src/CFCBindFile.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCBindFile.c?rev=1144673&view=auto
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCBindFile.c (added)
+++ incubator/lucy/trunk/clownfish/src/CFCBindFile.c Sat Jul  9 14:10:21 2011
@@ -0,0 +1,130 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "CFCBindFile.h"
+#include "CFCBindClass.h"
+#include "CFCBase.h"
+#include "CFCFile.h"
+#include "CFCClass.h"
+#include "CFCCBlock.h"
+#include "CFCUtil.h"
+
+#ifdef _WIN32
+#define PATH_SEP "\\"
+#define PATH_SEP_CHAR '\\'
+#else
+#define PATH_SEP "/"
+#define PATH_SEP_CHAR '/'
+#endif
+
+void
+CFCBindFile_write_h(CFCFile *file, const char *dest, const char *header,
+                    const char *footer) {
+    CFCUTIL_NULL_CHECK(file);
+    CFCUTIL_NULL_CHECK(dest);
+    CFCUTIL_NULL_CHECK(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);
+    for (size_t len = h_path_buf_size; len--; ) {
+        if (h_dir[len] == PATH_SEP_CHAR) {
+            h_dir[len] = 0;
+            break;
+        }
+    }
+    if (!CFCUtil_is_dir(h_dir)) {
+        CFCUtil_make_path(h_dir);
+        if (!CFCUtil_is_dir(h_dir)) {
+            CFCUtil_die("Can't make path %s", h_dir);
+        }
+    }
+
+    // Create the include-guard strings.
+    const char *include_guard_start = CFCFile_guard_start(file);
+    const char *include_guard_close = CFCFile_guard_close(file);
+
+    // Aggregate block content.
+    char *content = CFCUtil_strdup("");
+    CFCBase **blocks = CFCFile_blocks(file);
+    for (int i = 0; blocks[i] != NULL; i++) {
+        const char *cfc_class = CFCBase_get_cfc_class(blocks[i]);
+
+        if (strcmp(cfc_class, "Clownfish::Parcel") == 0) {
+            ;
+        }
+        else if (strcmp(cfc_class, "Clownfish::Class") == 0) {
+            CFCBindClass *class_binding
+                = CFCBindClass_new((CFCClass*)blocks[i]);
+            char *c_header = CFCBindClass_to_c_header(class_binding);
+            content = CFCUtil_cat(content, c_header, "\n", NULL);
+            FREEMEM(c_header);
+            CFCBase_decref((CFCBase*)class_binding);
+        }
+        else if (strcmp(cfc_class, "Clownfish::CBlock") == 0) {
+            const char *block_contents 
+                = CFCCBlock_get_contents((CFCCBlock*)blocks[i]);
+            content = CFCUtil_cat(content, block_contents, "\n", NULL);
+        }
+        else {
+            CFCUtil_die("Unexpected class: %s", cfc_class);
+        }
+    }
+
+    char pattern[] = 
+        "%s\n"
+        "\n"
+        "%s\n"
+        "\n"
+        "#ifdef __cplusplus\n"
+        "extern \"C\" {\n"
+        "#endif\n"
+        "\n"
+        "%s\n"
+        "\n"
+        "#ifdef __cplusplus\n"
+        "}\n"
+        "#endif\n"
+        "\n"
+        "%s\n"
+        "\n"
+        "%s\n"
+        "\n";
+
+    size_t size = sizeof(pattern)
+                  + strlen(header)
+                  + strlen(include_guard_start)
+                  + strlen(content)
+                  + strlen(include_guard_close)
+                  + strlen(footer)
+                  + 50;
+    char *file_content = (char*)MALLOCATE(size);
+    sprintf(file_content, pattern, header, include_guard_start, content,
+            include_guard_close, footer);
+
+    // Unlink then write file.
+    remove(h_path);
+    CFCUtil_write_file(h_path, file_content, strlen(file_content));
+
+    FREEMEM(file_content);
+    FREEMEM(h_path);
+}
+

Propchange: incubator/lucy/trunk/clownfish/src/CFCBindFile.c
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: incubator/lucy/trunk/clownfish/src/CFCBindFile.h (from r1144672, incubator/lucy/trunk/clownfish/include/CFC.h)
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCBindFile.h?p2=incubator/lucy/trunk/clownfish/src/CFCBindFile.h&p1=incubator/lucy/trunk/clownfish/include/CFC.h&r1=1144672&r2=1144673&rev=1144673&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/include/CFC.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCBindFile.h Sat Jul  9 14:10:21 2011
@@ -14,24 +14,23 @@
  * limitations under the License.
  */
 
-#include "CFCBase.h"
-#include "CFCCBlock.h"
-#include "CFCClass.h"
-#include "CFCDocuComment.h"
-#include "CFCDumpable.h"
-#include "CFCFile.h"
-#include "CFCFunction.h"
-#include "CFCHierarchy.h"
-#include "CFCMethod.h"
-#include "CFCParamList.h"
-#include "CFCParcel.h"
-#include "CFCSymbol.h"
-#include "CFCType.h"
-#include "CFCUtil.h"
-#include "CFCVariable.h"
-
-#include "CFCBindAliases.h"
-#include "CFCBindClass.h"
-#include "CFCBindFunction.h"
-#include "CFCBindMethod.h"
+#ifndef H_CFCBINDFILE
+#define H_CFCBINDFILE
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct CFCFile;
+
+void
+CFCBindFile_write_h(struct CFCFile *file, const char *dest,
+                    const char *header, const char *footer);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_CFCBINDFILE */
+