Wregistry

Check-in [3965c0bcc6]
Login

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

Overview
Comment:Add function WrDeleteSection().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:3965c0bcc6ab5514f194d4a2973a49d50f2aca91
User & Date: wbp 2016-01-18 19:55:56
Context
2016-01-20
20:13
Fix error in WrDeleteSection() when trying to delete non-existent section. check-in: f676e3ddbc user: wbp tags: trunk
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to testwreg.c.

3
4
5
6
7
8
9

10
11

12
13
14
15
16

17
18
19

20
21
22
23
24





25
26
#include <stdio.h>	/* for fprintf(), &c. */
#include <stdlib.h>	/* for exit() */

#include "wregistry.h"

main()
{

    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);
}







>


>





>



>





>
>
>
>
>


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
#include <stdio.h>	/* for fprintf(), &c. */
#include <stdlib.h>	/* for exit() */

#include "wregistry.h"

main()
{
    int         code;
    char const *string = NULL;

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

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

    /* test of WrWriteStringEntry() */
    WrWriteStringEntry("Settings", "section", "test value");

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

    /* test of WrDeleteSection() */
    code = WrDeleteSection("Settings" );
    printf("WrDeleteSection() %s\n", code ? "succeeded" : "failed");
    string = WrReadStringEntry("Settings", "section", "no 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


















30






31
32
33
34
35
36
37
..
44
45
46
47
48
49
50





51
52
53
54
55
56
57
..
68
69
70
71
72
73
74





75
76
77
78
79
80
81
...
103
104
105
106
107
108
109





110
111
112
113
114
115
116
/*=============================================================================
|  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)
................................................................................
                           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)





{
    static char   sql[256];
    sqlite3_stmt *sth;
    static char   string[256];

    if (Db == NULL)
        return NULL;
................................................................................

    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;

................................................................................
                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;







>





|







 









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>







 







>
>
>
>
>







 







>
>
>
>
>







 







>
>
>
>
>







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
..
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
..
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*=============================================================================
|  wregistry.c
|
|  This module contains functions to access a registry implemented in SQLite.
|
|  Functions included (local functions in rounded brackets):
|  ---------------------------------------------------------
|  WrDeleteSection
|  WrOpen
|  WrReadStringEntry
|  WrWriteStringEntry
|  (insert_key_value)
|
|  Last modified:  18-Jan-2016  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
WrDeleteSection(char const *section)
/*=============================================================================
 *
 *  WrDeleteSection -- Delete a section in the registry database.
 *  Returns 1 on success; 0 on error.
 *
 *============================================================================*/
{
    char sql[256];

    if (Db == NULL)
        return 0;

    snprintf(sql, sizeof sql, "drop table \"%s\"", section);
    return sqlite3_exec(Db, sql, NULL, NULL, NULL) == SQLITE_OK;
}

int
WrOpen(char const *app_key, char const *vendor_key)
/*=============================================================================
 *
 *  WrOpen -- Open the registry database.
 *  Returns 0 on success; -1 on error.
 *
 *============================================================================*/
{
    static char registry_path[256];
    int         code;
    char const *home = NULL;

    home = getenv("HOME");
    if (home == NULL)
................................................................................
                           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)
/*=============================================================================
 *
 *  WrReadStringEntry -- Read a string entry from the registry database.
 *
 *============================================================================*/
{
    static char   sql[256];
    sqlite3_stmt *sth;
    static char   string[256];

    if (Db == NULL)
        return NULL;
................................................................................

    sqlite3_finalize(sth);
    return string;
}

void
WrWriteStringEntry(char const *section, char const *key, char const *value)
/*=============================================================================
 *
 *  WrWriteStringEntry -- Write a string entry to the registry database.
 *
 *============================================================================*/
{
    static char   sql[256];
    sqlite3_stmt *sth;

    if (Db == NULL)
        return;

................................................................................
                insert_key_value(section, key, value);
            }
    }
}

int
insert_key_value(char const *section, char const *key, char const *value)
/*=============================================================================
 *
 *  insert_key_value -- Insert a string value into the registry database.
 *
 *============================================================================*/
{
    int           code;
    char          sql[256];
    sqlite3_stmt *sth;

    if (Db == NULL)
        return -1;

Changes to wregistry.h.

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);





|


>
>
>








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

int
WrDeleteSection(char const *section);

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);