📄 sltypes.c
字号:
/* Basic type operations for S-Lang *//* Copyright (c) 1992, 1996, 2001, 2002, 2003 John E. Davis * This file is part of the S-Lang library. * * You may distribute under the terms of either the GNU General Public * License or the Perl Artistic License. */#include "slinclud.h"#if SLANG_HAS_FLOAT# include <math.h>#endif/* #define SL_APP_WANTS_FOREACH */ /* for String_Type */#include "slang.h"#include "_slang.h"int SLpop_string (char **s) /*{{{*/{ char *sls; *s = NULL; if (-1 == SLang_pop_slstring (&sls)) return -1; if (NULL == (*s = SLmake_string (sls))) { SLang_free_slstring (sls); return -1; } SLang_free_slstring (sls); return 0;}/*}}}*/int SLang_pop_slstring (char **s) /*{{{*/{ return SLclass_pop_ptr_obj (SLANG_STRING_TYPE, (VOID_STAR *)s);}/*}}}*//* if *data != 0, string should be freed upon use. */int SLang_pop_string(char **s, int *data) /*{{{*/{ if (SLpop_string (s)) return -1; *data = 1; return 0;}/*}}}*/int _SLang_push_slstring (char *s){ if (0 == SLclass_push_ptr_obj (SLANG_STRING_TYPE, (VOID_STAR)s)) return 0; SLang_free_slstring (s); return -1;}int _SLpush_alloced_slstring (char *s, unsigned int len){ if (NULL == (s = _SLcreate_via_alloced_slstring (s, len))) return -1; return _SLang_push_slstring (s);}int SLang_push_string (char *t) /*{{{*/{ if (t == NULL) return SLang_push_null (); if (NULL == (t = SLang_create_slstring (t))) return -1; return _SLang_push_slstring (t);}/*}}}*/int _SLang_dup_and_push_slstring (char *s){ if (NULL == (s = _SLstring_dup_slstring (s))) return SLang_push_null (); return _SLang_push_slstring (s);}/* This function _always_ frees the malloced string */int SLang_push_malloced_string (char *c) /*{{{*/{ int ret; ret = SLang_push_string (c); SLfree (c); return ret;}/*}}}*/#if 0static int int_int_power (int a, int b){ int r, s; if (a == 0) return 0; if (b < 0) return 0; if (b == 0) return 1; s = 1; if (a < 0) { if ((b % 2) == 1) s = -1; a = -a; } /* FIXME: Priority=low * This needs optimized */ r = 1; while (b) { r = r * a; b--; } return r * s;}#endifstatic intstring_string_bin_op_result (int op, unsigned char a, unsigned char b, unsigned char *c){ (void) a; (void) b; switch (op) { default: return 0; case SLANG_PLUS: *c = SLANG_STRING_TYPE; break; case SLANG_GT: case SLANG_GE: case SLANG_LT: case SLANG_LE: case SLANG_EQ: case SLANG_NE: *c = SLANG_CHAR_TYPE; break; } return 1;}static intstring_string_bin_op (int op, unsigned char a_type, VOID_STAR ap, unsigned int na, unsigned char b_type, VOID_STAR bp, unsigned int nb, VOID_STAR cp){ char *ic; char **a, **b, **c; unsigned int n, n_max; unsigned int da, db; (void) a_type; (void) b_type; if (na == 1) da = 0; else da = 1; if (nb == 1) db = 0; else db = 1; if (na > nb) n_max = na; else n_max = nb; a = (char **) ap; b = (char **) bp; if ((op != SLANG_NE) && (op != SLANG_EQ)) for (n = 0; n < n_max; n++) { if ((*a == NULL) || (*b == NULL)) { SLang_verror (SL_VARIABLE_UNINITIALIZED, "String element[%u] not initialized for binary operation", n); return -1; } a += da; b += db; } a = (char **) ap; b = (char **) bp; ic = (char *) cp; c = NULL; switch (op) { case SLANG_DIVIDE: case SLANG_MINUS: default: return 0; case SLANG_PLUS: /* Concat */ c = (char **) cp; for (n = 0; n < n_max; n++) { if (NULL == (c[n] = SLang_concat_slstrings (*a, *b))) goto return_error; a += da; b += db; } break; case SLANG_NE: for (n = 0; n < n_max; n++) { if ((*a == NULL) || (*b == NULL)) ic [n] = (*a != *b); else ic [n] = (*a != *b) && (0 != strcmp (*a, *b)); a += da; b += db; } break; case SLANG_GT: for (n = 0; n < n_max; n++) { ic [n] = (strcmp (*a, *b) > 0); a += da; b += db; } break; case SLANG_GE: for (n = 0; n < n_max; n++) { ic [n] = (strcmp (*a, *b) >= 0); a += da; b += db; } break; case SLANG_LT: for (n = 0; n < n_max; n++) { ic [n] = (strcmp (*a, *b) < 0); a += da; b += db; } break; case SLANG_LE: for (n = 0; n < n_max; n++) { ic [n] = (strcmp (*a, *b) <= 0); a += da; b += db; } break; case SLANG_EQ: for (n = 0; n < n_max; n++) { if ((*a == NULL) || (*b == NULL)) ic[n] = (*a == *b); else ic [n] = (*a == *b) || (strcmp (*a, *b) == 0); a += da; b += db; } break; } return 1; return_error: if (c != NULL) { unsigned int nn; for (nn = 0; nn < n; nn++) { SLang_free_slstring (c[nn]); c[nn] = NULL; } for (nn = n; nn < n_max; nn++) c[nn] = NULL; } return -1;}static void string_destroy (unsigned char unused, VOID_STAR s){ (void) unused; SLang_free_slstring (*(char **) s);}static int string_push (unsigned char unused, VOID_STAR sptr){ (void) unused; return SLang_push_string (*(char **) sptr);}static int string_cmp (unsigned char unused, VOID_STAR ap, VOID_STAR bp, int *c){ char *a, *b; (void) unused; a = *(char **) ap; b = *(char **) bp; if (a != b) { if (a == NULL) *c = -1; else if (b == NULL) *c = 1; else *c = strcmp (a, b); return 0; } *c = 0; return 0;}static int string_to_int (unsigned char a_type, VOID_STAR ap, unsigned int na, unsigned char b_type, VOID_STAR bp){ char **s; unsigned int i; int *b; (void) a_type; (void) b_type; s = (char **) ap; b = (int *) bp; for (i = 0; i < na; i++) { if (s[i] == NULL) b[i] = 0; else b[i] = s[i][0]; } return 1;}static int string_acopy (SLtype unused, VOID_STAR src_sptr, VOID_STAR dest_sptr){ char *s; (void) unused; if (NULL == (s = SLang_create_slstring (*(char **)src_sptr))) return -1; *(char **)dest_sptr = s; return 0;}struct _SLang_Foreach_Context_Type{ char *string; unsigned int n;};static SLang_Foreach_Context_Type *string_foreach_open (unsigned char type, unsigned int num){ char *s; SLang_Foreach_Context_Type *c; (void) type; if (num != 0) { SLang_verror (SL_NOT_IMPLEMENTED, "'foreach using' form not supported by String_Type"); SLdo_pop_n (num + 1); return NULL; } if (-1 == SLang_pop_slstring (&s)) return NULL; c = (SLang_Foreach_Context_Type *)SLmalloc (sizeof (SLang_Foreach_Context_Type)); if (c == NULL) { SLang_free_slstring (s); return NULL; } memset ((char *) c, 0, sizeof (SLang_Foreach_Context_Type)); c->string = s; return c;}static void string_foreach_close (unsigned char type, SLang_Foreach_Context_Type *c){ (void) type; if (c == NULL) return; SLang_free_slstring (c->string); SLfree ((char *) c);}static int string_foreach (unsigned char type, SLang_Foreach_Context_Type *c){ char ch; (void) type; ch = c->string[c->n]; if (ch == 0) return 0; /* done */ c->n += 1; if (-1 == SLclass_push_int_obj (SLANG_INT_TYPE, ch)) return -1; return 1;}int _SLstring_list_push (_SLString_List_Type *p){ unsigned int num; int inum; SLang_Array_Type *at; char **buf; if ((buf = p->buf) == NULL) return SLang_push_null (); num = p->num; inum = (int) num; if (num == 0) num++; if (num != p->max_num) { if (NULL == (buf = (char **)SLrealloc ((char *) buf, sizeof (char *) * num))) { _SLstring_list_delete (p); return -1; } p->max_num = num; p->buf = buf; } if (NULL == (at = SLang_create_array (SLANG_STRING_TYPE, 0, (VOID_STAR) buf, &inum, 1))) { _SLstring_list_delete (p); return -1; } p->buf = NULL; _SLstring_list_delete (p); return SLang_push_array (at, 1);}int _SLstring_list_init (_SLString_List_Type *p, unsigned int max_num, unsigned int delta_num){ if (NULL == (p->buf = (char **) SLmalloc (max_num * sizeof (char *)))) return -1; p->max_num = max_num; p->num = 0; p->delta_num = delta_num; return 0;}int _SLstring_list_append (_SLString_List_Type *p, char *s){ if (s == NULL) { _SLstring_list_delete (p); return -1; } if (p->max_num == p->num) { char **b; unsigned int max_num = p->num + p->delta_num; b = (char **)SLrealloc ((char *)p->buf, max_num * sizeof (char *)); if (b == NULL) { _SLstring_list_delete (p); SLang_free_slstring (s); return -1; } p->buf = b; p->max_num = max_num; } p->buf[p->num] = s; p->num++; return 0;}void _SLstring_list_delete (_SLString_List_Type *p){ if (p->buf != NULL) { unsigned int i, imax; char **buf = p->buf; imax = p->num; for (i = 0; i < imax; i++) SLang_free_slstring (buf[i]); SLfree ((char *)buf); p->buf = NULL; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -