Wregistry

Check-in [b02a7306d8]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add function WrWriteStringEntry().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:b02a7306d8764088275f854687225628d3c69929
User & Date: wbp 2015-07-27 19:20:29
Context
2016-01-18
19:55
Add function WrDeleteSection(). check-in: 3965c0bcc6 user: wbp tags: trunk
2015-07-27
19:20
Add function WrWriteStringEntry(). check-in: b02a7306d8 user: wbp tags: trunk
2015-07-26
22:48
Don't treat trying to read a value when the registry database doesn't exist as an error. check-in: 683d8f6b9e user: wbp tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to testwreg.c.

10
11
12
13
14
15
16
17





18
19
20
21
    char const *string = NULL;

    if (WrOpen("Testwreg", "Wikareia") != 0)
        fprintf(stderr, "failed to open registry\n");
    else
        fprintf(stderr, "registry opened\n");

    string = WrReadStringEntry("File locations", "database", "default value");





    printf("value read: %s\n", string);

    exit(0);
}







|
>
>
>
>
>




10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    char const *string = NULL;

    if (WrOpen("Testwreg", "Wikareia") != 0)
        fprintf(stderr, "failed to open registry\n");
    else
        fprintf(stderr, "registry opened\n");

    string = WrReadStringEntry("Settings", "section", "default value");
    printf("value read: %s\n", string);

    WrWriteStringEntry("Settings", "section", "test value");

    string = WrReadStringEntry("Settings", "section", "default value");
    printf("value read: %s\n", string);

    exit(0);
}

Changes to wregistry.c.

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
..
31
32
33
34
35
36
37
38

39
40
41
42
43
44
45
..
59
60
61
62
63
64
65




























































/*=============================================================================
|  wregistry.c
|
|  This module contains functions to access a registry implemented in SQLite.
|
|  Functions included
|  ------------------
|  WrOpen
|  WrReadStringEntry


|
|  Last modified:  26-Jul-2015  Wm. Parsons
|=============================================================================*/
#include <sqlite3.h>    	/* for sqlite3 functions */
#include <stdio.h>      	/* for snprintf() */
#include <stdlib.h>     	/* for getenv() */
#include <string.h>     	/* for strncpy() */

#include "wregistry.h"


static sqlite3 *Db;





int
WrOpen(char const *app_key, char const *vendor_key)
{
    static char registry_path[256];
    int         code;
    char const *home = NULL;
................................................................................
    home = getenv("HOME");
    if (home == NULL)
        return -1;

    snprintf(registry_path, sizeof(registry_path), "%s/.registry/%s/%s.db3",
             home, vendor_key, app_key);

    code = sqlite3_open_v2(registry_path, &Db, SQLITE_OPEN_READWRITE, NULL);


    return (code == SQLITE_OK) ? 0 : -1;
}

char const *
WrReadStringEntry(char const *section, char const *key, char const *defvalue)
{
................................................................................
        strncpy(string, sqlite3_column_text(sth, 0), sizeof string - 1);
    else
        strncpy(string, defvalue, sizeof string - 1);

    sqlite3_finalize(sth);
    return string;
}

































































|
|


>
>

|











>
>
>







 







|
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
..
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
..
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
/*=============================================================================
|  wregistry.c
|
|  This module contains functions to access a registry implemented in SQLite.
|
|  Functions included (local functions in rounded brackets):
|  ---------------------------------------------------------
|  WrOpen
|  WrReadStringEntry
|  WrWriteStringEntry
|  (insert_key_value)
|
|  Last modified:  27-Jul-2015  Wm. Parsons
|=============================================================================*/
#include <sqlite3.h>    	/* for sqlite3 functions */
#include <stdio.h>      	/* for snprintf() */
#include <stdlib.h>     	/* for getenv() */
#include <string.h>     	/* for strncpy() */

#include "wregistry.h"


static sqlite3 *Db;


static int insert_key_value(char const *, char const *, char const *);


int
WrOpen(char const *app_key, char const *vendor_key)
{
    static char registry_path[256];
    int         code;
    char const *home = NULL;
................................................................................
    home = getenv("HOME");
    if (home == NULL)
        return -1;

    snprintf(registry_path, sizeof(registry_path), "%s/.registry/%s/%s.db3",
             home, vendor_key, app_key);

    code = sqlite3_open_v2(registry_path, &Db,
                           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL);

    return (code == SQLITE_OK) ? 0 : -1;
}

char const *
WrReadStringEntry(char const *section, char const *key, char const *defvalue)
{
................................................................................
        strncpy(string, sqlite3_column_text(sth, 0), sizeof string - 1);
    else
        strncpy(string, defvalue, sizeof string - 1);

    sqlite3_finalize(sth);
    return string;
}

void
WrWriteStringEntry(char const *section, char const *key, char const *value)
{
    static char   sql[256];
    sqlite3_stmt *sth;

    if (Db == NULL)
        return;

    snprintf(sql, sizeof sql, "update \"%s\" set value=? where key=?", section);
    switch (sqlite3_prepare(Db, sql, -1, &sth, NULL))
    {
        case SQLITE_OK:
            sqlite3_bind_text(sth, 1, value, -1, SQLITE_STATIC);
            sqlite3_bind_text(sth, 2, key, -1, SQLITE_STATIC);
            sqlite3_step(sth);
            sqlite3_finalize(sth);

            if (sqlite3_changes(Db) == 0)
                /* no row was updated so insert a new row */
                insert_key_value(section, key, value);
            break;

        case SQLITE_ERROR:
            if (insert_key_value(section, key, value) == SQLITE_ERROR)
            {
                snprintf(sql, sizeof sql,
                         "create table \"%s\"(key text unique, value)",
                         section);
                sqlite3_exec(Db, sql, NULL, NULL, NULL);
                insert_key_value(section, key, value);
            }
    }
}

int
insert_key_value(char const *section, char const *key, char const *value)
{
    int           code;
    char          sql[256];
    sqlite3_stmt *sth;

    if (Db == NULL)
        return -1;

    snprintf(sql, sizeof sql,
             "insert into \"%s\"(key, value) values(?,?)", section);

    code = sqlite3_prepare(Db, sql, -1, &sth, NULL);

    if (code == SQLITE_OK)
    {
        sqlite3_bind_text(sth, 1, key, -1, SQLITE_STATIC);
        sqlite3_bind_text(sth, 2, value, -1, SQLITE_STATIC);
        code = sqlite3_step(sth);
        sqlite3_finalize(sth);
    }
    return code;
}

Changes to wregistry.h.

1
2
3
4
5
6
7
8
9
10
11
12
13



/*=============================================================================
|  wregistry.h
|
|  Function headers to access a registry implemented in SQLite.
|
|  Last modified:  26-Jul-2015  Wm. Parsons
|=============================================================================*/

int
WrOpen(char const *app_key, char const *vendor_key);

char const *
WrReadStringEntry(char const *section, char const *key, char const *defvalue);






|

|







>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*=============================================================================
|  wregistry.h
|
|  Prototypes for functions to access a registry implemented in SQLite.
|
|  Last modified:  27-Jul-2015  Wm. Parsons
|=============================================================================*/

int
WrOpen(char const *app_key, char const *vendor_key);

char const *
WrReadStringEntry(char const *section, char const *key, char const *defvalue);

void
WrWriteStringEntry(char const *section, char const *key, char const *value);