Fossil SCM

Changed the new json_deserialize_array() interface to make it easier to call correctly.

wyoung 2021-06-22 07:52 fossil-spawn
Commit 765acbc080804aa61066b9c15a7bb355b883053784258a1d4f477ad545f592f1
1 file changed +5 -5
+5 -5
--- src/db.c
+++ src/db.c
@@ -4540,11 +4540,11 @@
45404540
if( g.argc!=3 ) usage("FILE");
45414541
blob_zero(&json);
45424542
sqlite3_open(":memory:", &g.db);
45434543
if( blob_read_from_file(&json, g.argv[2], ExtFILE)>=0 ) {
45444544
size_t nValues;
4545
- char** azValues = json_deserialize_array(&nValues, blob_str(&json));
4545
+ const char** azValues = json_deserialize_array(&nValues, blob_str(&json));
45464546
int i;
45474547
for( i=0; i<nValues; ++i ){
45484548
fossil_print("JSON[%d] = \"%s\"\n", i, azValues[i]);
45494549
}
45504550
}
@@ -4553,15 +4553,15 @@
45534553
}
45544554
45554555
/*
45564556
** Deserializes the passed JSON array of strings as a C array of C strings.
45574557
*/
4558
-char** json_deserialize_array(size_t* pnValues, const char* zJSON){
4559
- char** azValues;
4560
- size_t i=0, n;
4558
+const char** json_deserialize_array(size_t* pnValues, const char* zJSON){
4559
+ const char** azValues;
4560
+ size_t i = 0, n = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON);
45614561
Stmt q;
4562
- n = *pnValues = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON);
4562
+ if(pnValues) *pnValues = n;
45634563
azValues = fossil_malloc(sizeof(char*) * (n+1));
45644564
db_prepare(&q, "SELECT json_each.value FROM json_each(%Q)", zJSON);
45654565
while( (i<n) && (db_step(&q)==SQLITE_ROW) ){
45664566
azValues[i++] = fossil_strdup(db_column_text(&q, 0));
45674567
}
45684568
--- src/db.c
+++ src/db.c
@@ -4540,11 +4540,11 @@
4540 if( g.argc!=3 ) usage("FILE");
4541 blob_zero(&json);
4542 sqlite3_open(":memory:", &g.db);
4543 if( blob_read_from_file(&json, g.argv[2], ExtFILE)>=0 ) {
4544 size_t nValues;
4545 char** azValues = json_deserialize_array(&nValues, blob_str(&json));
4546 int i;
4547 for( i=0; i<nValues; ++i ){
4548 fossil_print("JSON[%d] = \"%s\"\n", i, azValues[i]);
4549 }
4550 }
@@ -4553,15 +4553,15 @@
4553 }
4554
4555 /*
4556 ** Deserializes the passed JSON array of strings as a C array of C strings.
4557 */
4558 char** json_deserialize_array(size_t* pnValues, const char* zJSON){
4559 char** azValues;
4560 size_t i=0, n;
4561 Stmt q;
4562 n = *pnValues = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON);
4563 azValues = fossil_malloc(sizeof(char*) * (n+1));
4564 db_prepare(&q, "SELECT json_each.value FROM json_each(%Q)", zJSON);
4565 while( (i<n) && (db_step(&q)==SQLITE_ROW) ){
4566 azValues[i++] = fossil_strdup(db_column_text(&q, 0));
4567 }
4568
--- src/db.c
+++ src/db.c
@@ -4540,11 +4540,11 @@
4540 if( g.argc!=3 ) usage("FILE");
4541 blob_zero(&json);
4542 sqlite3_open(":memory:", &g.db);
4543 if( blob_read_from_file(&json, g.argv[2], ExtFILE)>=0 ) {
4544 size_t nValues;
4545 const char** azValues = json_deserialize_array(&nValues, blob_str(&json));
4546 int i;
4547 for( i=0; i<nValues; ++i ){
4548 fossil_print("JSON[%d] = \"%s\"\n", i, azValues[i]);
4549 }
4550 }
@@ -4553,15 +4553,15 @@
4553 }
4554
4555 /*
4556 ** Deserializes the passed JSON array of strings as a C array of C strings.
4557 */
4558 const char** json_deserialize_array(size_t* pnValues, const char* zJSON){
4559 const char** azValues;
4560 size_t i = 0, n = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON);
4561 Stmt q;
4562 if(pnValues) *pnValues = n;
4563 azValues = fossil_malloc(sizeof(char*) * (n+1));
4564 db_prepare(&q, "SELECT json_each.value FROM json_each(%Q)", zJSON);
4565 while( (i<n) && (db_step(&q)==SQLITE_ROW) ){
4566 azValues[i++] = fossil_strdup(db_column_text(&q, 0));
4567 }
4568

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button