Révision 136

pobysoC-4.0/src/pobyso.c (revision 136)
136 136
{
137 137
  int expressionLength, i;
138 138
  char *expressionWithSemiCo;
139
  sollya_obj_t expressionSa;
139
  sollya_obj_t expressionSo;
140 140

  
141 141
  /* Arguments check. */
142 142
  if (expression == NULL)
......
166 166
    if (expression[i] == ';') /* Nothing special to do:
167 167
                                 try to parse the string*/
168 168
    {
169
      expressionSa = sollya_lib_parse_string(expression);
170
      return expressionSa;
169
      expressionSo = sollya_lib_parse_string(expression);
170
      if (sollya_lib_obj_is_error(expressionSo))
171
      {
172
        sollya_lib_clear_obj(expressionSo);
173
        return NULL;
174
      }
175
      else
176
      {
177
        return expressionSo;
178
      }
171 179
    }
172 180
    else
173 181
    {
......
191 199
        strncpy(expressionWithSemiCo, expression, i+1);
192 200
        expressionWithSemiCo[i + 1] = ';';
193 201
        expressionWithSemiCo[i + 2] = '\0';
194
        expressionSa = sollya_lib_parse_string(expressionWithSemiCo);
202
        expressionSo = sollya_lib_parse_string(expressionWithSemiCo);
195 203
        free(expressionWithSemiCo);
196
        return expressionSa;
204
        if (sollya_lib_obj_is_error(expressionSo))
205
        {
206
          sollya_lib_clear_obj(expressionSo);
207
          return NULL;
208
        }
209
        else
210
        {
211
          return expressionSo;
212
        }
197 213
      } /* End character != ';' and from a blank. */
198 214
    /* Create a new string for the expression, add the ";" and
199 215
     * and call sollya_lib_parse_string. */
......
206 222
                       "ONLY_BLANK_ARGUMENT",
207 223
                        "The expression is only made of blanks");
208 224
  return NULL;
209

  
210 225
} /* pobyso_parse_string */
211 226

  
212 227
pobyso_func_exp_t
......
295 310
 * @see pobyso.h#pobyso_subpoly
296 311
 */
297 312
pobyso_func_exp_t
298
pobyso_subpoly(pobyso_func_exp_t polynomialSo, long expsNum,...)
313
pobyso_subpoly(pobyso_func_exp_t polynomialSo, long expsNum, long int* expsList)
299 314
{
300
  sollya_obj_t expsListSo = NULL;
301
  sollya_obj_t* expsList  = NULL;
302
  sollya_obj_t subpoly    = NULL;
315
  sollya_obj_t  expsListSo    = NULL;
316
  sollya_obj_t* expsList_pso  = NULL;
317
  sollya_obj_t subpoly        = NULL;
318
  int i, j;
303 319

  
304
  va_list vaExpsList;
305
  int currentExp;
306
  int i;
307

  
308 320
  /* Arguments check. */
309 321
  if (polynomialSo == NULL) return NULL;
310 322
  if (expsNum < 0) return NULL;
311 323
  if (expsNum == 0) return sollya_lib_copy_obj(polynomialSo);
312

  
313
  va_start(vaExpsList, expsNum);
314
  expsList = (sollya_obj_t*) malloc(expsNum * sizeof(sollya_obj_t));
315
  if (expsList == NULL)
324
  if (expsList == 0) return NULL;
325
  /* Create a list of Sollya constants. */
326
  expsList_pso = (sollya_obj_t*) malloc(expsNum * sizeof(sollya_obj_t));
327
  if (expsList_pso == NULL)
316 328
  {
317 329
    pobyso_error_message("pobyso_subpoly",
318 330
                          "MEMORY_ALLOCATION_ERROR",
319
                          "Could not allocate the expression string");
331
                          "Could not allocate the Sollya exponents list");
320 332
    return NULL;
321 333
  }
334
  /* Fill up the list. */
322 335
  for (i = 0 ; i < expsNum ; i++)
323 336
  {
324
    currentExp = va_arg(vaExpsList, long);
325
    expsList[i] = sollya_lib_constant_from_int64(currentExp);
337
    /* Abort if an exponent is negative. */
338
    if (expsList[i] < 0 )
339
    {
340
      for (j = 0 ; j < i ; j++)
341
      {
342
        sollya_lib_clear_obj(expsList_pso[j]);
343
      }
344
      free(expsList_pso);
345
      return NULL;
346
    }
347
    expsList_pso[i] = sollya_lib_constant_from_int64(expsList[i]);
326 348
  } /* End for */
327
  va_end(vaExpsList);
328
  expsListSo = sollya_lib_list(expsList, expsNum);
329
  if (expsList == NULL)
349
  expsListSo = sollya_lib_list(expsList_pso, expsNum);
350
  for (i = 0 ; i < expsNum ; i++)
330 351
  {
352
    sollya_lib_clear_obj(expsList_pso[i]);
353
  }
354
  free(expsList_pso);
355
  if (expsListSo == NULL)
356
  {
331 357
    pobyso_error_message("pobyso_subpoly",
332 358
                          "LIST_CREATIONERROR",
333 359
                          "Could not create the exponents list");
334
    for (i = 0 ; i < expsNum ; i++)
335
    {
336
      sollya_lib_clear_obj(expsList[i]);
337
    }
338
    free(expsList);
339 360
    return NULL;
340 361
  }
341 362
  subpoly = sollya_lib_subpoly(polynomialSo, expsListSo);
363
  pobyso_autoprint(expsListSo);
342 364
  sollya_lib_clear_obj(expsListSo);
343
  for (i = 0 ; i < expsNum ; i++)
344
  {
345
    sollya_lib_clear_obj(expsList[i]);
346
  }
347
  free(expsList);
348 365
  return subpoly;
349 366
} /* pobyso_subpoly. */
350 367

  
pobysoC-4.0/src/test-pobyso-subpoly.sh (revision 136)
1
#! /bin/sh
2
#
3
#
4
TEST_BIN=./test-pobyso-subpoly
5

  
6
echo
7

  
8
bla() {
9
  echo "$@"
10
  eval "$TEST_BIN $ARGUMENTS"
11
if [ $? -ne 0 ] ; then echo "Failed for $ARGUMENTS" ; exit 1 ; fi
12
echo
13
}
14
##
15
ARGUMENTS="\"4*x^2\" 2"
16
bla "Testing: $ARGUMENTS"
17

  
18
##
19
ARGUMENTS="\"4-2*x+4*x^2\" 0 2"
20
bla "Testing: $ARGUMENTS"
21

  
22
##
23
ARGUMENTS="\"4-2*x+4*x^2\" 0 -1 2"
24
bla "Testing: $ARGUMENTS"
25

  
26
##
27
echo "Tests terminated without error."
28
echo
0 29

  
pobysoC-4.0/src/Makefile (revision 136)
14 14

  
15 15
ALL_TARGETS = test-pobyso-01 test-pobyso-02 \
16 16
              test-pobyso-constant-expression \
17
              test-pobyso-new-monomial
17
              test-pobyso-new-monomial \
18
              test-pobyso-subpoly
18 19

  
19 20
SCRATCH_PRG := $(patsubst %.c,%,$(wildcard *.c))
20 21
SCRATCH_ALL := $(SCRATCH_PRG)
21 22

  
pobysoC-4.0/src/pobyso.h (revision 136)
26 26
typedef sollya_obj_t pobyso_on_off_t;
27 27
typedef sollya_obj_t pobyso_precision_t;
28 28
typedef sollya_obj_t pobyso_range_t;
29
typedef sollya_obj_t pobyso_sollya_verbosity_t;
30 29

  
31 30
#define POBYSO_ABSOLUTE (1)
32 31
#define POBYSO_RELATIVE (2)
......
52 51
{return(sollya_lib_obj_is_function(functionCandidate));}
53 52

  
54 53
/**
55
 * Print object(s) to stdout.
54
 * Print an object to stdout.
56 55
 * A very thin wrapper around the lib_sollya_autoprint() function.
57 56
 */
58 57
void
......
162 161
 */
163 162

  
164 163
pobyso_func_exp_t
165
pobyso_subpoly(pobyso_func_exp_t polynomial, long expsNum,...);
164
pobyso_subpoly(pobyso_func_exp_t polynomial, long expsNum, long* expsList);
166 165

  
167 166
#if 0
168 167
/**
pobysoC-4.0/src/test-pobyso-subpoly.c (revision 136)
1
/** @file test-pobyso-subpoly.c
2
 * Name & purpose
3
 *
4
 * @author
5
 * @date
6
 *
7
 * @details Verbosity manipulation are performed  to make sur  that verbosity
8
 *  suppression is correctly managed in pobyso_is_constant_expression()
9
 *  function.
10
 */
11
/******************************************************************************/
12
/* Headers, applying the "particular to general" convention.*/
13

  
14
#include "pobyso.h"
15

  
16
/* includes of local headers */
17

  
18
/* includes of project headers */
19

  
20
/* includes of system headers */
21
#include <stdlib.h>
22
/* Other declarations */
23

  
24
/* Internal prototypes */
25
long int
26
read_long_decimal(long int* value, char* string);
27
long int
28
read_unsigned_long_decimal(long int* value, char* string);
29

  
30
/* Types, constants and macros definitions */
31

  
32
/* Global variables */
33

  
34
/* Functions */
35
long int
36
read_long_decimal(long int* returnValue, char* string)
37
{
38
  char* endptr[1];
39
  long int convertedValue = 0;
40

  
41
  convertedValue = strtoul(string, endptr, 10);
42
  /* For a completely safe conversion *endptr must point to 0 value char. */
43
  if (**endptr != '\0')
44
  {
45
    return 1;
46
  }
47
  else
48
  {
49
    *returnValue = convertedValue;
50
    return 0;
51
  }
52
  return 0;
53
} /* End read_long_decimal. */
54

  
55
long int
56
read_unsigned_long_decimal(long int* returnValue, char* string)
57
{
58
  char* endptr[1];
59
  long int convertedValue = 0;
60

  
61
  /* Negative sign -> failure. */
62
  if (*string == '-')
63
  {
64
    return 1;
65
  }
66
  convertedValue = strtoul(string, endptr, 10);
67
  /* For a completely safe conversion *endptr must point to 0 value char. */
68
  if (**endptr != '\0')
69
  {
70
    return 1;
71
  }
72
  else
73
  {
74
    *returnValue = convertedValue;
75
    return 0;
76
  }
77
  return 0;
78
} /* End read_unsigned_long_decimal. */
79

  
80
int
81
main(int argc, char** argv)
82
{
83
  long int* expsArray   = NULL;
84
  int i;
85
  pobyso_func_exp_t polynomial;
86
  pobyso_func_exp_t subpoly;
87

  
88
  if (argc < 3)
89
  {
90
    fprintf(stderr,
91
            "Usage: %s polynomialAsString power1 [power2 [...[powern]..]\n",
92
            argv[0]);
93
    return 1;
94
  }
95

  
96
  pobyso_set_canonical_on();
97
  polynomial = pobyso_parse_string(argv[1]);
98
  if (polynomial == NULL)
99
  {
100
    fprintf(stderr,
101
            "%s: can't create the polynomial.\n",
102
            argv[0]);
103
    return 1;
104
  }
105
  pobyso_autoprint(polynomial);
106
  expsArray = (long int*) malloc((argc - 2) * sizeof(long int));
107
  if (expsArray == NULL)
108
  {
109
    fprintf(stderr,
110
            "%s: can't create the exponents array.\n",
111
            argv[0]);
112
    return 1;
113
  }
114
  for (i = 2 ; i < argc ; i++)
115
  {
116
    if (read_long_decimal(&(expsArray[i - 2]), argv[i]))
117
    {
118
      fprintf(stderr,
119
              "%s: can't read exponent \"%s\".\n",
120
              argv[0],
121
              argv[i]);
122
      return 1;
123
    }
124
  }
125
  subpoly = pobyso_subpoly(polynomial, argc - 2, expsArray);
126
  if (subpoly == NULL)
127
  {
128
    return 1;
129
  }
130
  else
131
  {
132
    pobyso_autoprint(subpoly);
133
    return 0;
134
  }
135
} /* End main */
0 136

  

Formats disponibles : Unified diff