libfsbv.c - clic - Clic is an command line interactive client for gopher written in Common LISP
 (HTM) git clone git://bitreich.org/clic/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/clic/
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
       libfsbv.c (3900B)
       ---
            1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*-
            2  *
            3  * libfsbv.c --- auxiliary C lib for testing foreign structure by value calls
            4  *
            5  * Copyright (C) 2011, 2015 Liam M. Healy
            6  *
            7  * Permission is hereby granted, free of charge, to any person
            8  * obtaining a copy of this software and associated documentation
            9  * files (the "Software"), to deal in the Software without
           10  * restriction, including without limitation the rights to use, copy,
           11  * modify, merge, publish, distribute, sublicense, and/or sell copies
           12  * of the Software, and to permit persons to whom the Software is
           13  * furnished to do so, subject to the following conditions:
           14  *
           15  * The above copyright notice and this permission notice shall be
           16  * included in all copies or substantial portions of the Software.
           17  *
           18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
           19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
           20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
           21  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
           22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
           23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
           24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
           25  * DEALINGS IN THE SOFTWARE.
           26  */
           27 
           28 #ifdef WIN32
           29 #define DLLEXPORT __declspec(dllexport)
           30 #else
           31 #define DLLEXPORT
           32 #endif
           33 
           34 #include <stdio.h>
           35 #include <limits.h>
           36 #include <string.h>
           37 #include <stdlib.h>
           38 #include <stdbool.h>
           39 #include <math.h>
           40 #include <float.h>
           41 
           42 /* MSVC doesn't have stdint.h and uses a different syntax for stdcall */
           43 #ifndef _MSC_VER
           44 #include <stdint.h>
           45 #endif
           46 
           47 #ifdef WIN32
           48 #ifdef _MSC_VER
           49 #define STDCALL __stdcall
           50 #else
           51 #define STDCALL __attribute__((stdcall))
           52 #endif
           53 #else
           54 #define STDCALL
           55 #endif
           56 
           57 struct struct_pair {
           58     int a;
           59     int b;
           60 };
           61 
           62 struct struct_pair_double {
           63     struct struct_pair pr;
           64     double dbl;
           65 };
           66 
           67 typedef enum {
           68     ONE = 1,
           69     TWO,
           70     THREE,
           71     FOUR,
           72     FORTY_ONE = 41,
           73     FORTY_TWO
           74 } numeros;
           75 
           76 int sumpair (struct struct_pair sp);
           77 int enumpair (numeros mynum, struct struct_pair sp);
           78 struct struct_pair doublepair (struct struct_pair dp);
           79 double prodsumpair (struct struct_pair_double spd);
           80 struct struct_pair_double doublepairdouble (struct struct_pair_double pd);
           81 
           82 DLLEXPORT
           83 int sumpair (struct struct_pair sp)
           84 {
           85   return sp.a + sp.b;
           86 }
           87 
           88 DLLEXPORT
           89 int enumpair (numeros mynum, struct struct_pair sp)
           90 {
           91   if ( mynum == ONE )
           92     {
           93       return sp.a + sp.b;
           94     }
           95   else if ( mynum == TWO )
           96     {
           97       return sp.a + 2*sp.b;
           98     }
           99   else if ( mynum == THREE )
          100     {
          101       return 2*sp.a + sp.b;
          102     }
          103   else if ( mynum == FOUR )
          104     {
          105       return 2*sp.a + 2*sp.b;
          106     }
          107   else 
          108     {
          109       return 41*sp.a + 42*sp.b;
          110     }
          111 }
          112 
          113 DLLEXPORT
          114 struct struct_pair makepair (bool cond)
          115 {
          116   struct struct_pair ret;
          117   ret.a = -127;
          118   ret.b = cond ? 42 : 43;
          119   return ret;
          120 }
          121 
          122 const struct struct_pair static_pair = { 40, 2};
          123 
          124 DLLEXPORT
          125 struct struct_pair * returnpairpointer (struct struct_pair ignored)
          126 {
          127   return &static_pair;
          128 }
          129 
          130 DLLEXPORT
          131 struct struct_pair doublepair (struct struct_pair dp)
          132 {
          133   struct struct_pair ret;
          134   ret.a = 2*dp.a;
          135   ret.b = 2*dp.b;
          136   return ret;
          137 }
          138 
          139 DLLEXPORT
          140 double prodsumpair (struct struct_pair_double pd)
          141 {
          142   return pd.dbl * sumpair(pd.pr);
          143 }
          144 
          145 DLLEXPORT
          146 struct struct_pair_double doublepairdouble (struct struct_pair_double pd)
          147 {
          148   struct struct_pair_double ret;
          149   ret.pr = doublepair(pd.pr);
          150   ret.dbl = 2*pd.dbl;
          151   return ret;
          152 }
          153 
          154 DLLEXPORT
          155 unsigned long long ullsum (unsigned long long a, unsigned long long b)
          156 {
          157   return a + b;
          158 }
          159 
          160 DLLEXPORT
          161 struct struct_pair stringlenpair (char *string, struct struct_pair dp)
          162 {
          163   struct struct_pair ret;
          164   int len = strlen(string);
          165   ret.a = len*dp.a;
          166   ret.b = len*dp.b;
          167   return ret;
          168 }
          169 
          170 struct bitfield_struct {
          171   unsigned int b;
          172 };
          173 
          174 DLLEXPORT
          175 struct bitfield_struct structbitfield (unsigned int x) {
          176   struct bitfield_struct ret;
          177   ret.b = x;
          178   return ret;
          179 }