LCOV - code coverage report
Current view: top level - engine/interface - cs.h (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 54.8 % 62 34
Test Date: 2026-02-11 06:32:47 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              : struct stringslice final
      90              : {
      91              :     const char *str;
      92              :     int len;
      93         4481 :     stringslice() {}
      94            1 :     stringslice(const char *str, int len) : str(str), len(len) {}
      95            0 :     stringslice(const char *str, const char *end) : str(str), len(static_cast<int>(end-str)) {}
      96              : 
      97           50 :     const char *end() const
      98              :     {
      99           50 :         return &str[len];
     100              :     }
     101              : };
     102              : 
     103            0 : inline char *copystring(char *d, const stringslice &s, size_t len)
     104              : {
     105            0 :     size_t slen = min(size_t(s.len), len-1);
     106            0 :     std::memcpy(d, s.str, slen);
     107            0 :     d[slen] = 0;
     108            0 :     return d;
     109              : }
     110              : 
     111              : template<size_t N>
     112            0 : inline char *copystring(char (&d)[N], const stringslice &s)
     113              : {
     114            0 :     return copystring(d, s, N);
     115              : }
     116              : 
     117              : // not all platforms (windows) can parse hexadecimal integers via strtod
     118          276 : inline float parsefloat(const char *s)
     119              : {
     120              :     char *end;
     121          276 :     double val = std::strtod(s, &end);
     122              :     return val
     123           44 :         || end==s
     124          320 :         || (*end!='x' && *end!='X') ? static_cast<float>(val) : static_cast<float>(parseint(s));
     125              : }
     126              : 
     127            2 : inline double parsenumber(const char *s)
     128              : {
     129              :     char *end;
     130            2 :     double val = std::strtod(s, &end);
     131              :     return val
     132            1 :         || end==s
     133            3 :         || (*end!='x' && *end!='X') ? static_cast<double>(val) : static_cast<double>(parseint(s));
     134              : }
     135              : 
     136           68 : inline void intformat(char *buf, int v, int len = 20)
     137              : {
     138           68 :     nformatstring(buf, len, "%d", v);
     139           68 : }
     140              : 
     141          116 : inline void floatformat(char *buf, float v, int len = 20)
     142              : {
     143          116 :     nformatstring(buf, len, v==static_cast<int>(v) ? "%.1f" : "%.7g", v);
     144          116 : }
     145              : 
     146              : extern const char *intstr(int v);
     147              : 
     148          354 : inline const char *getstr(const identval &v, int type)
     149              : {
     150          354 :     switch(type)
     151              :     {
     152          247 :         case Value_String:
     153              :         case Value_Macro:
     154              :         case Value_CString:
     155              :         {
     156          247 :             return v.s;
     157              :         }
     158           60 :         case Value_Integer:
     159              :         {
     160           60 :             return intstr(v.i);
     161              :         }
     162            0 :         case Value_Float:
     163              :         {
     164            0 :             return floatstr(v.f);
     165              :         }
     166           47 :         default:
     167              :         {
     168           47 :             return "";
     169              :         }
     170              :     }
     171              : }
     172              : 
     173          247 : inline const char *tagval::getstr() const
     174              : {
     175          247 :     return ::getstr(*this, type);
     176              : }
     177              : 
     178          107 : inline const char *ident::getstr() const
     179              : {
     180          107 :     return ::getstr(alias.val, valtype);
     181              : }
     182              : 
     183            0 : inline void getval(const identval &v, int type, tagval &r)
     184              : {
     185            0 :     switch(type)
     186              :     {
     187            0 :         case Value_String:
     188              :         case Value_Macro:
     189              :         case Value_CString:
     190              :         {
     191            0 :             r.setstr(newstring(v.s));
     192            0 :             break;
     193              :         }
     194            0 :         case Value_Integer:
     195              :         {
     196            0 :             r.setint(v.i);
     197            0 :             break;
     198              :         }
     199            0 :         case Value_Float:
     200              :         {
     201            0 :             r.setfloat(v.f);
     202            0 :             break;
     203              :         }
     204            0 :         default:
     205              :         {
     206            0 :             r.setnull();
     207            0 :             break;
     208              :         }
     209              :     }
     210            0 : }
     211              : 
     212            0 : inline void tagval::getval(tagval &r) const
     213              : {
     214            0 :     ::getval(*this, type, r);
     215            0 : }
     216              : 
     217              : struct NullVal final : tagval
     218              : {
     219           29 :     NullVal()
     220           29 :     {
     221           29 :         setnull();
     222           29 :     }
     223              : };
     224              : 
     225              : struct IdentLink final
     226              : {
     227              :     ident *id;
     228              :     IdentLink *next;
     229              :     int usedargs;
     230              :     identstack *argstack;
     231              : };
     232              : 
     233              : extern const char *sourcefile,
     234              :                   *sourcestr;
     235              : 
     236              : extern std::array<std::vector<char>, 4> strbuf;
     237              : extern int stridx;
     238              : 
     239              : extern tagval *commandret;
     240              : extern void executeret(const uint *code, tagval &result = *commandret);
     241              : 
     242              : /**
     243              :  * @brief Executes a given string, returning a tagval by reference parameter
     244              :  *
     245              :  * @param p a string to execute
     246              :  * @param result tagval containing result metadata
     247              :  */
     248              : extern void executeret(const char *p, tagval &result = *commandret);
     249              : 
     250              : /**
     251              :  * @brief Executes a given ident, returning a tagval by reference parameter
     252              :  *
     253              :  * @param id the ident to execute
     254              :  * @param args an array of arguments
     255              :  * @param numargs size of args array
     256              :  * @param lookup whether to lookup (dereference) args (?)
     257              :  * @param result tagval containing result metadata
     258              :  */
     259              : extern void executeret(ident *id, tagval *args, int numargs, bool lookup = false, tagval &result = *commandret);
     260              : 
     261              : extern void poparg(ident &id);
     262              : extern void pusharg(ident &id, const tagval &v, identstack &stack);
     263              : extern bool getbool(const tagval &v);
     264              : extern void cleancode(ident &id);
     265              : extern char *conc(const tagval *v, int n, bool space);
     266              : extern char *conc(const tagval *v, int n, bool space, const char *prefix);
     267              : extern void freearg(tagval &v);
     268              : extern int unescapestring(char *dst, const char *src, const char *end);
     269              : extern const char *parsestring(const char *p);
     270              : extern void setarg(ident &id, tagval &v);
     271              : extern void setalias(ident &id, tagval &v);
     272              : extern void undoarg(ident &id, identstack &stack);
     273              : extern void redoarg(ident &id, const identstack &stack);
     274              : extern const char *parseword(const char *p);
     275              : 
     276              : extern bool validateblock(const char *s);
     277              : extern std::unordered_map<std::string, ident> idents;
     278              : 
     279              : extern void setvarchecked(ident *id, int val);
     280              : extern void setfvarchecked(ident *id, float val);
     281              : extern void setsvarchecked(ident *id, const char *val);
     282              : 
     283              : extern void printvar(const ident *id);
     284              : extern void printvar(const ident *id, int i);
     285              : 
     286              : extern void clearoverrides();
     287              : 
     288              : /**
     289              :  * @brief Drops sleep commands, optionally only dropping sleeps with override bit set.
     290              :  *
     291              :  * If clearoverrides is `false`, drops every element in the sleepcmds vector.
     292              :  *
     293              :  * If clearoverrides is `true`, keeps elements where Idf_Overridden bit is not set
     294              :  * and discards elements where Idf_Overridden bit is set.
     295              :  *
     296              :  * Idf_Overridden is an enum element in command.h.
     297              :  *
     298              :  * @param clearoverrides whether to drop elements with Idf_Overridden bit set.
     299              :  */
     300              : extern void clearsleep(bool clearoverrides = true);
     301              : 
     302              : extern char *executestr(ident *id, tagval *args, int numargs, bool lookup = false);
     303              : extern uint *compilecode(const char *p);
     304              : extern void freecode(uint *p);
     305              : extern int execute(ident *id, tagval *args, int numargs, bool lookup = false);
     306              : extern bool executebool(ident *id, tagval *args, int numargs, bool lookup = false);
     307              : extern void alias(const char *name, const char *action);
     308              : 
     309              : /**
     310              :  * @brief Converts a list of delimited strings into a vector of elements.
     311              :  *
     312              :  * List elements in the input string are added to the vector of strings passed
     313              :  * as elems.
     314              :  *
     315              :  * The list is parsed according to the CubeScript list formatting.
     316              :  * Tokens in the list are delimited by spaces, unless those tokens are themselves
     317              :  * delimited by [] "" or () characters.
     318              :  *
     319              :  * Existing elements in the elems vector will not be modified. The limit parameter
     320              :  * sets the maximum number of elements in the vector, regardless of whether they
     321              :  * were added by this function.
     322              :  *
     323              :  * Elements added to this vector are all heap-allocated raw character strings and
     324              :  * therefore must be delete[]'d after they are no longer being used
     325              :  *
     326              :  * @param s the list to explode
     327              :  * @param elems the vector to fill with elements
     328              :  * @param limit maximum size of the elems vector allowed
     329              :  */
     330              : extern void explodelist(const char *s, std::vector<char *> &elems, int limit = -1);
     331              : extern void explodelist(const char *s, std::vector<std::string> &elems, int limit = -1);
     332              : 
     333              : extern void result(tagval &v);
     334              : extern const char *numberstr(double v);
     335              : extern float clampfvar(std::string name, float val, float minval, float maxval);
     336              : 
     337              : #endif
        

Generated by: LCOV version 2.0-1