[78940] branches/gsoc11-rev-upgrade/base/src/cregistry

cal at macports.org cal at macports.org
Fri May 27 12:28:32 PDT 2011


Revision: 78940
          http://trac.macports.org/changeset/78940
Author:   cal at macports.org
Date:     2011-05-27 12:28:29 -0700 (Fri, 27 May 2011)
Log Message:
-----------
rev-upgrade: Moved utility functions from entry.{h,c} to util.{h,c} to prepare introduction of a new type file

Modified Paths:
--------------
    branches/gsoc11-rev-upgrade/base/src/cregistry/Makefile
    branches/gsoc11-rev-upgrade/base/src/cregistry/entry.c
    branches/gsoc11-rev-upgrade/base/src/cregistry/entry.h

Added Paths:
-----------
    branches/gsoc11-rev-upgrade/base/src/cregistry/util.c
    branches/gsoc11-rev-upgrade/base/src/cregistry/util.h

Modified: branches/gsoc11-rev-upgrade/base/src/cregistry/Makefile
===================================================================
--- branches/gsoc11-rev-upgrade/base/src/cregistry/Makefile	2011-05-27 18:16:17 UTC (rev 78939)
+++ branches/gsoc11-rev-upgrade/base/src/cregistry/Makefile	2011-05-27 19:28:29 UTC (rev 78940)
@@ -1,6 +1,6 @@
 # $Id$
 
-OBJS = registry.o entry.o sql.o vercomp.o
+OBJS = registry.o entry.o sql.o vercomp.o util.o
 STLIB_NAME = cregistry.a
 RANLIB = ranlib
 

Modified: branches/gsoc11-rev-upgrade/base/src/cregistry/entry.c
===================================================================
--- branches/gsoc11-rev-upgrade/base/src/cregistry/entry.c	2011-05-27 18:16:17 UTC (rev 78939)
+++ branches/gsoc11-rev-upgrade/base/src/cregistry/entry.c	2011-05-27 19:28:29 UTC (rev 78940)
@@ -33,10 +33,11 @@
 #include "entry.h"
 #include "registry.h"
 #include "sql.h"
+#include "util.h"
 
-#include <string.h>
-#include <stdlib.h>
 #include <sqlite3.h>
+#include <stdlib.h>
+#include <string.h>
 
 /*
  * TODO: possibly, allow reg_entry_search to take different matching strategies
@@ -52,9 +53,6 @@
  *       always have very discrete values. These could be more efficiently dealt
  *       with as integers.
  *
- * TODO: move the utility functions to util.h or something. Not important until
- *       there are more types in the registry than entry, though.
- *
  * TODO: considering a "weak" flag in registry.files. The meaning of this would
  *       be "I wish for my version of this file to be activated when I am, but
  *       not to be deactivated when I am; nor should other ports be prevented
@@ -68,85 +66,6 @@
  */
 
 /**
- * Concatenates `src` to string `dst`. Simple concatenation. Only guaranteed to
- * work with strings that have been allocated with `malloc`. Amortizes cost of
- * expanding string buffer for O(N) concatenation and such. Uses `memcpy` in
- * favor of `strcpy` in hopes it will perform a bit better.
- *
- * @param [in,out] dst       a reference to a null-terminated string
- * @param [in,out] dst_len   number of characters currently in `dst`
- * @param [in,out] dst_space number of characters `dst` can hold
- * @param [in] src           string to concatenate to `dst`
- */
-static int reg_strcat(char** dst, size_t* dst_len, size_t* dst_space, char* src) {
-    size_t src_len = strlen(src);
-    size_t result_len = *dst_len + src_len;
-    if (result_len > *dst_space) {
-        char* new_dst;
-        *dst_space *= 2;
-        if (*dst_space < result_len) {
-            *dst_space = result_len;
-        }
-        new_dst = realloc(*dst, *dst_space * sizeof(char) + 1);
-        if (!new_dst)
-            return 0;
-        else
-            *dst = new_dst;
-    }
-    memcpy(*dst + *dst_len, src, src_len+1);
-    *dst_len = result_len;
-    return 1;
-}
-
-/**
- * Appends element `src` to the list `dst`. It's like `reg_strcat`, except `src`
- * represents a single element and not a sequence of `char`s.
- *
- * @param [in,out] dst       a reference to a list of pointers
- * @param [in,out] dst_len   number of elements currently in `dst`
- * @param [in,out] dst_space number of elements `dst` can hold
- * @param [in] src           elements to append to `dst`
- */
-static int reg_listcat(void*** dst, int* dst_len, int* dst_space, void* src) {
-    if (*dst_len == *dst_space) {
-        void** new_dst;
-        *dst_space *= 2;
-        new_dst = realloc(*dst, *dst_space * sizeof(void*));
-        if (!new_dst)
-            return 0;
-        else
-            *dst = new_dst;
-    }
-    (*dst)[*dst_len] = src;
-    (*dst_len)++;
-    return 1;
-}
-
-/**
- * Returns an expression to use for the given strategy. This should be passed as
- * the `fmt` argument of `sqlite3_mprintf`, with the key and value following.
- *
- * @param [in] strategy a strategy (one of the `reg_strategy_*` constants)
- * @param [out] errPtr  on error, a description of the error that occurred
- * @return              a sqlite3 expression if success; NULL if failure
- */
-static char* reg_strategy_op(reg_strategy strategy, reg_error* errPtr) {
-    switch (strategy) {
-        case reg_strategy_exact:
-            return "%q = '%q'";
-        case reg_strategy_glob:
-            return "%q GLOB '%q'";
-        case reg_strategy_regexp:
-            return "REGEXP(%q, '%q')";
-        default:
-            errPtr->code = REG_INVALID;
-            errPtr->description = "invalid matching strategy specified";
-            errPtr->free = NULL;
-            return NULL;
-    }
-}
-
-/**
  * Converts a `sqlite3_stmt` into a `reg_entry`. The first column of the stmt's
  * row must be the id of an entry; the second either `SQLITE_NULL` or the
  * address of the entry in memory.
@@ -409,72 +328,6 @@
 }
 
 /**
- * Convenience method for returning all objects of a given type from the
- * registry.
- *
- * @param [in] reg       registry to select objects from
- * @param [in] query     the select query to execute
- * @param [in] query_len length of the query (or -1 for automatic)
- * @param [out] objects  the objects selected
- * @param [in] fn        a function to convert sqlite3_stmts to the desired type
- * @param [in] del       a function to delete the desired type of object
- * @param [out] errPtr   on error, a description of the error that occurred
- * @return               the number of objects if success; negative if failure
- */
-static int reg_all_objects(reg_registry* reg, char* query, int query_len,
-        void*** objects, cast_function* fn, free_function* del,
-        reg_error* errPtr) {
-    void** results = malloc(10*sizeof(void*));
-    int result_count = 0;
-    int result_space = 10;
-    sqlite3_stmt* stmt = NULL;
-    if (!results || !fn) {
-        return -1;
-    }
-    if (sqlite3_prepare(reg->db, query, query_len, &stmt, NULL) == SQLITE_OK) {
-        int r;
-        reg_entry* entry;
-        do {
-            r = sqlite3_step(stmt);
-            switch (r) {
-                case SQLITE_ROW:
-                    if (fn(reg, (void**)&entry, stmt, errPtr)) {
-                        if (!reg_listcat(&results, &result_count, &result_space, entry)) {
-                            r = SQLITE_ERROR;
-                        }
-                    } else {
-                        r = SQLITE_ERROR;
-                    }
-                    break;
-                case SQLITE_DONE:
-                case SQLITE_BUSY:
-                    break;
-                default:
-                    reg_sqlite_error(reg->db, errPtr, query);
-                    break;
-            }
-        } while (r == SQLITE_ROW || r == SQLITE_BUSY);
-        sqlite3_finalize(stmt);
-        if (r == SQLITE_DONE) {
-            *objects = results;
-            return result_count;
-        } else if (del) {
-            int i;
-            for (i=0; i<result_count; i++) {
-                del(NULL, results[i]);
-            }
-        }
-    } else {
-        if (stmt) {
-            sqlite3_finalize(stmt);
-        }
-        reg_sqlite_error(reg->db, errPtr, query);
-    }
-    free(results);
-    return -1;
-}
-
-/**
  * Type-safe version of `reg_all_objects` for `reg_entry`.
  *
  * @param [in] reg       registry to select entries from

Modified: branches/gsoc11-rev-upgrade/base/src/cregistry/entry.h
===================================================================
--- branches/gsoc11-rev-upgrade/base/src/cregistry/entry.h	2011-05-27 18:16:17 UTC (rev 78939)
+++ branches/gsoc11-rev-upgrade/base/src/cregistry/entry.h	2011-05-27 19:28:29 UTC (rev 78940)
@@ -36,12 +36,6 @@
 
 #include <sqlite3.h>
 
-typedef enum {
-    reg_strategy_exact = 1,
-    reg_strategy_glob = 2,
-    reg_strategy_regexp = 3
-} reg_strategy;
-
 typedef struct {
     sqlite_int64 id; /* rowid in database */
     reg_registry* reg; /* associated registry */

Added: branches/gsoc11-rev-upgrade/base/src/cregistry/util.c
===================================================================
--- branches/gsoc11-rev-upgrade/base/src/cregistry/util.c	                        (rev 0)
+++ branches/gsoc11-rev-upgrade/base/src/cregistry/util.c	2011-05-27 19:28:29 UTC (rev 78940)
@@ -0,0 +1,181 @@
+/*
+ * util.h
+ * vim:tw=80:expandtab
+ * $Id$
+ *
+ * Copyright (c) 2007 Chris Pickel <sfiera at macports.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "util.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * Concatenates `src` to string `dst`. Simple concatenation. Only guaranteed to
+ * work with strings that have been allocated with `malloc`. Amortizes cost of
+ * expanding string buffer for O(N) concatenation and such. Uses `memcpy` in
+ * favor of `strcpy` in hopes it will perform a bit better.
+ *
+ * @param [in,out] dst       a reference to a null-terminated string
+ * @param [in,out] dst_len   number of characters currently in `dst`
+ * @param [in,out] dst_space number of characters `dst` can hold
+ * @param [in] src           string to concatenate to `dst`
+ */
+int reg_strcat(char** dst, size_t* dst_len, size_t* dst_space, char* src) {
+    size_t src_len = strlen(src);
+    size_t result_len = *dst_len + src_len;
+    if (result_len > *dst_space) {
+        char* new_dst;
+        *dst_space *= 2;
+        if (*dst_space < result_len) {
+            *dst_space = result_len;
+        }
+        new_dst = realloc(*dst, *dst_space * sizeof(char) + 1);
+        if (!new_dst)
+            return 0;
+        else
+            *dst = new_dst;
+    }
+    memcpy(*dst + *dst_len, src, src_len+1);
+    *dst_len = result_len;
+    return 1;
+}
+
+/**
+ * Appends element `src` to the list `dst`. It's like `reg_strcat`, except `src`
+ * represents a single element and not a sequence of `char`s.
+ *
+ * @param [in,out] dst       a reference to a list of pointers
+ * @param [in,out] dst_len   number of elements currently in `dst`
+ * @param [in,out] dst_space number of elements `dst` can hold
+ * @param [in] src           elements to append to `dst`
+ */
+int reg_listcat(void*** dst, int* dst_len, int* dst_space, void* src) {
+    if (*dst_len == *dst_space) {
+        void** new_dst;
+        *dst_space *= 2;
+        new_dst = realloc(*dst, *dst_space * sizeof(void*));
+        if (!new_dst)
+            return 0;
+        else
+            *dst = new_dst;
+    }
+    (*dst)[*dst_len] = src;
+    (*dst_len)++;
+    return 1;
+}
+
+/**
+ * Returns an expression to use for the given strategy. This should be passed as
+ * the `fmt` argument of `sqlite3_mprintf`, with the key and value following.
+ *
+ * @param [in] strategy a strategy (one of the `reg_strategy_*` constants)
+ * @param [out] errPtr  on error, a description of the error that occurred
+ * @return              a sqlite3 expression if success; NULL if failure
+ */
+char* reg_strategy_op(reg_strategy strategy, reg_error* errPtr) {
+    switch (strategy) {
+        case reg_strategy_exact:
+            return "%q = '%q'";
+        case reg_strategy_glob:
+            return "%q GLOB '%q'";
+        case reg_strategy_regexp:
+            return "REGEXP(%q, '%q')";
+        case reg_strategy_null:
+            return "%q = NULL";
+        default:
+            errPtr->code = REG_INVALID;
+            errPtr->description = "invalid matching strategy specified";
+            errPtr->free = NULL;
+            return NULL;
+    }
+}
+
+/**
+ * Convenience method for returning all objects of a given type from the
+ * registry.
+ *
+ * @param [in] reg       registry to select objects from
+ * @param [in] query     the select query to execute
+ * @param [in] query_len length of the query (or -1 for automatic)
+ * @param [out] objects  the objects selected
+ * @param [in] fn        a function to convert sqlite3_stmts to the desired type
+ * @param [in] del       a function to delete the desired type of object
+ * @param [out] errPtr   on error, a description of the error that occurred
+ * @return               the number of objects if success; negative if failure
+ */
+int reg_all_objects(reg_registry* reg, char* query, int query_len,
+        void*** objects, cast_function* fn, free_function* del,
+        reg_error* errPtr) {
+    void** results = malloc(10*sizeof(void*));
+    int result_count = 0;
+    int result_space = 10;
+    sqlite3_stmt* stmt = NULL;
+    if (!results || !fn) {
+        return -1;
+    }
+    if (sqlite3_prepare(reg->db, query, query_len, &stmt, NULL) == SQLITE_OK) {
+        int r;
+        void* row;
+        do {
+            r = sqlite3_step(stmt);
+            switch (r) {
+                case SQLITE_ROW:
+                    if (fn(reg, &row, stmt, errPtr)) {
+                        if (!reg_listcat(&results, &result_count, &result_space, row)) {
+                            r = SQLITE_ERROR;
+                        }
+                    } else {
+                        r = SQLITE_ERROR;
+                    }
+                    break;
+                case SQLITE_DONE:
+                case SQLITE_BUSY:
+                    break;
+                default:
+                    reg_sqlite_error(reg->db, errPtr, query);
+                    break;
+            }
+        } while (r == SQLITE_ROW || r == SQLITE_BUSY);
+        sqlite3_finalize(stmt);
+        if (r == SQLITE_DONE) {
+            *objects = results;
+            return result_count;
+        } else if (del) {
+            int i;
+            for (i=0; i<result_count; i++) {
+                del(NULL, results[i]);
+            }
+        }
+    } else {
+        if (stmt) {
+            sqlite3_finalize(stmt);
+        }
+        reg_sqlite_error(reg->db, errPtr, query);
+    }
+    free(results);
+    return -1;
+}
+

Added: branches/gsoc11-rev-upgrade/base/src/cregistry/util.h
===================================================================
--- branches/gsoc11-rev-upgrade/base/src/cregistry/util.h	                        (rev 0)
+++ branches/gsoc11-rev-upgrade/base/src/cregistry/util.h	2011-05-27 19:28:29 UTC (rev 78940)
@@ -0,0 +1,53 @@
+/*
+ * util.h
+ * vim:tw=80:expandtab
+ * $Id$
+ *
+ * Copyright (c) 2007 Chris Pickel <sfiera at macports.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef _CUTIL_H
+#define _CUTIL_H
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "registry.h"
+
+typedef enum {
+    reg_strategy_exact = 1,
+    reg_strategy_glob = 2,
+    reg_strategy_regexp = 3,
+    reg_strategy_null = 4
+} reg_strategy;
+
+int reg_strcat(char** dst, size_t* dst_len, size_t* dst_space, char* src);
+int reg_listcat(void*** dst, int* dst_len, int* dst_space, void* src);
+int reg_all_objects(reg_registry* reg, char* query, int query_len,
+        void*** objects, cast_function* fn, free_function* del,
+        reg_error* errPtr);
+char* reg_strategy_op(reg_strategy strategy, reg_error* errPtr);
+
+#endif /* _CUTIL_H */
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20110527/d831865b/attachment-0001.html>


More information about the macports-changes mailing list