SpeedCrunch  0.11
/usr/src/RPM/BUILD/speedcrunch-0.11/src/math/floatio.h
Go to the documentation of this file.
00001 /* floatio.h: low level conversion, based on floatnum. */
00002 /*
00003     Copyright (C) 2007, 2008 Wolf Lammen.
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License , or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; see the file COPYING.  If not, write to:
00017 
00018       The Free Software Foundation, Inc.
00019       59 Temple Place, Suite 330
00020       Boston, MA 02111-1307 USA.
00021 
00022 
00023     You may contact the author by:
00024        e-mail:  ookami1 <at> gmx <dot> de
00025        mail:  Wolf Lammen
00026               Oertzweg 45
00027               22307 Hamburg
00028               Germany
00029 
00030 *************************************************************************/
00031 
00032 #include <core/errors.h>
00033 
00034 #ifndef FLOATIO_H
00035 # define FLOATIO_H
00036 
00037 #define NO_DIGIT 0x7F
00038 
00039 #define IO_BASE_ZERO       1
00040 #define IO_BASE_NAN        0
00041 #define IO_BASE_DEFAULT  (-1)
00042 
00043 #define IO_SIGN_PLUS         1
00044 #define IO_SIGN_NONE         0
00045 #define IO_SIGN_MINUS      (-1)
00046 #define IO_SIGN_COMPLEMENT (-2)
00047 
00048 #define IO_FLAG_SUPPRESS_PLUS       0x001
00049 #define IO_FLAG_SUPPRESS_BASETAG    0x002
00050 #define IO_FLAG_SUPPRESS_CMPL       0x004
00051 #define IO_FLAG_SUPPRESS_LDG_ZERO   0x008
00052 #define IO_FLAG_SUPPRESS_TRL_ZERO   0x010
00053 #define IO_FLAG_SUPPRESS_DOT        0x020
00054 #define IO_FLAG_SUPPRESS_EXPPLUS    0x040
00055 #define IO_FLAG_SUPPRESS_EXPBASE    0x080
00056 #define IO_FLAG_SUPPRESS_EXPZERO    0x100
00057 #define IO_FLAG_SHOW_BASE           0x200
00058 #define IO_FLAG_SHOW_EXPBASE        0x400
00059 
00060 #ifdef __cplusplus
00061 extern "C"{
00062 #endif
00063 
00064 typedef struct{
00065   int sz;
00066   char* buf;
00067 }t_buffer;
00068 typedef t_buffer* p_buffer;
00069 
00070 /* t_seq_desc describes the format of a sequence of digits.
00071   leadingSignDigits are the count of leading zeros (or for
00072   two's complement, F's, 7's or 1's), trailing0 are the number
00073   of zeros at the end of the sequence and digits are the total
00074   count of digits in the sequence. If a sequence contains only
00075   zeros, in some contexts, they are counted as sign digits,
00076   in others they are trailing zeros.
00077   base is the number base the sequence is coded in (one of 2, 8,
00078   10 or 16) and is reserved for callback. */
00079 typedef struct{
00080   int leadingSignDigits;
00081   int trailing0;
00082   int digits;
00083   int base;
00084   void* param;
00085 } t_seq_desc;
00086 typedef t_seq_desc* p_seq_desc;
00087 
00088 /* the number of digits not being a leading sign digit or a trailing zero */
00089 int _significantdigits(p_seq_desc n);
00090 
00091 /* sequences of digits can be encoded in various ways (ASCII, bc_num style,
00092    packed and so on). In order to access a single digit, a getter has to
00093    be supplied for each encoding. This is the common interface of these getters.
00094    ofs is the index of the digit in the sequence, the first (most significant)
00095    having an index 0. The getter should return a sign digit (mostly 0) for
00096    negative indices and 0 for indices greator or equal to the length of the
00097    sequence. Instances of t_getdigit usually access the param field of n to
00098    find the data structure where the digits are encoded in */
00099 typedef char (*t_getdigit)(int ofs, p_seq_desc param);
00100 
00101 /* list of tokens that are created in an output process.
00102    Instead of returning a single ASCII string, all parts
00103    of a number are kept in separate places, so a post-processor
00104    can reorder or beautify them */
00105 typedef struct{
00106   signed char sign;
00107   signed char base;
00108   t_buffer intpart;
00109   t_buffer fracpart;
00110   int exp;
00111 } t_otokens;
00112 typedef t_otokens* p_otokens;
00113 
00114 /* list of tokens that are sources in an input process.
00115    Instead of using a single ASCII string, all parts
00116    of a number are kept in separate places, stripped off all
00117    grammar related information. The tokens need not be 0
00118    terminated, as long as the token is delimited by something
00119    not mistaken as a part of it. */
00120 typedef struct{
00121   signed char sign;
00122   signed char base;
00123   const char* intpart;
00124   const char* fracpart;
00125   signed char expsign;
00126   unsigned exp;
00127   unsigned maxdigits;
00128 } t_itokens;
00129 typedef t_itokens* p_itokens;
00130 
00131 typedef struct{
00132   t_seq_desc seq;
00133   t_getdigit getdigit;
00134 } t_ext_seq_desc;
00135 typedef t_ext_seq_desc* p_ext_seq_desc;
00136 
00137 typedef struct{
00138   signed char sign;
00139   signed char base;
00140 } t_prefix;
00141 typedef t_prefix* p_prefix;
00142 
00143 typedef struct{
00144   t_prefix prefix;
00145   t_ext_seq_desc intpart;
00146   t_ext_seq_desc fracpart;
00147   int exp;
00148 } t_number_desc;
00149 typedef t_number_desc* p_number_desc;
00150 
00151 void _clearnumber(p_number_desc n);
00152 
00153 Error str2desc(p_number_desc n, p_itokens tokens);
00154 Error desc2str(p_otokens tokens, p_number_desc n, int scale);
00155 Error exp2str(p_buffer dest, int exp, char base);
00156 
00157 /*------------   additional stuff   ------------------*/
00158 
00159 /* t_ioparams is a data structure that contains all necessary information
00160    to convert an ASCII character encoded number into a t_token and vice versa.
00161    Most information is grammar related like dot, basetag and so on. Others
00162    like maxdigits describe general limits of floatnums. */
00163 typedef struct{
00164   signed char base;
00165   signed char expbase;
00166   char dot;
00167   char* basetag;
00168   char* expbegin;
00169   char* expend;
00170   char* cmpltag;
00171   unsigned maxdigits;
00172 } t_ioparams;
00173 typedef t_ioparams* p_ioparams;
00174 
00175 const char* basePrefix(char base);
00176 Error parse(p_itokens tokens, const char** buf);
00177 int cattokens(char* buf, int bufsz, p_otokens tokens,
00178               signed char expbase, unsigned flags);
00179 void float_stdconvert();
00180 char setioparams(p_ioparams params);
00181 char delioparams(signed char base);
00182 p_ioparams getioparams(signed char base);
00183 signed char setdefaultbase(signed char base);
00184 
00185 #ifdef __cplusplus
00186 }
00187 #endif
00188 
00189 #endif /* FLOATIO_H */