Add the fileio.c loadable extension, that implements readfile() and writefile()

SQL functions.

FossilOrigin-Name: 0ca104d821d5841ab0754113be074c520cf07f23
This commit is contained in:
drh 2014-06-13 13:43:25 +00:00
parent 0b93058b0e
commit 40e75cb6f6
3 changed files with 110 additions and 6 deletions

103
ext/misc/fileio.c Normal file
View File

@ -0,0 +1,103 @@
/*
** 2014-06-13
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
******************************************************************************
**
** This SQLite extension implements SQL functions readfile() and
** writefile().
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <stdio.h>
/*
** Implementation of the "readfile(X)" SQL function. The entire content
** of the file named X is read and returned as a BLOB. NULL is returned
** if the file does not exist or is unreadable.
*/
static void readfileFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
const char *zName;
FILE *in;
long nIn;
void *pBuf;
zName = (const char*)sqlite3_value_text(argv[0]);
if( zName==0 ) return;
in = fopen(zName, "rb");
if( in==0 ) return;
fseek(in, 0, SEEK_END);
nIn = ftell(in);
rewind(in);
pBuf = sqlite3_malloc( nIn );
if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
}else{
sqlite3_free(pBuf);
}
fclose(in);
}
/*
** Implementation of the "writefile(X,Y)" SQL function. The argument Y
** is written into file X. The number of bytes written is returned. Or
** NULL is returned if something goes wrong, such as being unable to open
** file X for writing.
*/
static void writefileFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
FILE *out;
const char *z;
int n;
long rc;
const char *zFile;
zFile = (const char*)sqlite3_value_text(argv[0]);
if( zFile==0 ) return;
out = fopen(zFile, "wb");
if( out==0 ) return;
z = (const char*)sqlite3_value_blob(argv[1]);
if( z==0 ){
n = 0;
rc = 0;
}else{
n = sqlite3_value_bytes(argv[1]);
rc = fwrite(z, 1, n, out);
}
fclose(out);
sqlite3_result_int64(context, rc);
}
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_fileio_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
int rc = SQLITE_OK;
SQLITE_EXTENSION_INIT2(pApi);
(void)pzErrMsg; /* Unused parameter */
rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
readfileFunc, 0, 0);
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
writefileFunc, 0, 0);
}
return rc;
}

View File

@ -1,5 +1,5 @@
C Add\san\sextension\sthat\simplements\scompress()\sand\suncompress()\sSQL\sfunctions.
D 2014-06-13T13:08:21.440
C Add\sthe\sfileio.c\sloadable\sextension,\sthat\simplements\sreadfile()\sand\swritefile()\nSQL\sfunctions.
D 2014-06-13T13:43:25.488
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in dd2b1aba364ff9b05de41086f74407f285c57670
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -109,6 +109,7 @@ F ext/icu/sqliteicu.h 728867a802baa5a96de7495e9689a8e01715ef37
F ext/misc/amatch.c 678056a4bfcd83c4e82dea81d37543cd1d6dbee1
F ext/misc/closure.c 636024302cde41b2bf0c542f81c40c624cfb7012
F ext/misc/compress.c 76e45655f4046e756064ab10c62e18f2eb846b9f
F ext/misc/fileio.c 2927bf833edad966ed222f0552c0602ae473ca16
F ext/misc/fuzzer.c 136533c53cfce0957f0b48fa11dba27e21c5c01d
F ext/misc/ieee754.c b0362167289170627659e84173f5d2e8fee8566e
F ext/misc/nextchar.c 35c8b8baacb96d92abbb34a83a997b797075b342
@ -1175,7 +1176,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 2aeacf81df92b4fe5d1825c1dc1cd176b8af8bd9
R d1737f084b807e547e6c83682c2e8d71
P d5c17d1a423321f766616d82c9b27ef87c1d5afd
R 1805f6a0ae3bbffdcfbcff54f7b6c6b0
U drh
Z 8447f1843125715fcf2f15ddbc0454fa
Z 904e0ffbd1a81754412e5093211c823c

View File

@ -1 +1 @@
d5c17d1a423321f766616d82c9b27ef87c1d5afd
0ca104d821d5841ab0754113be074c520cf07f23