/ Check-in [0ca104d8]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Add the fileio.c loadable extension, that implements readfile() and writefile() SQL functions.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0ca104d821d5841ab0754113be074c520cf07f23
User & Date: drh 2014-06-13 13:43:25
Context
2014-06-16
12:44
Add test cases for the fileio extension. check-in: 86343331 user: drh tags: trunk
2014-06-13
13:43
Add the fileio.c loadable extension, that implements readfile() and writefile() SQL functions. check-in: 0ca104d8 user: drh tags: trunk
13:08
Add an extension that implements compress() and uncompress() SQL functions. check-in: d5c17d1a user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Added ext/misc/fileio.c.

            1  +/*
            2  +** 2014-06-13
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This SQLite extension implements SQL functions readfile() and
           14  +** writefile().
           15  +*/
           16  +#include "sqlite3ext.h"
           17  +SQLITE_EXTENSION_INIT1
           18  +#include <stdio.h>
           19  +
           20  +/*
           21  +** Implementation of the "readfile(X)" SQL function.  The entire content
           22  +** of the file named X is read and returned as a BLOB.  NULL is returned
           23  +** if the file does not exist or is unreadable.
           24  +*/
           25  +static void readfileFunc(
           26  +  sqlite3_context *context,
           27  +  int argc,
           28  +  sqlite3_value **argv
           29  +){
           30  +  const char *zName;
           31  +  FILE *in;
           32  +  long nIn;
           33  +  void *pBuf;
           34  +
           35  +  zName = (const char*)sqlite3_value_text(argv[0]);
           36  +  if( zName==0 ) return;
           37  +  in = fopen(zName, "rb");
           38  +  if( in==0 ) return;
           39  +  fseek(in, 0, SEEK_END);
           40  +  nIn = ftell(in);
           41  +  rewind(in);
           42  +  pBuf = sqlite3_malloc( nIn );
           43  +  if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
           44  +    sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
           45  +  }else{
           46  +    sqlite3_free(pBuf);
           47  +  }
           48  +  fclose(in);
           49  +}
           50  +
           51  +/*
           52  +** Implementation of the "writefile(X,Y)" SQL function.  The argument Y
           53  +** is written into file X.  The number of bytes written is returned.  Or
           54  +** NULL is returned if something goes wrong, such as being unable to open
           55  +** file X for writing.
           56  +*/
           57  +static void writefileFunc(
           58  +  sqlite3_context *context,
           59  +  int argc,
           60  +  sqlite3_value **argv
           61  +){
           62  +  FILE *out;
           63  +  const char *z;
           64  +  int n;
           65  +  long rc;
           66  +  const char *zFile;
           67  +
           68  +  zFile = (const char*)sqlite3_value_text(argv[0]);
           69  +  if( zFile==0 ) return;
           70  +  out = fopen(zFile, "wb");
           71  +  if( out==0 ) return;
           72  +  z = (const char*)sqlite3_value_blob(argv[1]);
           73  +  if( z==0 ){
           74  +    n = 0;
           75  +    rc = 0;
           76  +  }else{
           77  +    n = sqlite3_value_bytes(argv[1]);
           78  +    rc = fwrite(z, 1, n, out);
           79  +  }
           80  +  fclose(out);
           81  +  sqlite3_result_int64(context, rc);
           82  +}
           83  +
           84  +
           85  +#ifdef _WIN32
           86  +__declspec(dllexport)
           87  +#endif
           88  +int sqlite3_fileio_init(
           89  +  sqlite3 *db, 
           90  +  char **pzErrMsg, 
           91  +  const sqlite3_api_routines *pApi
           92  +){
           93  +  int rc = SQLITE_OK;
           94  +  SQLITE_EXTENSION_INIT2(pApi);
           95  +  (void)pzErrMsg;  /* Unused parameter */
           96  +  rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
           97  +                               readfileFunc, 0, 0);
           98  +  if( rc==SQLITE_OK ){
           99  +    rc = sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
          100  +                                 writefileFunc, 0, 0);
          101  +  }
          102  +  return rc;
          103  +}