LCOV - code coverage report
Current view: top level - monetdb5/modules/atoms - identifier.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 5 33 15.2 %
Date: 2021-10-13 02:24:04 Functions: 2 5 40.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * @f identifier
      11             :  * @a Fabian Groffen, Martin Kersten
      12             :  * @+ Identifier Wrapper
      13             :  * The identifier atom is a shallow wrapper that contains an object's id.
      14             :  * Due to it being wrapped by this atom, methods can distinguish
      15             :  * it from a normal string.
      16             :  * The variable of this time can be further extended with properties
      17             :  * to further qualify the identifier referenced.
      18             :  *
      19             :  */
      20             : #include "monetdb_config.h"
      21             : #include "mal.h"
      22             : #include "mal_exception.h"
      23             : 
      24             : typedef str identifier;
      25             : 
      26             : static int TYPE_identifier;
      27             : 
      28         266 : static str IDprelude(void *ret)
      29             : {
      30             :         (void) ret;
      31         266 :         TYPE_identifier = ATOMindex("identifier");
      32         266 :         return MAL_SUCCEED;
      33             : }
      34             : 
      35             : /**
      36             :  * Creates a new identifier from the given string (stupid string copy).
      37             :  * Warning: GDK function, does NOT pass a string by reference, and wants
      38             :  * a pointer to a pointer for the retval!
      39             :  * Returns the number of chars read
      40             :  */
      41             : static ssize_t
      42           0 : IDfromString(const char *src, size_t *len, void **RETVAL, bool external)
      43             : {
      44             :         identifier *retval = (identifier *) RETVAL;
      45           0 :         size_t l = strlen(src) + 1;
      46           0 :         if (*retval == NULL || *len < l) {
      47           0 :                 GDKfree(*retval);
      48           0 :                 *retval = GDKmalloc(l);
      49           0 :                 if (*retval == NULL)
      50             :                         return -1;
      51           0 :                 *len = l;
      52             :         }
      53           0 :         if (external && strncmp(src, "nil", 3) == 0) {
      54           0 :                 memcpy(*retval, str_nil, 2);
      55           0 :                 return 3;
      56             :         }
      57           0 :         memcpy(*retval, src, l);
      58           0 :         return (ssize_t) l - 1;
      59             : }
      60             : 
      61             : /**
      62             :  * Returns the string representation of the given identifier.
      63             :  * Warning: GDK function
      64             :  * Returns the length of the string
      65             :  */
      66             : static ssize_t
      67           0 : IDtoString(char **retval, size_t *len, const void *HANDLE, bool external)
      68             : {
      69             :         const char *handle = HANDLE;
      70           0 :         size_t hl = strlen(handle) + 1;
      71           0 :         if (external && strNil(handle))
      72             :                 hl = 4;
      73           0 :         if (*len < hl || *retval == NULL) {
      74           0 :                 GDKfree(*retval);
      75           0 :                 *retval = GDKmalloc(hl);
      76           0 :                 if (*retval == NULL)
      77             :                         return -1;
      78           0 :                 *len = hl;
      79             :         }
      80           0 :         if (external && strNil(handle))
      81           0 :                 strcpy(*retval, "nil");
      82             :         else
      83           0 :                 memcpy(*retval, handle, hl);
      84           0 :         return (ssize_t) hl - 1;
      85             : }
      86             : /**
      87             :  * Returns an identifier, parsed from a string.  The fromStr function is used
      88             :  * to parse the string.
      89             :  */
      90             : static str
      91           0 : IDentifier(identifier *retval, str *in)
      92             : {
      93           0 :         size_t len = 0;
      94             : 
      95           0 :         if (IDfromString(*in, &len, (void **) retval, false) < 0)
      96           0 :                 throw(PARSE, "identifier.identifier", "Error while parsing %s", *in);
      97             : 
      98             :         return (MAL_SUCCEED);
      99             : }
     100             : 
     101             : #include "mel.h"
     102             : mel_atom identifier_init_atoms[] = {
     103             :  { .name="identifier", .basetype="str", .fromstr=IDfromString, .tostr=IDtoString, },  { .cmp=NULL }
     104             : };
     105             : mel_func identifier_init_funcs[] = {
     106             :  command("identifier", "identifier", IDentifier, false, "Cast a string to an identifer ", args(1,2, arg("",identifier),arg("s",str))),
     107             :  command("identifier", "prelude", IDprelude, false, "Initialize the module", args(1,1, arg("",void))),
     108             :  { .imp=NULL }
     109             : };
     110             : #include "mal_import.h"
     111             : #ifdef _MSC_VER
     112             : #undef read
     113             : #pragma section(".CRT$XCU",read)
     114             : #endif
     115         259 : LIB_STARTUP_FUNC(init_identifier_mal)
     116         259 : { mal_module("identifier", identifier_init_atoms, identifier_init_funcs); }

Generated by: LCOV version 1.14