~stub/ubuntu/precise/calibre/devel

« back to all changes in this revision

Viewing changes to src/calibre/library/sqlite_custom.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-04-12 11:29:25 UTC
  • mfrom: (42.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110412112925-c7171kt2bb5rmft4
Tags: 0.7.50+dfsg-2
* debian/control: Build with libpodofo-dev to enable PDF metadata.
  (Closes: #619632)
* debian/control: Add libboost1.42-dev build dependency. Apparently it is
  needed in some setups. (Closes: #619807)
* debian/rules: Call dh_sip to generate a proper sip API dependency, to
  prevent crashes like #616372 for partial upgrades.
* debian/control: Bump python-qt4 dependency to >= 4.8.3-2, which reportedly
  fixes crashes on startup. (Closes: #619701, #620125)

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
        free(list->vals[i]->val);
78
78
        free(list->vals[i]);
79
79
    }
 
80
    free(list->vals);
80
81
}
81
82
 
82
83
static int sort_concat_cmp(const void *a_, const void *b_) {
142
143
 
143
144
// }}}
144
145
 
 
146
// identifiers_concat {{{
 
147
 
 
148
typedef struct {
 
149
    char *val;
 
150
    size_t length;
 
151
} IdentifiersConcatItem;
 
152
 
 
153
typedef struct {
 
154
    IdentifiersConcatItem **vals;
 
155
    size_t count;
 
156
    size_t length;
 
157
} IdentifiersConcatList;
 
158
 
 
159
static void identifiers_concat_step(sqlite3_context *context, int argc, sqlite3_value **argv) {
 
160
    const char *key, *val;
 
161
    size_t len = 0;
 
162
    IdentifiersConcatList *list;
 
163
 
 
164
    assert(argc == 2);
 
165
 
 
166
    list = (IdentifiersConcatList*) sqlite3_aggregate_context(context, sizeof(*list));
 
167
    if (list == NULL) return;
 
168
 
 
169
    if (list->vals == NULL) {
 
170
        list->vals = (IdentifiersConcatItem**)calloc(100, sizeof(IdentifiersConcatItem*));
 
171
        if (list->vals == NULL) return;
 
172
        list->length = 100;
 
173
        list->count = 0;
 
174
    }
 
175
 
 
176
    if (list->count == list->length) {
 
177
        list->vals = (IdentifiersConcatItem**)realloc(list->vals, list->length + 100);
 
178
        if (list->vals == NULL) return;
 
179
        list->length = list->length + 100;
 
180
    }
 
181
 
 
182
    list->vals[list->count] = (IdentifiersConcatItem*)calloc(1, sizeof(IdentifiersConcatItem));
 
183
    if (list->vals[list->count] == NULL) return;
 
184
 
 
185
    key = (char*) sqlite3_value_text(argv[0]);
 
186
    val = (char*) sqlite3_value_text(argv[1]);
 
187
    if (key == NULL || val == NULL) {return;}
 
188
    len = strlen(key) + strlen(val) + 1;
 
189
 
 
190
    list->vals[list->count]->val = (char*)calloc(len+1, sizeof(char));
 
191
    if (list->vals[list->count]->val == NULL) return;
 
192
    snprintf(list->vals[list->count]->val, len+1, "%s:%s", key, val);
 
193
    list->vals[list->count]->length = len;
 
194
 
 
195
    list->count = list->count + 1;
 
196
 
 
197
}
 
198
 
 
199
 
 
200
static void identifiers_concat_finalize(sqlite3_context *context) {
 
201
    IdentifiersConcatList *list;
 
202
    IdentifiersConcatItem *item;
 
203
    char *ans, *pos;
 
204
    size_t sz = 0, i;
 
205
 
 
206
    list = (IdentifiersConcatList*) sqlite3_aggregate_context(context, sizeof(*list));
 
207
    if (list == NULL || list->vals == NULL || list->count < 1) return;
 
208
 
 
209
    for (i = 0; i < list->count; i++) { 
 
210
        sz += list->vals[i]->length;
 
211
    }
 
212
    sz += list->count; // Space for commas
 
213
    ans = (char*)calloc(sz+2, sizeof(char));
 
214
    if (ans == NULL) return;
 
215
 
 
216
    pos = ans;
 
217
 
 
218
    for (i = 0; i < list->count; i++) {
 
219
        item = list->vals[i];
 
220
        if (item == NULL || item->val == NULL) continue;
 
221
        memcpy(pos, item->val, item->length);
 
222
        pos += item->length;
 
223
        *pos = ',';
 
224
        pos += 1;
 
225
        free(item->val);
 
226
        free(item);
 
227
    }
 
228
    *(pos-1) = 0; // Remove trailing comma
 
229
    sqlite3_result_text(context, ans, -1, SQLITE_TRANSIENT);
 
230
    free(ans);
 
231
    free(list->vals);
 
232
}
 
233
 
 
234
// }}}
 
235
 
145
236
MYEXPORT int sqlite3_extension_init(
146
237
    sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi){
147
238
  SQLITE_EXTENSION_INIT2(pApi);
148
239
  sqlite3_create_function(db, "sortconcat", 2, SQLITE_UTF8, NULL, NULL, sort_concat_step, sort_concat_finalize);
149
240
  sqlite3_create_function(db, "sort_concat", 2, SQLITE_UTF8, NULL, NULL, sort_concat_step, sort_concat_finalize2);
 
241
  sqlite3_create_function(db, "identifiers_concat", 2, SQLITE_UTF8, NULL, NULL, identifiers_concat_step, identifiers_concat_finalize);
150
242
  return 0;
151
243
}
152
244