source: subversion/applications/utils/export/osm2pgsql/text-tree.c @ 26588

Last change on this file since 26588 was 5633, checked in by martinvoosterhout, 12 years ago

Restructure the projection code so it can support more projections,
primarily the true spherical mercator used by Google, TileCache? and others.
Add the -m option to select this. The default is still the incorrect
projection used before. Also display the used projection during processing.
Finally some minor cleanups to fix some warnings about undefined functions.

File size: 2.4 KB
Line 
1/* text-tree.c
2 *
3 * Storage of reference counted text strings
4 * used by keyvals.c to store the key/value strings
5 */
6#define _GNU_SOURCE
7#include <string.h>
8#include <assert.h>
9#include <stdlib.h>
10#include <stdio.h>
11#include "text-tree.h"
12
13struct tree_context *tree_ctx = NULL;
14
15int text_compare(const void *pa, const void *pb, void *rb_param)
16{
17    struct text_node *a = (struct text_node *)pa;
18    struct text_node *b = (struct text_node *)pb;
19
20    rb_param = NULL;
21    return strcmp(a->str, b->str);
22}
23
24struct tree_context *text_init(void)
25{
26    struct tree_context *context;
27    struct rb_table *table = rb_create (text_compare, NULL, NULL);
28
29    assert(table);
30    context = calloc(1, sizeof(*context));
31    assert(context);
32    context->table = table;
33    tree_ctx = context;
34    return context;
35}
36
37void text_free(void *pa, void *rb_param)
38{
39    struct text_node *a = (struct text_node *)pa;
40    rb_param = NULL;
41    free(a->str);
42    free(a);
43}
44
45const char *text_get(struct tree_context *context, const char *text)
46{
47    struct text_node *node, *dupe;
48
49    node = malloc(sizeof(*node));
50    assert(node);
51
52    node->str = strdup(text);
53    assert(node->str);
54    node->ref = 0;
55    dupe = rb_insert(context->table, (void *)node);
56    if (dupe) {
57        free(node->str);
58        free(node);
59        dupe->ref++;
60        return dupe->str;
61    } else {
62        node->ref++;
63        return node->str;
64    }
65}
66
67
68void text_release(struct tree_context *context, const char *text)
69{
70    struct text_node *node, find;
71
72    find.str = (char *)text;
73    find.ref = 0;
74    node = rb_find(context->table, (void *)&find);
75    if (!node) {
76        fprintf(stderr, "failed to find '%s'\n", text);
77        return;
78    }
79    node->ref--;
80    if (!node->ref) {
81        rb_delete (context->table, &find);
82        free(node->str);
83        free(node);
84    }
85}
86
87void text_exit(void)
88{
89    struct tree_context *context = tree_ctx;
90    rb_destroy(context->table, text_free);
91    free(context);
92    tree_ctx = NULL;
93}
94#if 0
95int main(int argc, char **argv)
96{
97    struct tree_context *ctx = text_init();
98
99    printf("%1$p %1$s\n", text_get(ctx, "Hello"));
100    printf("%1$p %1$s\n", text_get(ctx, "Hello"));
101    printf("%1$p %1$s\n", text_get(ctx, "World"));
102
103    text_release(ctx,"Hello");
104    text_release(ctx,"Hello");
105    text_release(ctx,"World");
106    text_release(ctx,"Hello");
107
108    text_exit(ctx);
109    return 0;
110}
111#endif
Note: See TracBrowser for help on using the repository browser.