Line data Source code
1 : /* 2 : * SPDX-License-Identifier: MPL-2.0 3 : * 4 : * This Source Code Form is subject to the terms of the Mozilla Public 5 : * License, v. 2.0. If a copy of the MPL was not distributed with this 6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 : * 8 : * Copyright 2024, 2025 MonetDB Foundation; 9 : * Copyright August 2008 - 2023 MonetDB B.V.; 10 : * Copyright 1997 - July 2008 CWI. 11 : */ 12 : 13 : #include "monetdb_config.h" 14 : #include "rel_remote.h" 15 : #include "msettings.h" 16 : 17 : static void * 18 13959 : msettings_sa_allocator(void *state, void *old, size_t size) 19 : { 20 13959 : allocator *sa = state; 21 : 22 13959 : if (size == 0) { 23 : // This is really a free(), ignore it. 24 : return NULL; 25 10320 : } else if (old == NULL) { 26 : // This is really a malloc() 27 10320 : return sa_alloc(sa, size); 28 : } else { 29 : // We can't handle generic realloc because we don't know how large the 30 : // previous allocation was, so we don't know how much to copy. 31 : // Fortunately, msettings doesn't really reallocate so we don't need 32 : // this for now. 33 0 : assert(size == 0 || old == NULL); 34 : return NULL; 35 : } 36 : } 37 : 38 : msettings * 39 1509 : sa_msettings_create(allocator *sa) 40 : { 41 1509 : return msettings_create_with(msettings_sa_allocator, sa); 42 : } 43 : 44 : char* 45 648 : sa_msettings_to_string(const msettings *mp, allocator *sa, size_t size_hint) 46 : { 47 648 : size_t buffer_size = size_hint ? size_hint + 1 : 80; 48 648 : do { 49 648 : char *buffer = sa_alloc(sa, buffer_size); 50 648 : if (!buffer) 51 : return NULL; 52 648 : size_t needed = msettings_write_url(mp, buffer, buffer_size); 53 648 : if (needed + 1 <= buffer_size) 54 648 : return buffer; 55 : buffer_size = needed + 1; 56 : } while (1); 57 : } 58 : 59 : int 60 101 : mapiuri_valid( const char *uri, allocator *sa) 61 : { 62 101 : msettings *mp = sa_msettings_create(sa); 63 101 : return msettings_parse_url(mp, uri) == NULL; 64 : } 65 : 66 : /* assume valid uri's next functions */ 67 : 68 : /* strip schema- and table name from uri */ 69 : const char * 70 540 : mapiuri_uri( const char *uri, allocator *sa) 71 : { 72 540 : msettings *mp = sa_msettings_create(sa); 73 540 : if (!mp || msettings_parse_url(mp, uri) != NULL) 74 0 : return NULL; 75 540 : msetting_set_string(mp, MP_TABLESCHEMA, ""); 76 540 : msetting_set_string(mp, MP_TABLE, ""); 77 540 : return sa_msettings_to_string(mp, sa, strlen(uri)); 78 : } 79 : 80 : const char * 81 218 : mapiuri_schema( const char *uri, allocator *sa, const char *fallback) 82 : { 83 218 : msettings *mp = sa_msettings_create(sa); 84 218 : if (!mp || msettings_parse_url(mp, uri) != NULL) 85 0 : return fallback; 86 218 : const char *schema = msetting_string(mp, MP_TABLESCHEMA); 87 218 : return schema[0] ? schema : fallback; 88 : } 89 : 90 : const char * 91 542 : mapiuri_table( const char *uri, allocator *sa, const char *fallback) 92 : { 93 542 : msettings *mp = sa_msettings_create(sa); 94 542 : if (!mp || msettings_parse_url(mp, uri) != NULL) 95 0 : return fallback; 96 542 : const char *schema = msetting_string(mp, MP_TABLE); 97 542 : return schema[0] ? schema : fallback; 98 : }