dict @ 78b0ba12073b0940541d91a7568e8b7ada572848

 1#include <stdlib.h>
 2#include <stdio.h>
 3#include <string.h>
 4
 5#include "util.h"
 6
 7#define BUF_SIZE 100
 8
 9char* copy_achar(const char* src) {
10    int len = strlen(src) + 1;
11    char* dest = (char*)malloc(sizeof(char)*len);
12    strcpy(dest, src);
13
14    return dest;
15}
16
17
18int load_or_save_db(sqlite3 *pInMemory, const char *zFilename, int isSave){
19    int rc;                   /* Function return code */
20    sqlite3 *pFile;           /* Database connection opened on zFilename */
21    sqlite3_backup *pBackup;  /* Backup object used to copy data */
22    sqlite3 *pTo;             /* Database to copy to (pFile or pInMemory) */
23    sqlite3 *pFrom;           /* Database to copy from (pFile or pInMemory) */
24
25    rc = sqlite3_open(zFilename, &pFile);
26    if( rc==SQLITE_OK ){
27        pFrom = (isSave ? pInMemory : pFile);
28        pTo   = (isSave ? pFile     : pInMemory);
29
30        pBackup = sqlite3_backup_init(pTo, "main", pFrom, "main");
31        if( pBackup ){
32            (void)sqlite3_backup_step(pBackup, -1);
33            (void)sqlite3_backup_finish(pBackup);
34        }
35        rc = sqlite3_errcode(pTo);
36    }
37
38    (void)sqlite3_close(pFile);
39    return rc;
40}
41
42unsigned int count_file_lines(FILE *file) {
43    char buf[BUF_SIZE];
44    unsigned int counter = 0;
45    for(;;)
46    {
47        size_t res = fread(buf, 1, BUF_SIZE, file);
48        if (ferror(file))
49            return -1;
50
51        size_t i;
52        for(i = 0; i < res; i++)
53            if (buf[i] == '\n')
54                counter++;
55
56        if (feof(file))
57            break;
58    }
59
60    return counter;
61}