]> git.ozlabs.org Git - ccan/blobdiff - ccan/asort/_info
New asort routine; unf. breaks under -Wmissing-prototypes etc :(
[ccan] / ccan / asort / _info
diff --git a/ccan/asort/_info b/ccan/asort/_info
new file mode 100644 (file)
index 0000000..3b71998
--- /dev/null
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <string.h>
+
+/**
+ * asort - typesafe array sort (qsort)
+ *
+ * qsort() is the standard routine for sorting an array of objects.
+ * Unfortunately, it has two problems:
+ *     1) It isn't typesafe,
+ *     2) The comparison function doesn't take a context pointer.
+ *
+ * asort does both.
+ *
+ * Licence: LGPL
+ *
+ * Example:
+ *     #include <ccan/asort/asort.h>
+ *     #include <stdio.h>
+ *     #include <string.h>
+ *     
+ *     static int cmp(const char **a, const char **n, bool *casefold)
+ *     {
+ *             if (*casefold)
+ *                     return strcasecmp(*a, *b);
+ *             else
+ *                     return strcmp(*a, *b);
+ *     }
+ *     
+ *     int main(int argc, char *argv[])
+ *     {
+ *             bool casefold = false;
+ *             unsigned int i;
+ *     
+ *             if (argc < 2) {
+ *                     fprintf(stderr, "Usage: %s [-i] <list>...\n"
+ *                             "Sort arguments (-i = ignore case)\n",
+ *                             argv[0]);
+ *                     exit(1);
+ *             }
+ *     
+ *             if (strcmp(argv[1], "-i") == 0) {
+ *                     casefold = true;
+ *                     argc--;
+ *                     argv++;
+ *             }
+ *             asort(&argv[1], argc-1, cmp, &casefold);
+ *             for (i = 1; i < argc; i++)
+ *                     printf("%s ", argv[i]);
+ *             printf("\n");
+ *             return 0;
+ *     }
+ */
+int main(int argc, char *argv[])
+{
+       if (argc != 2)
+               return 1;
+
+       if (strcmp(argv[1], "depends") == 0) {
+               printf("ccan/typesafe_cb\n");
+               printf("ccan/array_size\n");
+               return 0;
+       }
+
+       return 1;
+}