MCPcopy Create free account
hub / github.com/MariaDB/server / jfile_bjson

Function jfile_bjson

storage/connect/jsonudf.cpp:6006–6103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6004} // end of jfile_bjson_init
6005
6006char *jfile_bjson(UDF_INIT *initid, UDF_ARGS *args, char *result,
6007 unsigned long *res_length, uchar *, uchar *error) {
6008 char *fn, *ofn, *buf, *str = NULL;
6009 bool loop;
6010 ssize_t len, newloc;
6011 size_t lrecl, *binszp;
6012 PJSON jsp;
6013 SWAP *swp;
6014 PGLOBAL g = (PGLOBAL)initid->ptr;
6015
6016 PlugSubSet(g->Sarea, g->Sarea_Size);
6017 fn = MakePSZ(g, args, 0);
6018 ofn = MakePSZ(g, args, 1);
6019
6020 if (args->arg_count == 3)
6021 lrecl = (size_t)*(longlong*)args->args[2];
6022 else
6023 lrecl = 1024;
6024
6025 if (!g->Xchk) {
6026 int msgid = MSGID_OPEN_MODE_STRERROR;
6027 FILE *fout = NULL;
6028 FILE *fin;
6029
6030 if (!(fin = global_fopen(g, msgid, fn, "rt")))
6031 str = strcpy(result, g->Message);
6032 else if (!(fout = global_fopen(g, msgid, ofn, "wb")))
6033 str = strcpy(result, g->Message);
6034 else if ((buf = (char*)PlgDBSubAlloc(g, NULL, lrecl)) &&
6035 (binszp = (size_t*)PlgDBSubAlloc(g, NULL, sizeof(size_t)))) {
6036 JsonMemSave(g);
6037
6038 try {
6039 do {
6040 loop = false;
6041 JsonSubSet(g);
6042
6043 if (!fgets(buf, lrecl, fin)) {
6044 if (!feof(fin)) {
6045 snprintf(g->Message, sizeof(g->Message), "Error %d reading %zu bytes from %s", errno, lrecl, fn);
6046 str = strcpy(result, g->Message);
6047 } else
6048 str = strcpy(result, ofn);
6049
6050 } else if ((len = strlen(buf))) {
6051 if ((jsp = ParseJson(g, buf, len))) {
6052 newloc = (size_t)PlugSubAlloc(g, NULL, 0);
6053 *binszp = newloc - (size_t)jsp;
6054
6055 swp = new(g) SWAP(g, jsp);
6056 swp->SwapJson(jsp, true);
6057
6058 if (fwrite(binszp, sizeof(binszp), 1, fout) != 1) {
6059 snprintf(g->Message, sizeof(g->Message), "Error %d writing %zu bytes to %s",
6060 errno, sizeof(binszp), ofn);
6061 str = strcpy(result, g->Message);
6062 } else if (fwrite(jsp, *binszp, 1, fout) != 1) {
6063 snprintf(g->Message, sizeof(g->Message), "Error %d writing %zu bytes to %s",

Callers

nothing calls this directly

Calls 9

PlugSubSetFunction · 0.85
MakePSZFunction · 0.85
global_fopenFunction · 0.85
PlgDBSubAllocFunction · 0.85
JsonSubSetFunction · 0.85
ParseJsonFunction · 0.85
PlugSubAllocFunction · 0.85
SwapJsonMethod · 0.80
JsonMemSaveFunction · 0.70

Tested by

no test coverage detected