Implement class CFCDocument for standalone documentation

Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/5cdc2b94
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/5cdc2b94
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/5cdc2b94

Branch: refs/heads/master
Commit: 5cdc2b9499740f843fc500a7f9622163a8db149d
Parents: 692f8ad
Author: Nick Wellnhofer <wellnho...@aevum.de>
Authored: Wed Jul 8 14:40:24 2015 +0200
Committer: Nick Wellnhofer <wellnho...@aevum.de>
Committed: Sat Jul 11 14:54:59 2015 +0200

----------------------------------------------------------------------
 compiler/include/CFC.h      |   1 +
 compiler/src/CFCDocument.c  | 141 +++++++++++++++++++++++++++++++++++++++
 compiler/src/CFCDocument.h  |  62 +++++++++++++++++
 compiler/src/CFCHierarchy.c |  27 +++++++-
 4 files changed, 230 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5cdc2b94/compiler/include/CFC.h
----------------------------------------------------------------------
diff --git a/compiler/include/CFC.h b/compiler/include/CFC.h
index 486aec0..e57c873 100644
--- a/compiler/include/CFC.h
+++ b/compiler/include/CFC.h
@@ -19,6 +19,7 @@
 #include "CFCCallable.h"
 #include "CFCClass.h"
 #include "CFCDocuComment.h"
+#include "CFCDocument.h"
 #include "CFCFile.h"
 #include "CFCFileSpec.h"
 #include "CFCFunction.h"

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5cdc2b94/compiler/src/CFCDocument.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCDocument.c b/compiler/src/CFCDocument.c
new file mode 100644
index 0000000..8993b9c
--- /dev/null
+++ b/compiler/src/CFCDocument.c
@@ -0,0 +1,141 @@
+/* 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 "charmony.h"
+
+#include <string.h>
+
+#define CFC_NEED_BASE_STRUCT_DEF
+#include "CFCBase.h"
+#include "CFCDocument.h"
+#include "CFCUtil.h"
+
+struct CFCDocument {
+    CFCBase base;
+    char *path;
+    char *path_part;
+    char *name;
+};
+
+static const CFCMeta CFCDOCUMENT_META = {
+    "Clownfish::CFC::Model::Document",
+    sizeof(CFCDocument),
+    (CFCBase_destroy_t)CFCDocument_destroy
+};
+
+static CFCDocument **registry = NULL;
+static size_t registry_size = 0;
+static size_t registry_cap  = 0;
+
+static void
+S_register(CFCDocument *self);
+
+CFCDocument*
+CFCDocument_create(const char *path, const char *path_part) {
+    CFCDocument *self = (CFCDocument*)CFCBase_allocate(&CFCDOCUMENT_META);
+    return CFCDocument_do_create(self, path, path_part);
+}
+
+CFCDocument*
+CFCDocument_do_create(CFCDocument *self, const char *path,
+                      const char *path_part) {
+    self->path      = CFCUtil_strdup(path);
+    self->path_part = CFCUtil_strdup(path_part);
+
+    if (CHY_DIR_SEP_CHAR != '/') {
+        for (size_t i = 0; self->path_part[i]; i++) {
+            if (self->path_part[i] == '/') {
+                self->path_part[i] = CHY_DIR_SEP_CHAR;
+            }
+        }
+    }
+
+    const char *last_dir_sep = strrchr(self->path_part, CHY_DIR_SEP_CHAR);
+    self->name = CFCUtil_strdup(last_dir_sep + 1);
+
+    S_register(self);
+
+    return self;
+}
+
+void
+CFCDocument_destroy(CFCDocument *self) {
+    FREEMEM(self->path);
+    FREEMEM(self->path_part);
+    FREEMEM(self->name);
+}
+
+static void
+S_register(CFCDocument *self) {
+    if (CFCDocument_fetch(self->name) != NULL) {
+        CFCUtil_die("Two documents with name %s", self->name);
+    }
+
+    if (registry_size == registry_cap) {
+        size_t new_cap = registry_cap + 10;
+        size_t bytes   = (new_cap + 1) * sizeof(CFCDocument*);
+        registry = (CFCDocument**)REALLOCATE(registry, bytes);
+        registry_cap = new_cap;
+    }
+
+    registry[registry_size]   = (CFCDocument*)CFCBase_incref((CFCBase*)self);
+    registry[registry_size+1] = NULL;
+    registry_size++;
+}
+
+CFCDocument**
+CFCDocument_get_registry() {
+    if (registry == NULL) {
+        registry = (CFCDocument**)CALLOCATE(1, sizeof(CFCDocument*));
+    }
+
+    return registry;
+}
+
+CFCDocument*
+CFCDocument_fetch(const char *name) {
+    for (size_t i = 0; i < registry_size; i++) {
+        CFCDocument *doc = registry[i];
+
+        if (strcmp(doc->name, name) == 0) {
+            return doc;
+        }
+    }
+
+    return NULL;
+}
+
+char*
+CFCDocument_get_contents(CFCDocument *self) {
+    size_t len;
+    return CFCUtil_slurp_text(self->path, &len);
+}
+
+const char*
+CFCDocument_get_path(CFCDocument *self) {
+    return self->path;
+}
+
+const char*
+CFCDocument_get_path_part(CFCDocument *self) {
+    return self->path_part;
+}
+
+const char*
+CFCDocument_get_name(CFCDocument *self) {
+    return self->name;
+}
+

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5cdc2b94/compiler/src/CFCDocument.h
----------------------------------------------------------------------
diff --git a/compiler/src/CFCDocument.h b/compiler/src/CFCDocument.h
new file mode 100644
index 0000000..5a32cf5
--- /dev/null
+++ b/compiler/src/CFCDocument.h
@@ -0,0 +1,62 @@
+/* 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.
+ */
+
+/** Clownfish::CFC::Model::Document - A standalone documentatio file.
+ */
+
+#ifndef H_CFCDOCUMENT
+#define H_CFCDOCUMENT
+
+#ifdef __cplusplus
+extern "C";
+#endif
+
+typedef struct CFCDocument CFCDocument;
+
+CFCDocument*
+CFCDocument_create(const char *path, const char *path_part);
+
+CFCDocument*
+CFCDocument_do_create(CFCDocument *self, const char *path,
+                      const char *path_part);
+
+void
+CFCDocument_destroy(CFCDocument *self);
+
+CFCDocument**
+CFCDocument_get_registry(void);
+
+CFCDocument*
+CFCDocument_fetch(const char *name);
+
+char*
+CFCDocument_get_contents(CFCDocument *self);
+
+const char*
+CFCDocument_get_path(CFCDocument *self);
+
+const char*
+CFCDocument_get_path_part(CFCDocument *self);
+
+const char*
+CFCDocument_get_name(CFCDocument *self);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_CFCDOCUMENT */
+

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5cdc2b94/compiler/src/CFCHierarchy.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCHierarchy.c b/compiler/src/CFCHierarchy.c
index a9bda94..57d3bfc 100644
--- a/compiler/src/CFCHierarchy.c
+++ b/compiler/src/CFCHierarchy.c
@@ -38,6 +38,7 @@
 #include "CFCSymbol.h"
 #include "CFCUtil.h"
 #include "CFCParser.h"
+#include "CFCDocument.h"
 
 struct CFCHierarchy {
     CFCBase base;
@@ -79,6 +80,9 @@ static void
 S_parse_cf_files(CFCHierarchy *self, const char *source_dir, int is_included);
 
 static void
+S_find_doc_files(const char *source_dir);
+
+static void
 S_find_files(const char *path, void *arg);
 
 static char*
@@ -228,9 +232,10 @@ CFCHierarchy_build(CFCHierarchy *self) {
 
     S_check_prereqs(self);
 
-    // Read .cfh files.
+    // Read .cfh and .md files.
     for (size_t i = 0; self->sources[i] != NULL; i++) {
         S_parse_cf_files(self, self->sources[i], false);
+        S_find_doc_files(self->sources[i]);
     }
     for (size_t i = 0; self->includes[i] != NULL; i++) {
         S_parse_cf_files(self, self->includes[i], true);
@@ -372,6 +377,26 @@ S_parse_cf_files(CFCHierarchy *self, const char 
*source_dir, int is_included) {
 }
 
 static void
+S_find_doc_files(const char *source_dir) {
+    CFCFindFilesContext context;
+    context.ext       = ".md";
+    context.paths     = (char**)CALLOCATE(1, sizeof(char*));
+    context.num_paths = 0;
+    CFCUtil_walk(source_dir, S_find_files, &context);
+
+    for (int i = 0; context.paths[i] != NULL; i++) {
+        char *path = context.paths[i];
+        char *path_part = S_extract_path_part(path, source_dir, ".md");
+        CFCDocument *doc = CFCDocument_create(path, path_part);
+
+        CFCBase_decref((CFCBase*)doc);
+        FREEMEM(path_part);
+    }
+
+    CFCUtil_free_string_array(context.paths);
+}
+
+static void
 S_find_files(const char *path, void *arg) {
     CFCFindFilesContext *context = (CFCFindFilesContext*)arg;
     const char  *ext       = context->ext;

Reply via email to