Révision 128 pobysoC-4.0/src/pobyso.c

pobyso.c (revision 128)
17 17
/* includes of project headers */
18 18

  
19 19
/* includes of system headers */
20
#include <string.h>
21
#include <stdlib.h>
22
#include <stdio.h>
20 23

  
21 24
/* Other declarations */
22 25

  
......
39 42
  va_end(va);
40 43
} /* End pobyso_autoprint. */
41 44

  
45
/* @see pobyso.h#pobyso_parse*/
46
pobyso_function_t
47
pobyso_parse(const char* expression)
48
{
49
  int expressionLength, i;
50
  char *expressionWithSemiCo;
51
  sollya_obj_t expressionStringSa;
52
  sollya_obj_t expressionSa;
53

  
54
  /* Arguments check. */
55
  if (expression == NULL)
56
  {
57
    pobyso_error_message("pobyso_parse",
58
                        "NULL_POINTER_ARGUMENT",
59
                        "The expression is a NULL pointer");
60
    return(sollya_lib_error());
61
  }
62
  expressionLength = strlen(expression);
63
  if (expressionLength == 0)
64
  {
65
    pobyso_error_message("pobyso_parse",
66
                        "EMPTY_STRING_ARGUMENT",
67
                        "The expression is an empty string");
68
    return(sollya_lib_error());
69
  }
70
  /* Search from the last char of the expression until, whichever happens
71
   * first:
72
   * a ";" is found;
73
   * a char  != ';' is found the the ";" is appended.
74
   * If the head of the string is reached before any of these two events happens
75
   * return an error.
76
   */
77
  for (i = expressionLength - 1 ; i >= 0 ; i--)
78
  {
79
    if (expression[i] == ';') /* Nothing special to do:
80
                                 try to parse the string*/
81
    {
82
      /* This ugly cast comes from a deficient definition in Sollya:
83
         sollya_lib_string does not take a const char * argument. */
84
      expressionStringSa = sollya_lib_string((char*)expression);
85
      expressionSa = sollya_lib_parse(expressionStringSa);
86
      sollya_lib_free(expressionStringSa);
87
      return(expressionSa);
88
    }
89
    else
90
    {
91
      if (expression[i] == ' ' || expression[i] == '\t') /* A blank,
92
                                                           just continue. */
93
      {
94
        continue;
95
      }
96
      else /* a character != ';' and from a blank: create the ';'ed string. */
97
      {
98
        /* Create a new string for the expression, add the ";" and
99
         * and call sollya_lib_parse_string. */
100
        expressionWithSemiCo = calloc(i + 3, sizeof(char));
101
        if (expressionWithSemiCo == NULL)
102
        {
103
          pobyso_error_message("pobyso_parse",
104
                                "MEMORY_ALLOCATION_ERROR",
105
                                "Could not allocate the expression string");
106
          return(sollya_lib_error());
107
        }
108
        strncpy(expressionWithSemiCo, expression, i + 1);
109
        expressionWithSemiCo[i + 1] = ';';
110
        expressionWithSemiCo[i + 2] = '\0';
111
        expressionStringSa = sollya_lib_string(expressionWithSemiCo);
112
        expressionSa = sollya_lib_parse(expressionStringSa);
113
        free(expressionWithSemiCo);
114
        sollya_lib_free(expressionStringSa);
115
        return(expressionSa);
116
      } /* End character != ';' and from a blank. */
117
    /* Create a new string for the expression, add the ";" and
118
     * and call sollya_lib_parse_string. */
119
    } /* End else. */
120
  } /* End for. */
121
  /* We get here, it is because we did not find any char == anything different
122
   * from ' ' or '\t': the string is empty.
123
   */
124
  pobyso_error_message("pobyso_parse_string",
125
                       "ONLY_BLANK_ARGUMENT",
126
                        "The expression is only made of blanks");
127
  return(sollya_lib_error());
128

  
129
} /* pobyso_parse_string */
130

  
42 131
/* @see pobyso.h#pobyso_parse_string*/
43
sollya_obj_t
132
pobyso_function_t
44 133
pobyso_parse_string(const char* expression)
45 134
{
46
  int expressionLength;
135
  int expressionLength, i;
47 136
  char *expressionWithSemiCo;
48 137
  sollya_obj_t expressionSa;
138

  
49 139
  /* Arguments check. */
50 140
  if (expression == NULL)
51 141
  {
......
60 150
    pobyso_error_message("pobyso_parse_string",
61 151
                        "EMPTY_STRING_ARGUMENT",
62 152
                        "The expression is an empty string");
153
    return(sollya_lib_error());
63 154
  }
64
  /* If the final ";" has been forgotten. */
65
  if (expression[expressionLength-1] != ';')
155
  /* Search from the last char of the expression until, whichever happens
156
   * first:
157
   * a ";" is found;
158
   * a char  != ';' is found the the ";" is appended.
159
   * If the head of the string is reached before any of these two events happens
160
   * return an error.
161
   */
162
  for (i = expressionLength - 1 ; i >= 0 ; i--)
66 163
  {
67
    expressionWithSemiCo = calloc(expressionLength + 2, sizeof(char));
68
    if (expressionWithSemiCo == NULL)
164
    if (expression[i] == ';') /* Nothing special to do:
165
                                 try to parse the string*/
69 166
    {
70
      pobyso_error_message("pobyso_parse_string",
71
                            "MEMORY_ALLOCATION_ERROR",
72
                            "Could not allocate the expression string");
73
      return(sollya_lib_error());
167
      expressionSa = sollya_lib_parse_string(expression);
168
      return(expressionSa);
74 169
    }
75
    expressionWithSemiCo = strcat(expressionWithSemiCo, expression);
76
    expressionWithSemiCo = strcat(expressionWithSemiCo, ";");
77
    expressionSa = sollya_lib_parse_string(expressionWithSemiCo);
78
    free(expressionWithSemiCo);
79
    return(expressionSa);
80
  } /* End ";" forgotten */
81
  else
82
  {
83
    return(sollya_lib_parse_string(expression));
84
  }
170
    else
171
    {
172
      if (expression[i] == ' ' || expression[i] == '\t') /* A blank,
173
                                                           just continue. */
174
      {
175
        continue;
176
      }
177
      else /* a character != ';' and from a blank: create the ';'ed string. */
178
      {
179
        /* Create a new string for the expression, add the ";" and
180
         * and call sollya_lib_parse_string. */
181
        expressionWithSemiCo = calloc(i + 3, sizeof(char));
182
        if (expressionWithSemiCo == NULL)
183
        {
184
          pobyso_error_message("pobyso_parse_string",
185
                                "MEMORY_ALLOCATION_ERROR",
186
                                "Could not allocate the expression string");
187
          return(sollya_lib_error());
188
        }
189
        strncpy(expressionWithSemiCo, expression, i + 1);
190
        expressionWithSemiCo[i + 1] = ';';
191
        expressionWithSemiCo[i + 2] = '\0';
192
        expressionSa = sollya_lib_parse_string(expressionWithSemiCo);
193
        free(expressionWithSemiCo);
194
        return(expressionSa);
195
      } /* End character != ';' and from a blank. */
196
    /* Create a new string for the expression, add the ";" and
197
     * and call sollya_lib_parse_string. */
198
    } /* End else. */
199
  } /* End for. */
200
  /* We get here, it is because we did not find any char == anything different
201
   * from ' ' or '\t': the string is empty.
202
   */
203
  pobyso_error_message("pobyso_parse_string",
204
                       "ONLY_BLANK_ARGUMENT",
205
                        "The expression is only made of blanks");
206
  return(sollya_lib_error());
207

  
85 208
} /* pobyso_parse_string */
86 209

  
87
void
88
pobyso_set_verbosity_off(sollya_obj_t* currentVerbosityLevel)
210
sollya_verbosity_t
211
pobyso_set_verbosity_off()
89 212
{
90 213
  sollya_obj_t verbosityLevelZero;
91
  if (currentVerbosityLevel != NULL)
92
  {
93
     *currentVerbosityLevel = sollya_lib_get_verbosity();
94
  }
214
  sollya_obj_t currentVerbosityLevel = NULL;
215

  
216
  currentVerbosityLevel = sollya_lib_get_verbosity();
95 217
  verbosityLevelZero = sollya_lib_constant_from_int(0);
96 218
  sollya_lib_set_verbosity(verbosityLevelZero);
97 219
  sollya_lib_clear_obj(verbosityLevelZero);
220
  return(currentVerbosityLevel);
98 221
} /* End of pobyso_set_verbosity_off. */
99 222

  
100
void
223
sollya_verbosity_t
101 224
pobyso_set_verbosity_to(sollya_obj_t newVerbosityLevel)
102 225
{
103 226
  if (newVerbosityLevel == NULL)
......
105 228
    pobyso_error_message("pobyso_set_verbosity_to",
106 229
                        "NULL_POINTER_ARGUMENT",
107 230
                        "The new verbosity level is a NULL pointer");
108
    return;
231
    return(sollya_lib_error());
109 232
  }
110 233
  sollya_lib_set_verbosity(newVerbosityLevel);
111 234
} /* End of pobyso_set_verbosity_to. */

Formats disponibles : Unified diff