LCOV - code coverage report
Current view: top level - engine/interface - cs.h (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 49.1 % 53 26
Test Date: 2025-02-18 06:21:28 Functions: 68.8 % 16 11

            Line data    Source code
       1              : #ifndef CS_H_
       2              : #define CS_H_
       3              : // cs.h: low level cubscript functionality beyond script binding in command.h
       4              : 
       5              : enum
       6              : {
       7              :     Max_Args = 25,
       8              :     Max_Results = 7,
       9              :     Max_CommandArgs = 12
      10              : };
      11              : 
      12              : enum CubeScriptCodes
      13              : {
      14              :     Code_Start = 0,          //0
      15              :     Code_Offset,
      16              :     Code_Null,
      17              :     Code_True,
      18              :     Code_False,
      19              :     Code_Not,               //5
      20              :     Code_Pop,
      21              :     Code_Enter,
      22              :     Code_EnterResult,
      23              :     Code_Exit,
      24              :     Code_ResultArg,       //10
      25              :     Code_Val,
      26              :     Code_ValI,
      27              :     Code_Dup,
      28              :     Code_Macro,
      29              :     Code_Bool,            //15 (unused)
      30              :     Code_Block,
      31              :     Code_Empty,
      32              :     Code_Compile,
      33              :     Code_Cond,
      34              :     Code_Force,           //20
      35              :     Code_Result,
      36              :     Code_Ident,
      37              :     Code_IdentU,
      38              :     Code_IdentArg,
      39              :     Code_Com,             //25
      40              :     Code_ComD,
      41              :     Code_ComC,
      42              :     Code_ComV,
      43              :     Code_ConC,
      44              :     Code_ConCW,           //30
      45              :     Code_ConCM,
      46              :     Code_Down, // (unused)
      47              :     Code_StrVar,
      48              :     Code_StrVarM,
      49              :     Code_StrVar1,         //35
      50              :     Code_IntVar,
      51              :     Code_IntVar1,
      52              :     Code_IntVar2,
      53              :     Code_IntVar3,
      54              :     Code_FloatVar,        //40
      55              :     Code_FloatVar1,
      56              :     Code_Lookup,
      57              :     Code_LookupU,
      58              :     Code_LookupArg,
      59              :     Code_LookupM,         //45
      60              :     Code_LookupMU,
      61              :     Code_LookupMArg,
      62              :     Code_Alias,
      63              :     Code_AliasU,
      64              :     Code_AliasArg,        //50
      65              :     Code_Call,
      66              :     Code_CallU,
      67              :     Code_CallArg,
      68              :     Code_Print,
      69              :     Code_Local,           //55
      70              :     Code_Do,
      71              :     Code_DoArgs,
      72              :     Code_Jump,
      73              :     Code_JumpTrue,
      74              :     Code_JumpFalse,
      75              :     Code_JumpResultTrue,  //60
      76              :     Code_JumpResultFalse,
      77              : 
      78              :     Code_OpMask = 0x3F,
      79              :     Code_Ret = 6,
      80              :     Code_RetMask = 0xC0,
      81              : 
      82              :     /* return type flags */
      83              :     Ret_Null    = Value_Null<<Code_Ret,
      84              :     Ret_String  = Value_String<<Code_Ret,
      85              :     Ret_Integer = Value_Integer<<Code_Ret,
      86              :     Ret_Float   = Value_Float<<Code_Ret,
      87              : };
      88              : 
      89              : 
      90              : struct stringslice
      91              : {
      92              :     const char *str;
      93              :     int len;
      94         4483 :     stringslice() {}
      95            1 :     stringslice(const char *str, int len) : str(str), len(len) {}
      96            0 :     stringslice(const char *str, const char *end) : str(str), len(static_cast<int>(end-str)) {}
      97              : 
      98           50 :     const char *end() const { return &str[len]; }
      99              : };
     100              : 
     101              : 
     102            0 : inline char *copystring(char *d, const stringslice &s, size_t len)
     103              : {
     104            0 :     size_t slen = min(size_t(s.len), len-1);
     105            0 :     std::memcpy(d, s.str, slen);
     106            0 :     d[slen] = 0;
     107            0 :     return d;
     108              : }
     109              : 
     110              : template<size_t N>
     111            0 : inline char *copystring(char (&d)[N], const stringslice &s) { return copystring(d, s, N); }
     112              : 
     113              : // not all platforms (windows) can parse hexadecimal integers via strtod
     114          276 : inline float parsefloat(const char *s)
     115              : {
     116              :     char *end;
     117          276 :     double val = std::strtod(s, &end);
     118              :     return val
     119           44 :         || end==s
     120          320 :         || (*end!='x' && *end!='X') ? static_cast<float>(val) : static_cast<float>(parseint(s));
     121              : }
     122              : 
     123            2 : inline double parsenumber(const char *s)
     124              : {
     125              :     char *end;
     126            2 :     double val = std::strtod(s, &end);
     127              :     return val
     128            1 :         || end==s
     129            3 :         || (*end!='x' && *end!='X') ? static_cast<double>(val) : static_cast<double>(parseint(s));
     130              : }
     131              : 
     132              : 
     133           68 : inline void intformat(char *buf, int v, int len = 20) { nformatstring(buf, len, "%d", v); }
     134          116 : inline void floatformat(char *buf, float v, int len = 20) { nformatstring(buf, len, v==static_cast<int>(v) ? "%.1f" : "%.7g", v); }
     135              : 
     136              : 
     137              : extern const char *intstr(int v);
     138              : 
     139          354 : inline const char *getstr(const identval &v, int type)
     140              : {
     141          354 :     switch(type)
     142              :     {
     143          247 :         case Value_String:
     144              :         case Value_Macro:
     145              :         case Value_CString:
     146              :         {
     147          247 :             return v.s;
     148              :         }
     149           60 :         case Value_Integer:
     150              :         {
     151           60 :             return intstr(v.i);
     152              :         }
     153            0 :         case Value_Float:
     154              :         {
     155            0 :             return floatstr(v.f);
     156              :         }
     157           47 :         default:
     158              :         {
     159           47 :             return "";
     160              :         }
     161              :     }
     162              : }
     163          247 : inline const char *tagval::getstr() const
     164              : {
     165          247 :     return ::getstr(*this, type);
     166              : }
     167              : 
     168          107 : inline const char *ident::getstr() const
     169              : {
     170          107 :     return ::getstr(alias.val, valtype);
     171              : }
     172              : 
     173            0 : inline void getval(const identval &v, int type, tagval &r)
     174              : {
     175            0 :     switch(type)
     176              :     {
     177            0 :         case Value_String:
     178              :         case Value_Macro:
     179              :         case Value_CString:
     180              :         {
     181            0 :             r.setstr(newstring(v.s));
     182            0 :             break;
     183              :         }
     184            0 :         case Value_Integer:
     185              :         {
     186            0 :             r.setint(v.i);
     187            0 :             break;
     188              :         }
     189            0 :         case Value_Float:
     190              :         {
     191            0 :             r.setfloat(v.f);
     192            0 :             break;
     193              :         }
     194            0 :         default:
     195              :         {
     196            0 :             r.setnull();
     197            0 :             break;
     198              :         }
     199              :     }
     200            0 : }
     201              : 
     202            0 : inline void tagval::getval(tagval &r) const
     203              : {
     204            0 :     ::getval(*this, type, r);
     205            0 : }
     206              : 
     207              : struct NullVal : tagval
     208              : {
     209           29 :     NullVal() { setnull(); }
     210              : };
     211              : 
     212              : struct IdentLink
     213              : {
     214              :     ident *id;
     215              :     IdentLink *next;
     216              :     int usedargs;
     217              :     identstack *argstack;
     218              : };
     219              : 
     220              : extern const char *sourcefile,
     221              :                   *sourcestr;
     222              : 
     223              : extern std::vector<char> strbuf[4];
     224              : extern int stridx;
     225              : 
     226              : extern tagval *commandret;
     227              : extern void executeret(const uint *code, tagval &result = *commandret);
     228              : 
     229              : /**
     230              :  * @brief Executes a given string, returning a tagval by reference parameter
     231              :  *
     232              :  * @param p a string to execute
     233              :  * @param result tagval containing result metadata
     234              :  */
     235              : extern void executeret(const char *p, tagval &result = *commandret);
     236              : 
     237              : /**
     238              :  * @brief Executes a given ident, returning a tagval by reference parameter
     239              :  *
     240              :  * @param id the ident to execute
     241              :  * @param args an array of arguments
     242              :  * @param numargs size of args array
     243              :  * @param lookup whether to lookup (dereference) args (?)
     244              :  * @param result tagval containing result metadata
     245              :  */
     246              : extern void executeret(ident *id, tagval *args, int numargs, bool lookup = false, tagval &result = *commandret);
     247              : 
     248              : extern void poparg(ident &id);
     249              : extern void pusharg(ident &id, const tagval &v, identstack &stack);
     250              : extern bool getbool(const tagval &v);
     251              : extern void cleancode(ident &id);
     252              : extern char *conc(const tagval *v, int n, bool space);
     253              : extern char *conc(const tagval *v, int n, bool space, const char *prefix);
     254              : extern void freearg(tagval &v);
     255              : extern int unescapestring(char *dst, const char *src, const char *end);
     256              : extern const char *parsestring(const char *p);
     257              : extern void setarg(ident &id, tagval &v);
     258              : extern void setalias(ident &id, tagval &v);
     259              : extern void undoarg(ident &id, identstack &stack);
     260              : extern void redoarg(ident &id, const identstack &stack);
     261              : extern const char *parseword(const char *p);
     262              : 
     263              : extern bool validateblock(const char *s);
     264              : extern std::unordered_map<std::string, ident> idents;
     265              : 
     266              : extern void setvarchecked(ident *id, int val);
     267              : extern void setfvarchecked(ident *id, float val);
     268              : extern void setsvarchecked(ident *id, const char *val);
     269              : 
     270              : extern void printvar(const ident *id);
     271              : extern void printvar(const ident *id, int i);
     272              : 
     273              : extern void clearoverrides();
     274              : 
     275              : extern void clearsleep(bool clearoverrides = true);
     276              : 
     277              : extern char *executestr(ident *id, tagval *args, int numargs, bool lookup = false);
     278              : extern uint *compilecode(const char *p);
     279              : extern void freecode(uint *p);
     280              : extern int execute(ident *id, tagval *args, int numargs, bool lookup = false);
     281              : extern bool executebool(ident *id, tagval *args, int numargs, bool lookup = false);
     282              : extern void alias(const char *name, const char *action);
     283              : 
     284              : /**
     285              :  * @brief Converts a list of delimited strings into a vector of elements.
     286              :  *
     287              :  * List elements in the input string are added to the vector of strings passed
     288              :  * as elems.
     289              :  *
     290              :  * The list is parsed according to the CubeScript list formatting.
     291              :  * Tokens in the list are delimited by spaces, unless those tokens are themselves
     292              :  * delimited by [] "" or () characters.
     293              :  *
     294              :  * Existing elements in the elems vector will not be modified. The limit parameter
     295              :  * sets the maximum number of elements in the vector, regardless of whether they
     296              :  * were added by this function.
     297              :  *
     298              :  * Elements added to this vector are all heap-allocated raw character strings and
     299              :  * therefore must be delete[]'d after they are no longer being used
     300              :  *
     301              :  * @param s the list to explode
     302              :  * @param elems the vector to fill with elements
     303              :  * @param limit maximum size of the elems vector allowed
     304              :  */
     305              : extern void explodelist(const char *s, std::vector<char *> &elems, int limit = -1);
     306              : extern void explodelist(const char *s, std::vector<std::string> &elems, int limit = -1);
     307              : 
     308              : extern void result(tagval &v);
     309              : extern const char *numberstr(double v);
     310              : extern float clampfvar(std::string name, float val, float minval, float maxval);
     311              : 
     312              : #endif
        

Generated by: LCOV version 2.0-1