File: util.c

package info (click to toggle)
lgeneral 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 8,760 kB
  • sloc: ansic: 30,422; sh: 3,574; makefile: 378; sed: 93
file content (194 lines) | stat: -rw-r--r-- 6,186 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
    Data-structures for extraction.
    Copyright (C) 2005 Leo Savernik  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 OR
    CONTRIBUTORS 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 "intl/hash-string.h"
#include "util/hashtable.h"
#include "util/hashtable_itr.h"

#include "list.h"
#include "parser.h"

#include <stdlib.h>
#include <string.h>

/* === Translation Stuff ============================================= */

struct Translateables {
  struct hashtable *ht;
  char *domain;
};

struct Comment {
  char *s;		/* string */
  unsigned len;		/* length of string (w/o trailing 0) */
  int full:1;		/* 1 if no more comment may be added */
};

static void comment_delete(void *cmt) {
  struct Comment * const comment = cmt;
  if (!comment) return;
  free(comment->s);
  free(comment);
}

/**
 * Checks whether the given string is already contained at
 * the end of the comment.
 */
static int comment_ends_with(struct Comment *cmt, const char *s, unsigned len) {
  if (cmt->len < len) return 0;
  return memcmp(cmt->s + cmt->len - len, s, len) == 0;
}

static void comment_add(struct Comment *cmt, const char *s, unsigned len) {
  if (cmt->full) return;
  /* FIXME: inefficient */
  cmt->s = realloc(cmt->s, cmt->len + len + 1);
  memcpy(cmt->s + cmt->len, s, len + 1);
  cmt->len += len;
}

struct Translateables *translateables_create(void) {
  struct Translateables *xl = malloc(sizeof(struct Translateables));
  xl->ht = create_hashtable(16384, (unsigned int (*) (void*))hash_string,
                            (int (*)(void *, void *))strcmp, comment_delete);
  xl->domain = 0; /* heeeheee! will hideously crash on strcmp */
  return xl;
}

void translateables_delete(struct Translateables *xl) {
  if (xl) {
    hashtable_destroy(xl->ht, 1);
    free(xl->domain);
    free(xl);
  }
}

void translateables_set_domain(struct Translateables *xl, const char *domain) {
  xl->domain = strdup(domain);
}

const char *translateables_get_domain(struct Translateables *xl) {
  return xl->domain;
}

const char *translateables_get(struct Translateables *xl, const char *key, unsigned *len) {
  struct Comment *cmt = hashtable_search(xl->ht, (void *)key);
  if (!cmt) return 0;
  if (len) *len = cmt->len;
  return cmt->s;
}

void translateables_add(struct Translateables *xl, const char *key, const char *comment) {
  unsigned len = strlen(comment);
  struct Comment *cmt = hashtable_search(xl->ht, (void *)key);
  /* if already contained, add comment to old comment */
  if (cmt) {
    comment_add(cmt, comment, len);
    return;
  }
  
  /* otherwise, create new entry */
  cmt = calloc(1, sizeof(struct Comment));
  cmt->len = len;
  cmt->s = strdup(comment);
  
  hashtable_insert(xl->ht, strdup(key), cmt);
}

struct TranslateablesIterator *translateables_iterator(struct Translateables *xl) {
  return (struct TranslateablesIterator *)hashtable_iterator(xl->ht);
}

const char *translateables_iterator_key(struct TranslateablesIterator *it) {
  return hashtable_iterator_key((struct hashtable_itr *)it);
}

const char *translateables_iterator_comment(struct TranslateablesIterator *it) {
  return ((struct Comment *)hashtable_iterator_value((struct hashtable_itr *)it))->s;
}

int translateables_iterator_advance(struct TranslateablesIterator *it) {
  return hashtable_iterator_advance((struct hashtable_itr *)it);
}

void translateables_iterator_delete(struct TranslateablesIterator *it) {
  free(it);
}

void translateables_add_pdata(struct Translateables *xl, struct PData *pd) {
  const int comment_limit = 100;
  char context[1024];
  unsigned context_len;
  const char *tr_str;

  /* gather context information */
  snprintf(context, sizeof context, " %s:%d",
           parser_get_filename(pd), parser_get_linenumber(pd));
  context_len = strlen(context);

  if (!pd->values) return;
  
  list_reset(pd->values);
  for (; (tr_str = list_next(pd->values)); ) {
    struct Comment *cmt;
    
    /* don't translate empty strings */
    if (!*tr_str) continue;
    
    cmt = hashtable_search(xl->ht, (void *)tr_str);

    /* If not yet contained, insert new one */
    if (!cmt) {
      cmt = calloc(1, sizeof(struct Comment));
      hashtable_insert(xl->ht, strdup(tr_str), cmt);
    }
    
    /* Only add comment if not yet inserted */
    if (!comment_ends_with(cmt, context, context_len)) {
      comment_add(cmt, context, context_len);
      
      if (cmt->len + context_len >= comment_limit) {
        static const char more[] = " ...";
        comment_add(cmt, more, sizeof(more) - 1);
        cmt->full = 1;
      }
    }
  }
}

/* === End Translation Stuff ========================================= */


int resource_matches_domain(struct PData *pd, struct Translateables *xl) {
  char *str;
  if (!parser_get_value(pd, "domain", &str, 0)) return 0;
  return strcmp(translateables_get_domain(xl), str) == 0;
}

/* kate: tab-indents on; space-indent on; replace-tabs off; indent-width 2; dynamic-word-wrap off; inden(t)-mode cstyle */