Révision 128

pobysoC-4.0/src/pobyso.h (revision 128)
19 19

  
20 20
#ifndef POBYSO_h
21 21

  
22
/* Typedefs to make code more readable. */
23
typedef sollya_obj_t pobyso_error_t;
24
typedef sollya_obj_t pobyso_function_t;
25
typedef sollya_obj_t pobyso_precision_t;
26
typedef sollya_obj_t pobyso_range_t;
27

  
28
typedef sollya_obj_t sollya_verbosity_t;
29

  
22 30
#define POBYSO_ABSOLUTE 1
23 31
#define POBYSO_RELATIVE 2
32

  
24 33
/* Mimic the default behavior of interactive Sollya. */
25 34
#define POBYSO_DEFAULT_POINTS 501
26 35
#define POBYSO_INF_NORM_NUM_POINTS (POBYSO_DEFAULT_POINTS)
27 36
#define POBYSO_GUESS_DEGREE_BOUND 1024
28 37

  
38

  
39
/* Very thin wrappers around a lot of Sollya functions.
40
 */
41
static inline pobyso_error_t pobyso_error(void)
42
{return(sollya_lib_error());}
43

  
44
static inline int pobyso_is_error(sollya_obj_t errorCandidate)
45
{return(sollya_lib_obj_is_error(errorCandidate));}
46

  
47
static inline int pobyso_is_function(sollya_obj_t functionCandidate)
48
{return(sollya_lib_obj_is_function(functionCandidate));}
49

  
29 50
/**
30 51
 * Print object(s) to stdout.
31 52
 * A very thin wrapper around the lib_sollya_v_autoprint() function.
......
50 71
 * function.
51 72
 * @return a Sollya expression if successes, a Sollya error if fails.
52 73
 */
53
sollya_obj_t
74
pobyso_error_t
75
pobyso_parse(const char* expression);
76

  
77
/**
78
 * A wrapper around the Sollya Remez function.
79
 */
80
/**
81
 * Parse a string to create a Sollya object.
82
 * A very thin wrapper around the sollya_lib_parse_string() function.
83
 * If the final ";" is forgotten in the expression, it is added by the
84
 * function.
85
 * @return a Sollya expression if successes, a Sollya error if fails.
86
 */
87
pobyso_error_t
54 88
pobyso_parse_string(const char* expression);
55 89

  
56 90
/**
91
 * A wrapper around the Sollya Remez function with the canonical monomials
92
 * base.
93
 */
94

  
95
pobyso_function_t
96
pobyso_remez_canonical_monomials_base(pobyso_function_t function,
97
                                      long int degree,
98
                                      pobyso_range_t interval,
99
                                      pobyso_function_t weight,
100
                                      double quality,
101
                                      pobyso_range_t bounds);
102

  
103
/**
104
 * A wrapper around the Sollya Remez function with the a sparse monomials
105
 * base.
106
 */
107

  
108
pobyso_function_t
109
pobyso_remez_sparse_monomials_base(pobyso_function_t);
110

  
111
/**
112
 * A wrapper around the Sollya Remez function with the canonical monomials
113
 * base.
114
 */
115

  
116
pobyso_function_t
117
pobyso_remez_arbitrary_base(pobyso_function_t);
118

  
119

  
120

  
121
/**
57 122
 * Set the verbosity mode off (level 0).
58
 * If currentVerbosity != NULL, currentVerbosity is set
59
 * to the current verbosity level. It may be used to reset the
60
 * verbosity level later.
123
 * The current level of vebositiy is returned.
61 124
 */
62
void
63
pobyso_set_verbosity_off(sollya_obj_t* currentVerbosityLevel);
125
sollya_verbosity_t
126
pobyso_set_verbosity_off(void);
64 127

  
65 128
/**
66 129
 * Set the verbosity level to newVerbosityLevel.
67 130
 * @param newVerbosityLevel must be a Sollay object corresponding to an
68 131
 *        integer constant.
69 132
 */
70
void
133
sollya_verbosity_t
71 134
pobyso_set_verbosity_to(sollya_obj_t newVerbosityLevel);
72 135

  
73 136
#if 0
pobysoC-4.0/src/test-pobyso-01.c (revision 128)
1
#include <stdio.h>
2
int main()
3
{
4
    printf("Hello World!\n");
5
    return (0);
6
}
7

  
0 8

  
pobysoC-4.0/src/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