summaryrefslogtreecommitdiff
path: root/src/util.c
blob: cfa7fc0c29e66d84da780068f894ce8875a72bca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 *  LiberTI - TI-like calculator designed for LibreCalc
 *  Copyright (C) 2016-2017 Delwink, LLC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, version 3 only.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdarg.h>
#include <string.h>

#include "tibchar.h"
#include "util.h"

int
load_expr (struct tib_expr *dest, const char *src)
{
  unsigned int len = strlen (src);
  for (unsigned int i = 0; i < len; ++i)
    {
      int rc = tib_expr_push (dest, src[i]);
      if (rc)
        return rc;
    }

  return 0;
}

int
load_expr_num (struct tib_expr *dest, const char *src)
{
  int rc = load_expr (dest, src);
  if (rc)
    return rc;

  int e = tib_expr_indexof_r (dest, 'e');
  if (e >= 0)
    {
      tib_expr_delete (dest, e);

      if ('+' == dest->data[e])
        dest->data[e] = TIB_CHAR_EPOW10;
      else
        rc = tib_expr_insert (dest, e, TIB_CHAR_EPOW10);
    }

  return rc;
}

const char *
display_special_char (int c)
{
  static const int SKIPS[] =
    {
      TIB_CHAR_DEGREE,
      TIB_CHAR_EPOW10,
      TIB_CHAR_GREATEREQUAL,
      TIB_CHAR_L1,
      TIB_CHAR_L2,
      TIB_CHAR_L3,
      TIB_CHAR_L4,
      TIB_CHAR_L5,
      TIB_CHAR_L6,
      TIB_CHAR_L7,
      TIB_CHAR_L8,
      TIB_CHAR_L9,
      TIB_CHAR_LESSEQUAL,
      TIB_CHAR_PI,
      TIB_CHAR_SMALL1,
      TIB_CHAR_SMALL2,
      TIB_CHAR_SMALL3,
      TIB_CHAR_SMALL4,
      TIB_CHAR_SMALL5,
      TIB_CHAR_SMALL6,
      TIB_CHAR_SMALL7,
      TIB_CHAR_SMALL8,
      TIB_CHAR_SMALL9,
      TIB_CHAR_SMALL_MINUS,
      TIB_CHAR_STO,
      TIB_CHAR_THETA
    };

  for (unsigned int i = 0; i < (sizeof SKIPS / sizeof (int)); ++i)
    if (SKIPS[i] == c)
      return NULL;

  return tib_special_char_text (c);
}

char *
get_expr_display_str (const struct tib_expr *expr)
{
  return tib_expr_tostr_f (expr, display_special_char);
}

int
max (int x, int y)
{
  return (x > y) ? x : y;
}

int
min (int x, int y)
{
  return (x < y) ? x : y;
}