File indexing completed on 2024-05-12 17:22:35

0001 /* floatio.h: low level conversion, based on floatnum. */
0002 /*
0003     Copyright (C) 2007, 2008 Wolf Lammen.
0004 
0005     This program is free software; you can redistribute it and/or modify
0006     it under the terms of the GNU General Public License as published by
0007     the Free Software Foundation; either version 2 of the License , or
0008     (at your option) any later version.
0009 
0010     This program is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU General Public License for more details.
0014 
0015     You should have received a copy of the GNU General Public License
0016     along with this program; see the file COPYING.  If not, write to:
0017 
0018       The Free Software Foundation, Inc.
0019       59 Temple Place, Suite 330
0020       Boston, MA 02111-1307 USA.
0021 
0022 
0023     You may contact the author by:
0024        e-mail:  ookami1 <at> gmx <dot> de
0025        mail:  Wolf Lammen
0026               Oertzweg 45
0027               22307 Hamburg
0028               Germany
0029 
0030 *************************************************************************/
0031 
0032 #include "errors.h"
0033 
0034 #ifndef FLOATIO_H
0035 # define FLOATIO_H
0036 
0037 #define NO_DIGIT 0x7F
0038 
0039 #define IO_BASE_ZERO       1
0040 #define IO_BASE_NAN        0
0041 #define IO_BASE_DEFAULT  (-1)
0042 
0043 #define IO_SIGN_PLUS         1
0044 #define IO_SIGN_NONE         0
0045 #define IO_SIGN_MINUS      (-1)
0046 #define IO_SIGN_COMPLEMENT (-2)
0047 
0048 #define IO_FLAG_SUPPRESS_PLUS       0x001
0049 #define IO_FLAG_SUPPRESS_BASETAG    0x002
0050 #define IO_FLAG_SUPPRESS_CMPL       0x004
0051 #define IO_FLAG_SUPPRESS_LDG_ZERO   0x008
0052 #define IO_FLAG_SUPPRESS_TRL_ZERO   0x010
0053 #define IO_FLAG_SUPPRESS_DOT        0x020
0054 #define IO_FLAG_SUPPRESS_EXPPLUS    0x040
0055 #define IO_FLAG_SUPPRESS_EXPBASE    0x080
0056 #define IO_FLAG_SUPPRESS_EXPZERO    0x100
0057 #define IO_FLAG_SHOW_BASE           0x200
0058 #define IO_FLAG_SHOW_EXPBASE        0x400
0059 
0060 #ifdef __cplusplus
0061 extern "C"{
0062 #endif
0063 
0064 typedef struct{
0065   int sz;
0066   char* buf;
0067 }t_buffer;
0068 typedef t_buffer* p_buffer;
0069 
0070 /* t_seq_desc describes the format of a sequence of digits.
0071   leadingSignDigits are the count of leading zeros (or for
0072   two's complement, F's, 7's or 1's), trailing0 are the number
0073   of zeros at the end of the sequence and digits are the total
0074   count of digits in the sequence. If a sequence contains only
0075   zeros, in some contexts, they are counted as sign digits,
0076   in others they are trailing zeros.
0077   base is the number base the sequence is coded in (one of 2, 8,
0078   10 or 16) and is reserved for callback. */
0079 typedef struct{
0080   int leadingSignDigits;
0081   int trailing0;
0082   int digits;
0083   int base;
0084   void* param;
0085 } t_seq_desc;
0086 typedef t_seq_desc* p_seq_desc;
0087 
0088 /* the number of digits not being a leading sign digit or a trailing zero */
0089 int _significantdigits(p_seq_desc n);
0090 
0091 /* sequences of digits can be encoded in various ways (ASCII, bc_num style,
0092    packed and so on). In order to access a single digit, a getter has to
0093    be supplied for each encoding. This is the common interface of these getters.
0094    ofs is the index of the digit in the sequence, the first (most significant)
0095    having an index 0. The getter should return a sign digit (mostly 0) for
0096    negative indices and 0 for indices greator or equal to the length of the
0097    sequence. Instances of t_getdigit usually access the param field of n to
0098    find the data structure where the digits are encoded in */
0099 typedef char (*t_getdigit)(int ofs, p_seq_desc param);
0100 
0101 /* list of tokens that are created in an output process.
0102    Instead of returning a single ASCII string, all parts
0103    of a number are kept in separate places, so a post-processor
0104    can reorder or beautify them */
0105 typedef struct{
0106   signed char sign;
0107   signed char base;
0108   t_buffer intpart;
0109   t_buffer fracpart;
0110   int exp;
0111 } t_otokens;
0112 typedef t_otokens* p_otokens;
0113 
0114 /* list of tokens that are sources in an input process.
0115    Instead of using a single ASCII string, all parts
0116    of a number are kept in separate places, stripped off all
0117    grammar related information. The tokens need not be 0
0118    terminated, as long as the token is delimited by something
0119    not mistaken as a part of it. */
0120 typedef struct{
0121   signed char sign;
0122   signed char base;
0123   const char* intpart;
0124   const char* fracpart;
0125   signed char expsign;
0126   unsigned exp;
0127   unsigned maxdigits;
0128 } t_itokens;
0129 typedef t_itokens* p_itokens;
0130 
0131 typedef struct{
0132   t_seq_desc seq;
0133   t_getdigit getdigit;
0134 } t_ext_seq_desc;
0135 typedef t_ext_seq_desc* p_ext_seq_desc;
0136 
0137 typedef struct{
0138   signed char sign;
0139   signed char base;
0140 } t_prefix;
0141 typedef t_prefix* p_prefix;
0142 
0143 typedef struct{
0144   t_prefix prefix;
0145   t_ext_seq_desc intpart;
0146   t_ext_seq_desc fracpart;
0147   int exp;
0148 } t_number_desc;
0149 typedef t_number_desc* p_number_desc;
0150 
0151 void _clearnumber(p_number_desc n);
0152 
0153 Error str2desc(p_number_desc n, p_itokens tokens);
0154 Error desc2str(p_otokens tokens, p_number_desc n, int scale);
0155 Error exp2str(p_buffer dest, int exp, char base);
0156 
0157 /*------------   additional stuff   ------------------*/
0158 
0159 /* t_ioparams is a data structure that contains all necessary information
0160    to convert an ASCII character encoded number into a t_token and vice versa.
0161    Most information is grammar related like dot, basetag and so on. Others
0162    like maxdigits describe general limits of floatnums. */
0163 typedef struct{
0164   signed char base;
0165   signed char expbase;
0166   char dot;
0167   char* basetag;
0168   char* expbegin;
0169   char* expend;
0170   char* cmpltag;
0171   unsigned maxdigits;
0172 } t_ioparams;
0173 typedef t_ioparams* p_ioparams;
0174 
0175 const char* basePrefix(char base);
0176 Error parse(p_itokens tokens, const char** buf);
0177 int cattokens(char* buf, int bufsz, p_otokens tokens,
0178               signed char expbase, unsigned flags);
0179 void float_stdconvert();
0180 char setioparams(p_ioparams params);
0181 char delioparams(signed char base);
0182 p_ioparams getioparams(signed char base);
0183 signed char setdefaultbase(signed char base);
0184 
0185 #ifdef __cplusplus
0186 }
0187 #endif
0188 
0189 #endif /* FLOATIO_H */