"Fossies" - the Fresh Open Source Software Archive

Member "gretl-2020e/lib/src/gretl_typemap.c" (9 Aug 2020, 9652 Bytes) of package /linux/misc/gretl-2020e.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "gretl_typemap.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2020d_vs_2020e.

    1 /*
    2  *  gretl -- Gnu Regression, Econometrics and Time-series Library
    3  *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
    4  *
    5  *  This program is free software: you can redistribute it and/or modify
    6  *  it under the terms of the GNU General Public License as published by
    7  *  the Free Software Foundation, either version 3 of the License, or
    8  *  (at your option) any later version.
    9  *
   10  *  This program is distributed in the hope that it will be useful,
   11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  *  GNU General Public License for more details.
   14  *
   15  *  You should have received a copy of the GNU General Public License
   16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  *
   18  */
   19 
   20 #include "libgretl.h"
   21 #include "gretl_typemap.h"
   22 
   23 struct type_map {
   24     GretlType std;     /* a "standard" type: non-array, non-reference */
   25     GretlType stdref;  /* the "reference" form of the standard type, if any */
   26     GretlType plural;  /* the associated array type, if any */
   27     GretlType plref;   /* reference form of array type, if any */
   28 };
   29 
   30 static struct type_map gretl_type_map[] = {
   31     { GRETL_TYPE_MATRIX,   GRETL_TYPE_MATRIX_REF,
   32       GRETL_TYPE_MATRICES, GRETL_TYPE_MATRICES_REF},
   33     { GRETL_TYPE_BUNDLE,   GRETL_TYPE_BUNDLE_REF,
   34       GRETL_TYPE_BUNDLES,  GRETL_TYPE_BUNDLES_REF},
   35     { GRETL_TYPE_STRING,   GRETL_TYPE_STRING_REF,
   36       GRETL_TYPE_STRINGS,  GRETL_TYPE_STRINGS_REF},
   37     { GRETL_TYPE_ARRAY,    GRETL_TYPE_ARRAY_REF,
   38       GRETL_TYPE_ARRAYS,   GRETL_TYPE_ARRAYS_REF},
   39     { GRETL_TYPE_LIST,     GRETL_TYPE_LIST_REF,
   40       GRETL_TYPE_LISTS,    GRETL_TYPE_LISTS_REF},
   41     { GRETL_TYPE_SERIES,   GRETL_TYPE_SERIES_REF, 0, 0},
   42     { GRETL_TYPE_DOUBLE,   GRETL_TYPE_SCALAR_REF, 0, 0}
   43 };
   44 
   45 GretlType gretl_type_get_plural (GretlType type)
   46 {
   47     int i, n = G_N_ELEMENTS(gretl_type_map);
   48 
   49     if (type == 0) return 0;
   50 
   51     for (i=0; i<n; i++) {
   52     if (type == gretl_type_map[i].std) {
   53         return gretl_type_map[i].plural;
   54     }
   55     }
   56 
   57     return 0;
   58 }
   59 
   60 GretlType gretl_type_get_singular (GretlType type)
   61 {
   62     int i, n = G_N_ELEMENTS(gretl_type_map);
   63 
   64     if (type == 0) return 0;
   65 
   66     for (i=0; i<n; i++) {
   67     if (type == gretl_type_map[i].plural) {
   68         return gretl_type_map[i].std;
   69     }
   70     }
   71 
   72     return 0;
   73 }
   74 
   75 GretlType gretl_type_get_ref_type (GretlType type)
   76 {
   77     int i, n = G_N_ELEMENTS(gretl_type_map);
   78 
   79     if (type == 0) return 0;
   80 
   81     for (i=0; i<n; i++) {
   82     if (type == gretl_type_map[i].std) {
   83         return gretl_type_map[i].stdref;
   84     }
   85     if (type == gretl_type_map[i].plural) {
   86         return gretl_type_map[i].plref;
   87     }
   88     if (type == gretl_type_map[i].stdref ||
   89         type == gretl_type_map[i].plref) {
   90         /* allow pass-through */
   91         return type;
   92     }
   93     }
   94 
   95     return 0;
   96 }
   97 
   98 GretlType gretl_type_get_plain_type (GretlType type)
   99 {
  100     int i, n = G_N_ELEMENTS(gretl_type_map);
  101 
  102     if (type == 0) return 0;
  103 
  104     for (i=0; i<n; i++) {
  105     if (type == gretl_type_map[i].stdref) {
  106         return gretl_type_map[i].std;
  107     }
  108     if (type == gretl_type_map[i].plref) {
  109         return gretl_type_map[i].plural;
  110     }
  111     if (type == gretl_type_map[i].std ||
  112         type == gretl_type_map[i].plural) {
  113         /* allow pass-through */
  114         return type;
  115     }
  116     }
  117 
  118     return 0;
  119 }
  120 
  121 /**
  122  * gretl_type_get_name:
  123  * @type: a gretl type.
  124  *
  125  * Returns: the name of @type, or "invalid" on failure.
  126  */
  127 
  128 const char *gretl_type_get_name (GretlType type)
  129 {
  130     switch (type) {
  131     case GRETL_TYPE_BOOL:       return "bool";
  132     case GRETL_TYPE_INT:        return "int";
  133     case GRETL_TYPE_UNSIGNED:   return "unsigned";
  134     case GRETL_TYPE_OBS:        return "obs";
  135     case GRETL_TYPE_DOUBLE:     return "scalar";
  136     case GRETL_TYPE_SERIES:     return "series";
  137     case GRETL_TYPE_USERIES:    return "series";
  138     case GRETL_TYPE_MATRIX:     return "matrix";
  139     case GRETL_TYPE_LIST:       return "list";
  140     case GRETL_TYPE_BUNDLE:     return "bundle";
  141     case GRETL_TYPE_ARRAY:      return "array";
  142     case GRETL_TYPE_STRING:     return "string";
  143     case GRETL_TYPE_SCALAR_REF: return "scalar *";
  144     case GRETL_TYPE_SERIES_REF: return "series *";
  145     case GRETL_TYPE_MATRIX_REF: return "matrix *";
  146     case GRETL_TYPE_BUNDLE_REF: return "bundle *";
  147     case GRETL_TYPE_ARRAY_REF:  return "array *";
  148     case GRETL_TYPE_STRING_REF: return "string *";
  149     case GRETL_TYPE_LIST_REF:   return "list *";
  150 
  151     case GRETL_TYPE_STRINGS:      return "strings";
  152     case GRETL_TYPE_MATRICES:     return "matrices";
  153     case GRETL_TYPE_BUNDLES:      return "bundles";
  154     case GRETL_TYPE_LISTS:        return "lists";
  155     case GRETL_TYPE_ARRAYS:       return "arrays";
  156 
  157     case GRETL_TYPE_STRINGS_REF:  return "strings *";
  158     case GRETL_TYPE_MATRICES_REF: return "matrices *";
  159     case GRETL_TYPE_BUNDLES_REF:  return "bundles *";
  160     case GRETL_TYPE_LISTS_REF:    return "lists *";
  161     case GRETL_TYPE_ARRAYS_REF:   return "arrays *";
  162 
  163     case GRETL_TYPE_DATE:         return "date"; /* ODBC special */
  164 
  165     case GRETL_TYPE_VOID:       return "void";
  166     case GRETL_TYPE_NONE:       return "null";
  167     case GRETL_TYPE_ANY:        return "any";
  168     default:
  169     return "invalid";
  170     }
  171 }
  172 
  173 GretlType gretl_type_from_string (const char *s)
  174 {
  175     const char *p;
  176 
  177     if (!strncmp(s, "matrix", 6)) {
  178     p = s + 6;
  179     if (*p == '\0') {
  180         return GRETL_TYPE_MATRIX;
  181     } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  182         return GRETL_TYPE_MATRIX_REF;
  183     }
  184     } else if (!strncmp(s, "series", 6)) {
  185     p = s + 6;
  186     if (*p == '\0') {
  187         return GRETL_TYPE_SERIES;
  188     } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  189         return GRETL_TYPE_SERIES_REF;
  190     }
  191     } else if (!strncmp(s, "scalar", 6)) {
  192     p = s + 6;
  193     if (*p == '\0') {
  194         return GRETL_TYPE_DOUBLE;
  195     } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  196         return GRETL_TYPE_SCALAR_REF;
  197     }
  198     } else if (!strncmp(s, "bundle", 6)) {
  199     p = s + 6;
  200     if (*p == 's') {
  201         p++;
  202         if (*p == '\0') {
  203         return GRETL_TYPE_BUNDLES;
  204         } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  205         return GRETL_TYPE_BUNDLES_REF;
  206         }
  207     } else if (*p == '\0') {
  208         return GRETL_TYPE_BUNDLE;
  209     } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  210         return GRETL_TYPE_BUNDLE_REF;
  211     }
  212     } else if (!strncmp(s, "array", 5)) {
  213     p = s + 5;
  214     if (*p == 's') {
  215         p++;
  216         if (*p == '\0') {
  217         return GRETL_TYPE_ARRAYS;
  218         } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  219         return GRETL_TYPE_ARRAYS_REF;
  220         }
  221     } else if (*p == '\0') {
  222         return GRETL_TYPE_ARRAY;
  223     } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  224         return GRETL_TYPE_ARRAY_REF;
  225     }
  226     } else if (!strncmp(s, "string", 6)) {
  227     p = s + 6;
  228     if (*p == 's') {
  229         p++;
  230         if (*p == '\0') {
  231         return GRETL_TYPE_STRINGS;
  232         } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  233         return GRETL_TYPE_STRINGS_REF;
  234         }
  235     } else if (*p == '\0') {
  236         return GRETL_TYPE_STRING;
  237     } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  238         return GRETL_TYPE_STRING_REF;
  239     }
  240     } else if (!strncmp(s, "matrices", 8)) {
  241     p = s + 8;
  242     if (*p == '\0') {
  243         return GRETL_TYPE_MATRICES;
  244     } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  245         return GRETL_TYPE_MATRICES_REF;
  246     }
  247     } else if (!strncmp(s, "list", 4)) {
  248     p = s + 4;
  249     if (*p == 's') {
  250         p++;
  251         if (*p == '\0') {
  252         return GRETL_TYPE_LISTS;
  253         } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  254         return GRETL_TYPE_LISTS_REF;
  255         }
  256     } else if (*p == '\0') {
  257         return GRETL_TYPE_LIST;
  258     } else if (!strcmp(p, " *") || !strcmp(p, "ref")) {
  259         return GRETL_TYPE_LIST_REF;
  260     }
  261     } else {
  262     /* aliases */
  263     if (!strcmp(s, "bool"))     return GRETL_TYPE_BOOL;
  264     if (!strcmp(s, "boolean"))  return GRETL_TYPE_BOOL;
  265     if (!strcmp(s, "int"))      return GRETL_TYPE_INT;
  266     if (!strcmp(s, "unsigned")) return GRETL_TYPE_UNSIGNED;
  267     if (!strcmp(s, "obs"))      return GRETL_TYPE_OBS;
  268     }
  269 
  270     return GRETL_TYPE_NONE;
  271 }
  272 
  273 struct lookup {
  274     const char *word;
  275     GretlType type;
  276 };
  277 
  278 static const struct lookup gentypes[] = {
  279     { "series",   GRETL_TYPE_SERIES },
  280     { "scalar",   GRETL_TYPE_DOUBLE },
  281     { "matrix",   GRETL_TYPE_MATRIX },
  282     { "string",   GRETL_TYPE_STRING },
  283     { "list",     GRETL_TYPE_LIST },
  284     { "bundle",   GRETL_TYPE_BUNDLE },
  285     { "strings",  GRETL_TYPE_STRINGS },
  286     { "matrices", GRETL_TYPE_MATRICES },
  287     { "bundles",  GRETL_TYPE_BUNDLES },
  288     { "lists",    GRETL_TYPE_LISTS },
  289     { "arrays",   GRETL_TYPE_ARRAYS },
  290 };
  291 
  292 GretlType gretl_get_gen_type (const char *s)
  293 {
  294     static GHashTable *ht;
  295     gpointer p;
  296     GretlType t = 0;
  297 
  298     if (s == NULL) {
  299     /* the clean-up signal */
  300     if (ht != NULL) {
  301         g_hash_table_destroy(ht);
  302         ht = NULL;
  303     }
  304     return 0;
  305     }
  306 
  307     if (ht == NULL) {
  308     int i, n = G_N_ELEMENTS(gentypes);
  309 
  310     ht = g_hash_table_new(g_str_hash, g_str_equal);
  311     for (i=0; i<n; i++) {
  312         g_hash_table_insert(ht, (gpointer) gentypes[i].word,
  313                 GINT_TO_POINTER(gentypes[i].type));
  314     }
  315     }
  316 
  317     p = g_hash_table_lookup(ht, s);
  318     if (p != NULL) {
  319     t = GPOINTER_TO_INT(p);
  320     }
  321 
  322     return t;
  323 }
  324 
  325 int gretl_is_array_type (GretlType type)
  326 {
  327     return type == GRETL_TYPE_STRINGS ||
  328     type == GRETL_TYPE_MATRICES ||
  329     type == GRETL_TYPE_BUNDLES ||
  330     type == GRETL_TYPE_LISTS ||
  331     type == GRETL_TYPE_ARRAYS;
  332 }
  333 
  334 int gretl_is_arrayable_type (GretlType type)
  335 {
  336     return type == GRETL_TYPE_STRING ||
  337     type == GRETL_TYPE_MATRIX ||
  338     type == GRETL_TYPE_BUNDLE ||
  339     type == GRETL_TYPE_LIST ||
  340     type == GRETL_TYPE_ARRAY;
  341 }
  342 
  343 int gretl_is_scalar_type (GretlType type)
  344 {
  345     return type == GRETL_TYPE_BOOL ||
  346     type == GRETL_TYPE_INT ||
  347     type == GRETL_TYPE_UNSIGNED ||
  348     type == GRETL_TYPE_DOUBLE;
  349 }
  350 
  351 void gretl_typemap_cleanup (void)
  352 {
  353     gretl_get_gen_type(NULL);
  354 }