Wregistry

Check-in [683d8f6b9e]
Login

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

Overview
Comment:Don't treat trying to read a value when the registry database doesn't exist as an error.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:683d8f6b9ec6a45401fcd6c0a657ce4d1deb4c8d
User & Date: wbp 2015-07-26 22:48:51
Context
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
21:01
Initial import. check-in: e083916f16 user: wbp tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Makefile.in.

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
	cp -f $(HEADER) $(includedir)
	@if test ! -d $(libdir); then \
	   echo "installation directory $(libdir) does not exist" && false; \
	fi
	cp -f $(WREGLIB) $(libdir)

uninstall: FORCE
	cd (includedir) && rm -f wregistry.h
	cd $(libdir) && rm -f $(WREGLIB)

FORCE:

#==============================================================================
#    Suffix rules
#==============================================================================







|







92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
	cp -f $(HEADER) $(includedir)
	@if test ! -d $(libdir); then \
	   echo "installation directory $(libdir) does not exist" && false; \
	fi
	cp -f $(WREGLIB) $(libdir)

uninstall: FORCE
	cd $(includedir) && rm -f wregistry.h
	cd $(libdir) && rm -f $(WREGLIB)

FORCE:

#==============================================================================
#    Suffix rules
#==============================================================================

Changes to testwreg.c.

5
6
7
8
9
10
11
12
13
14
15
16
17

18
19
20
21
22
23
24

#include "wregistry.h"

main()
{
    char const *string = NULL;

    if (WrOpen("Recepsum", "Wikareia") != 0)
    {
        fprintf(stderr, "failed to open registry\n");
        exit(1);
    }


    printf("registry opened\n");

    string = WrReadStringEntry("File locations", "database", "default value");
    printf("value read: %s\n", string);

    exit(0);
}







|
<

<
<
<
>
|






5
6
7
8
9
10
11
12

13



14
15
16
17
18
19
20
21

#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("File locations", "database", "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
..
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*=============================================================================
|  wregistry.c
|
|  This module contains functions to access a registry implemented in SQLite.
|
|  Functions included
|  ------------------

|  WReadStringEntry
|
|  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() */
................................................................................
    static char   string[256];

    if (Db == NULL)
        return NULL;

    snprintf(sql, sizeof sql, "select value from \"%s\" where key=?", section);
    if (sqlite3_prepare(Db, sql, -1, &sth, NULL) != SQLITE_OK)
        return NULL;
    if (sqlite3_bind_text(sth, 1, key, -1, SQLITE_STATIC) != SQLITE_OK)
        return NULL;
    if (sqlite3_step(sth) == SQLITE_ROW)
        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
..
48
49
50
51
52
53
54
55
56
57
58
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() */
................................................................................
    static char   string[256];

    if (Db == NULL)
        return NULL;

    snprintf(sql, sizeof sql, "select value from \"%s\" where key=?", section);
    if (sqlite3_prepare(Db, sql, -1, &sth, NULL) != SQLITE_OK)
        return defvalue;
    if (sqlite3_bind_text(sth, 1, key, -1, SQLITE_STATIC) != SQLITE_OK)
        return NULL;
    if (sqlite3_step(sth) == SQLITE_ROW)
        strncpy(string, sqlite3_column_text(sth, 0), sizeof string - 1);
    else
        strncpy(string, defvalue, sizeof string - 1);

    sqlite3_finalize(sth);
    return string;
}