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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#include "dc_context.h"
#include "dc_array.h"

#define DC_ARRAY_MAGIC 0x000a11aa


/**
 * Create an array object in memory.
 *
 * @private @memberof dc_array_t
 * @param context The context object that should be stored in the array object. May be NULL.
 * @param type 0 for a standard array of int or one of DC_ARRAY_*.
 * @param initsize Initial maximal size of the array. If you add more items, the internal data pointer is reallocated.
 * @return New array object of the requested size, the data should be set directly.
 */
dc_array_t* dc_array_new_typed(dc_context_t* context, int type, size_t initsize)
{
	dc_array_t* array = NULL;

	array = (dc_array_t*) calloc(1, sizeof(dc_array_t));
	if (array==NULL) {
		exit(47);
	}

	array->magic     = DC_ARRAY_MAGIC;
	array->context   = context;
	array->count     = 0;
	array->allocated = initsize<1? 1 : initsize;
	array->type      = type;
	array->array     = malloc(array->allocated * sizeof(uintptr_t));
	if (array->array==NULL) {
		exit(48);
	}

	return array;
}


dc_array_t* dc_array_new(dc_context_t* context, size_t initsize)
{
	return dc_array_new_typed(context, 0, initsize);
}


/**
 * Free an array object. Does not free any data items.
 *
 * @memberof dc_array_t
 * @param array The array object to free,
 *     created eg. by dc_get_chatlist(), dc_get_contacts() and so on.
 *     If NULL is given, nothing is done.
 * @return None.
 */
void dc_array_unref(dc_array_t* array)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC) {
		return;
	}

	if (array->type==DC_ARRAY_LOCATIONS) {
		dc_array_free_ptr(array);
	}

	free(array->array);
	array->magic = 0;
	free(array);
}


/**
 * Calls free() for each item and sets the item to 0 afterwards.
 * The array object itself is not deleted and the size of the array stays the same.
 *
 * @private @memberof dc_array_t
 * @param array The array object.
 * @return None.
 */
void dc_array_free_ptr(dc_array_t* array)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC) {
		return;
	}

	for (size_t i = 0; i<array->count; i++) {
		if (array->type==DC_ARRAY_LOCATIONS) {
			free(((struct _dc_location*)array->array[i])->marker);
		}
		free((void*)array->array[i]);
		array->array[i] = 0;
	}
}


/**
 * Duplicates the array, take care if the array contains pointers to objects, take care to free them only once afterwards!
 * If the array only contains integers, you are always save.
 *
 * @private @memberof dc_array_t
 * @param array The array object.
 * @return The duplicated array.
 */
dc_array_t* dc_array_duplicate(const dc_array_t* array)
{
	dc_array_t* ret = NULL;

	if (array==NULL || array->magic!=DC_ARRAY_MAGIC) {
		return NULL;
	}

	ret = dc_array_new(array->context, array->allocated);
	ret->count = array->count;
	memcpy(ret->array, array->array, array->count * sizeof(uintptr_t));

	return ret;
}


static int cmp_intptr_t(const void* p1, const void* p2)
{
	uintptr_t v1 = *(uintptr_t*)p1;
	uintptr_t v2 = *(uintptr_t*)p2;
	return (v1<v2)? -1 : ((v1>v2)? 1 : 0); /* CAVE: do not use v1-v2 as the uintptr_t may be 64bit and the return value may be 32bit only... */
}


/**
 * Sort the array, assuming it contains unsigned integers.
 *
 * @private @memberof dc_array_t
 * @param array The array object.
 * @return The duplicated array.
 */
void dc_array_sort_ids(dc_array_t* array)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || array->count <= 1) {
		return;
	}
	qsort(array->array, array->count, sizeof(uintptr_t), cmp_intptr_t);
}


static int cmp_strings_t(const void* p1, const void* p2)
{
    const char* v1 = *(const char **)p1;
    const char* v2 = *(const char **)p2;
    return strcmp(v1, v2);
}


/**
 * Sort the array, assuming it contains pointers to strings.
 *
 * @private @memberof dc_array_t
 * @param array The array object.
 * @return The duplicated array.
 */
void dc_array_sort_strings(dc_array_t* array)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || array->count <= 1) {
		return;
	}
	qsort(array->array, array->count, sizeof(char*), cmp_strings_t);
}


/**
 * Empty an array object. Allocated data is not freed by this function, only the count is set to null.
 *
 * @private @memberof dc_array_t
 * @param array The array object to empty.
 * @return None.
 */
void dc_array_empty(dc_array_t* array)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC) {
		return;
	}

	array->count = 0;
}


/**
 * Add an unsigned integer to the array.
 * After calling this function the size of the array grows by one.
 * It is okay to add the ID 0, event in this case, the array grows by one.
 *
 * @param array The array to add the item to.
 * @param item The item to add.
 * @return None.
 */
void dc_array_add_uint(dc_array_t* array, uintptr_t item)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC) {
		return;
	}

	if (array->count==array->allocated) {
		int newsize = (array->allocated * 2) + 10;
		if ((array->array=realloc(array->array, newsize*sizeof(uintptr_t)))==NULL) {
			exit(49);
		}
		array->allocated = newsize;
	}

	array->array[array->count] = item;
	array->count++;
}


/**
 * Add an ID to the array.
 * After calling this function the size of the array grows by one.
 * It is okay to add the ID 0, event in this case, the array grows by one.
 *
 * @param array The array to add the item to.
 * @param item The item to add.
 * @return None.
 */
void dc_array_add_id(dc_array_t* array, uint32_t item)
{
	dc_array_add_uint(array, item);
}


/**
 * Add an pointer to the array.
 * After calling this function the size of the array grows by one.
 * It is okay to add the ID 0, event in this case, the array grows by one.
 *
 * @param array The array to add the item to.
 * @param item The item to add.
 * @return None.
 */
void dc_array_add_ptr(dc_array_t* array, void* item)
{
	dc_array_add_uint(array, (uintptr_t)item);
}


/**
 * Find out the number of items in an array.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @return Returns the number of items in a dc_array_t object. 0 on errors or if the array is empty.
 */
size_t dc_array_get_cnt(const dc_array_t* array)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC) {
		return 0;
	}

	return array->count;
}


/**
 * Get the item at the given index as an unsigned integer.
 * The size of the integer is always larget enough to hold a pointer.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item to get. Must be between 0 and dc_array_get_cnt()-1.
 * @return Returns the item at the given index. Returns 0 on errors or if the array is empty.
 */
uintptr_t dc_array_get_uint(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count) {
		return 0;
	}

	return array->array[index];
}


/**
 * Get the item at the given index as an ID.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item to get. Must be between 0 and dc_array_get_cnt()-1.
 * @return Returns the item at the given index. Returns 0 on errors or if the array is empty.
 */
uint32_t dc_array_get_id(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count) {
		return 0;
	}

	if (array->type==DC_ARRAY_LOCATIONS) {
		return ((struct _dc_location*)array->array[index])->location_id;
	}

	return (uint32_t)array->array[index];
}


/**
 * Get the item at the given index as an ID.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item to get. Must be between 0 and dc_array_get_cnt()-1.
 * @return Returns the item at the given index. Returns 0 on errors or if the array is empty.
 */
void* dc_array_get_ptr(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count) {
		return 0;
	}

	return (void*)array->array[index];
}


/**
 * Return the latitude of the item at the given index.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item. Must be between 0 and dc_array_get_cnt()-1.
 * @return Latitude of the item at the given index.
 *     0.0 if there is no latitude bound to the given item,
 */
double dc_array_get_latitude(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count
	 || array->type!=DC_ARRAY_LOCATIONS || array->array[index]==0 ) {
		return 0;
	}

	return ((struct _dc_location*)array->array[index])->latitude;
}


/**
 * Return the longitude of the item at the given index.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item. Must be between 0 and dc_array_get_cnt()-1.
 * @return Latitude of the item at the given index.
 *     0.0 if there is no longitude bound to the given item,
 */
double dc_array_get_longitude(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count
	 || array->type!=DC_ARRAY_LOCATIONS || array->array[index]==0 ) {
		return 0;
	}

	return ((struct _dc_location*)array->array[index])->longitude;
}


/**
 * Return the accuracy of the item at the given index.
 * See dc_set_location() for more information about the accuracy.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item. Must be between 0 and dc_array_get_cnt()-1.
 * @return Accuracy of the item at the given index.
 *     0.0 if there is no longitude bound to the given item,
 */
double dc_array_get_accuracy(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count
	 || array->type!=DC_ARRAY_LOCATIONS || array->array[index]==0 ) {
		return 0;
	}

	return ((struct _dc_location*)array->array[index])->accuracy;
}


/**
 * Return the timestamp of the item at the given index.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item. Must be between 0 and dc_array_get_cnt()-1.
 * @return Timestamp of the item at the given index.
 *     0 if there is no timestamp bound to the given item,
 */
time_t dc_array_get_timestamp(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count
	 || array->type!=DC_ARRAY_LOCATIONS || array->array[index]==0 ) {
		return 0;
	}

	return ((struct _dc_location*)array->array[index])->timestamp;
}


/**
 * Return the message-id of the item at the given index.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item. Must be between 0 and dc_array_get_cnt()-1.
 * @return Message-id of the item at the given index.
 *     0 if there is no message-id bound to the given item,
 */
uint32_t dc_array_get_msg_id(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count
	 || array->type!=DC_ARRAY_LOCATIONS || array->array[index]==0 ) {
		return 0;
	}

	return ((struct _dc_location*)array->array[index])->msg_id;
}


/**
 * Return the chat-id of the item at the given index.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item. Must be between 0 and dc_array_get_cnt()-1.
 * @return Chat-id of the item at the given index.
 *     0 if there is no chat-id bound to the given item,
 */
uint32_t dc_array_get_chat_id(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count
	 || array->type!=DC_ARRAY_LOCATIONS || array->array[index]==0 ) {
		return 0;
	}

	return ((struct _dc_location*)array->array[index])->chat_id;
}


/**
 * Return the contact-id of the item at the given index.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item. Must be between 0 and dc_array_get_cnt()-1.
 * @return Contact-id of the item at the given index.
 *     0 if there is no contact-id bound to the given item,
 */
uint32_t dc_array_get_contact_id(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count
	 || array->type!=DC_ARRAY_LOCATIONS || array->array[index]==0 ) {
		return 0;
	}

	return ((struct _dc_location*)array->array[index])->contact_id;
}


/**
 * Return the marker-character of the item at the given index.
 * Marker-character are typically bound to locations
 * returned by dc_get_locations()
 * and are typically created by on-character-messages
 * which can also be an emoticon :)
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item. Must be between 0 and dc_array_get_cnt()-1.
 * @return Marker-character of the item at the given index.
 *     NULL if there is no marker-character bound to the given item.
 *     The returned value must be free()'d after usage.
 */
char* dc_array_get_marker(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count
	 || array->type!=DC_ARRAY_LOCATIONS || array->array[index]==0 ) {
		return 0;
	}

	return dc_strdup_keep_null(((struct _dc_location*)array->array[index])->marker);
}


/**
 * Return the independent-state of the location at the given index.
 * Independent locations do not belong to the track of the user.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @param index Index of the item. Must be between 0 and dc_array_get_cnt()-1.
 * @return 0=Location belongs to the track of the user,
 *     1=Location was reported independently.
 */
int dc_array_is_independent(const dc_array_t* array, size_t index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || index>=array->count
	 || array->type!=DC_ARRAY_LOCATIONS || array->array[index]==0 ) {
		return 0;
	}

	return ((struct _dc_location*)array->array[index])->independent;
}


/**
 * Check if a given ID is present in an array.
 *
 * @private @memberof dc_array_t
 * @param array The array object to search in.
 * @param needle The ID to search for.
 * @param[out] ret_index If set, this will receive the index. Set to NULL if you're not interested in the index.
 * @return 1=ID is present in array, 0=ID not found.
 */
int dc_array_search_id(const dc_array_t* array, uint32_t needle, size_t* ret_index)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC) {
		return 0;
	}

	uintptr_t* data = array->array;
	size_t i, cnt = array->count;
	for (i=0; i<cnt; i++)
	{
		if (data[i]==needle) {
			if (ret_index) {
				*ret_index = i;
			}
			return 1;
		}
	}

	return 0;
}


/**
 * Get raw pointer to the data.
 *
 * @memberof dc_array_t
 * @param array The array object.
 * @return Raw pointer to the array. You MUST NOT free the data. You MUST NOT access the data beyond the current item count.
 *     It is not possible to enlarge the array this way.  Calling any other dc_array*()-function may discard the returned pointer.
 */
const uintptr_t* dc_array_get_raw(const dc_array_t* array)
{
	if (array==NULL || array->magic!=DC_ARRAY_MAGIC) {
		return NULL;
	}
	return array->array;
}


char* dc_arr_to_string(const uint32_t* arr, int cnt)
{
	/* return comma-separated value-string from integer array */
	char*       ret = NULL;
	const char* sep = ",";

	if (arr==NULL || cnt <= 0) {
		return dc_strdup("");
	}

	/* use a macro to allow using integers of different bitwidths */
	#define INT_ARR_TO_STR(a, c) { \
		int i; \
		ret = malloc((c)*(11+strlen(sep))/*sign,10 digits,sep*/+1/*terminating zero*/); \
		if (ret==NULL) { exit(35); } \
		ret[0] = 0; \
		for (i=0; i<(c); i++) { \
			if (i) { \
				strcat(ret, sep); \
			} \
			sprintf(&ret[strlen(ret)], "%lu", (unsigned long)(a)[i]); \
		} \
	}

	INT_ARR_TO_STR(arr, cnt);

	return ret;
}


char* dc_array_get_string(const dc_array_t* array, const char* sep)
{
	char* ret = NULL;

	if (array==NULL || array->magic!=DC_ARRAY_MAGIC || sep==NULL) {
		return dc_strdup("");
	}

	INT_ARR_TO_STR(array->array, array->count);

	return ret;
}