⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trio.c

📁 下载来的一个看图软件的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/************************************************************************* * * $Id: trio.c,v 1.1 2001/06/07 08:23:02 fjfranklin Exp $ * * Copyright (C) 1998 Bjorn Reese and Daniel Stenberg. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER. * ************************************************************************* * * A note to trio contributors: * * Avoid heap allocation at all costs to ensure that the trio functions * are async-safe. The exceptions are the printf/fprintf functions, which * uses fputc, and the asprintf functions and the <alloc> modifier, which * by design are required to allocate form the heap. * ************************************************************************//* * TODO: *  - Scan is probably too permissive about its modifiers. *  - Add hex-float to TrioReadDouble. *  - C escapes in %#[] ? *  - C99 support has not been properly tested. *  - Multibyte characters (done for format parsing, except scan groups) *  - Complex numbers? (C99 _Complex) *  - Boolean values? (C99 _Bool) *  - C99 NaN(n-char-sequence) missing *  - Should we support the GNU %a alloc modifier? GNU has an ugly hack *    for %a, because C99 used %a for other purposes. If specified as *    %as or %a[ it is interpreted as the alloc modifier, otherwise as *    the C99 hex-float. This means that you cannot scan %as as a hex-float *    immediately followed by an 's'. */static const char rcsid[] = "@(#)$Id: trio.c,v 1.1 2001/06/07 08:23:02 fjfranklin Exp $";#if defined(unix) || defined(__xlC__) /* AIX xlC workaround */# define PLATFORM_UNIX#elif defined(AMIGA) && defined(__GNUC__)# define PLATFORM_UNIX#endif/************************************************************************* * Include files */#include "trio.h"#include "triop.h"#include "strio.h"#if !defined(DEBUG) && !defined(NDEBUG)# define NDEBUG#endif#include <assert.h>#include <ctype.h>#include <math.h>#include <limits.h>#include <float.h>#include <stdarg.h>#include <errno.h>#if defined(TRIO_C99)# include <stdint.h>#endif#if defined(PLATFORM_UNIX)# include <unistd.h># include <locale.h># define USE_LOCALE#endif#if defined(_MSC_VER)#include <io.h>#define read _read#define write _write#endif /* _MSC_VER *//************************************************************************* * Generic definitions */#ifndef NULL# define NULL 0#endif#define NIL ((char)0)#ifdef __cplusplus# undef TRUE# undef FALSE# define TRUE true# define FALSE false# define BOOLEAN_T bool#else# ifndef FALSE#  define FALSE (1 == 0)#  define TRUE (! FALSE)# endif# define BOOLEAN_T int#endif/* mincore() can be used for debugging purposes */#define VALID(x) (NULL != (x))/* * Encode the error code and the position. This is decoded * with TRIO_ERROR_CODE and TRIO_ERROR_POSITION. */#if defined(TRIO_ERRORS)# define TRIO_ERROR_RETURN(x,y) (- ((x) + ((y) << 8)))#else# define TRIO_ERROR_RETURN(x,y) (-1)#endif/************************************************************************* * Internal definitions */#if defined(__STDC_ISO_10646__) || defined(MB_LEN_MAX)# define USE_MULTIBYTE#endif#if !defined(USE_LONGLONG)# if defined(__GNUC__) && !defined(__STRICT_ANSI__)#  define USE_LONGLONG# elif defined(__SUNPRO_C)#  define USE_LONGLONG# elif defined(_LONG_LONG) || defined(_LONGLONG)#  define USE_LONGLONG# endif#endif/* The extra long numbers */#if defined(USE_LONGLONG)# define LONGLONG long long# define ULONGLONG unsigned long long#else# define LONGLONG long# define ULONGLONG unsigned long#endif/* The longest possible integer */#if defined(TRIO_C99)# define LONGEST uintmax_t# define SLONGEST intmax_t#else# define LONGEST ULONGLONG# define SLONGEST LONGLONG#endif/* The maximal number of digits are for base 2 */#define MAX_CHARS_IN(x) (sizeof(x) * CHAR_BIT + 1)/* The width of a pointer. The number of bits in a hex digit is 4 */#define POINTER_WIDTH ((sizeof("0x") - 1) + sizeof(void *) * CHAR_BIT / 4)/* Infinite and Not-A-Number for floating-point */#define USE_NON_NUMBERS#ifndef NAN# define NAN (cos(HUGE_VAL))#endif#define INFINITE_LOWER "inf"#define INFINITE_UPPER "INF"#define LONG_INFINITE_LOWER "infinite"#define LONG_INFINITE_UPPER "INFINITE"#define NAN_LOWER "nan"#define NAN_UPPER "NAN"/* Various constants */enum {  TYPE_PRINT = 1,  TYPE_SCAN  = 2,  /* Flags. Use maximum 32 */  FLAGS_NEW                 = 0,  FLAGS_STICKY              = 1,  FLAGS_SPACE               = 2 * FLAGS_STICKY,  FLAGS_SHOWSIGN            = 2 * FLAGS_SPACE,  FLAGS_LEFTADJUST          = 2 * FLAGS_SHOWSIGN,  FLAGS_ALTERNATIVE         = 2 * FLAGS_LEFTADJUST,  FLAGS_SHORT               = 2 * FLAGS_ALTERNATIVE,  FLAGS_SHORTSHORT          = 2 * FLAGS_SHORT,  FLAGS_LONG                = 2 * FLAGS_SHORTSHORT,  FLAGS_QUAD                = 2 * FLAGS_LONG,  FLAGS_LONGDOUBLE          = 2 * FLAGS_QUAD,  FLAGS_SIZE_T              = 2 * FLAGS_LONGDOUBLE,  FLAGS_PTRDIFF_T           = 2 * FLAGS_SIZE_T,  FLAGS_INTMAX_T            = 2 * FLAGS_PTRDIFF_T,  FLAGS_NILPADDING          = 2 * FLAGS_INTMAX_T,  FLAGS_UNSIGNED            = 2 * FLAGS_NILPADDING,  FLAGS_UPPER               = 2 * FLAGS_UNSIGNED,  FLAGS_WIDTH               = 2 * FLAGS_UPPER,  FLAGS_WIDTH_PARAMETER     = 2 * FLAGS_WIDTH,  FLAGS_PRECISION           = 2 * FLAGS_WIDTH_PARAMETER,  FLAGS_PRECISION_PARAMETER = 2 * FLAGS_PRECISION,  FLAGS_BASE                = 2 * FLAGS_PRECISION_PARAMETER,  FLAGS_BASE_PARAMETER      = 2 * FLAGS_BASE,  FLAGS_FLOAT_E             = 2 * FLAGS_BASE_PARAMETER,  FLAGS_FLOAT_G             = 2 * FLAGS_FLOAT_E,  FLAGS_QUOTE               = 2 * FLAGS_FLOAT_G,  FLAGS_WIDECHAR            = 2 * FLAGS_QUOTE,  FLAGS_ALLOC               = 2 * FLAGS_WIDECHAR,  FLAGS_IGNORE              = 2 * FLAGS_ALLOC,  FLAGS_IGNORE_PARAMETER    = 2 * FLAGS_IGNORE,  FLAGS_SIZE_PARAMETER      = 2 * FLAGS_IGNORE_PARAMETER,  /* Reused flags */  FLAGS_EXCLUDE             = FLAGS_SHORT,  FLAGS_USER_DEFINED        = FLAGS_IGNORE,  /* Compounded flags */  FLAGS_ALL_VARSIZES        = FLAGS_LONG | FLAGS_QUAD | FLAGS_INTMAX_T | FLAGS_PTRDIFF_T | FLAGS_SIZE_T,  NO_POSITION  = -1,  NO_WIDTH     =  0,  NO_PRECISION = -1,  NO_SIZE      = -1,  NO_BASE      = -1,  MIN_BASE     =  2,  MAX_BASE     = 36,  BASE_BINARY  =  2,  BASE_OCTAL   =  8,  BASE_DECIMAL = 10,  BASE_HEX     = 16,  /* Maximal number of allowed parameters */  MAX_PARAMETERS = 64,  /* Maximal number of characters in class */  MAX_CHARACTER_CLASS = UCHAR_MAX,  /* Maximal string lengths for user-defined specifiers */  MAX_USER_NAME = 64,  MAX_USER_DATA = 256,    /* Maximal length of locale separator strings */  MAX_LOCALE_SEPARATOR_LENGTH = 64,  /* Maximal number of integers in grouping */  MAX_LOCALE_GROUPS = 64};#define NO_GROUPING ((int)CHAR_MAX)/* Fundamental formatting parameter types */#define FORMAT_UNKNOWN   0#define FORMAT_INT       1#define FORMAT_DOUBLE    2#define FORMAT_CHAR      3#define FORMAT_STRING    4#define FORMAT_POINTER   5#define FORMAT_COUNT     6#define FORMAT_PARAMETER 7#define FORMAT_GROUP     8#if defined(TRIO_GNU)# define FORMAT_ERRNO    9#endif#if defined(TRIO_EXTENSION)# define FORMAT_USER_DEFINED 10#endif/* Character constants */#define CHAR_IDENTIFIER '%'#define CHAR_BACKSLASH '\\'#define CHAR_QUOTE '\"'#define CHAR_ADJUST ' '/* Character class expressions */#define CLASS_ALNUM ":alnum:"#define CLASS_ALPHA ":alpha:"#define CLASS_CNTRL ":cntrl:"#define CLASS_DIGIT ":digit:"#define CLASS_GRAPH ":graph:"#define CLASS_LOWER ":lower:"#define CLASS_PRINT ":print:"#define CLASS_PUNCT ":punct:"#define CLASS_SPACE ":space:"#define CLASS_UPPER ":upper:"#define CLASS_XDIGIT ":xdigit:"/* * SPECIFIERS: * * * a  Hex-float * A  Hex-float * c  Character * C  Widechar character (wint_t) * d  Decimal * e  Float * E  Float * F  Float * F  Float * g  Float * G  Float * i  Integer * m  Error message * n  Count * o  Octal * p  Pointer * s  String * S  Widechar string (wchar_t) * u  Unsigned * x  Hex * X  Hex * [] Group * <> User-defined * * Reserved: * * D  Binary Coded Decimal %D(length,precision) (OS/390) */#define SPECIFIER_CHAR 'c'#define SPECIFIER_STRING 's'#define SPECIFIER_DECIMAL 'd'#define SPECIFIER_INTEGER 'i'#define SPECIFIER_UNSIGNED 'u'#define SPECIFIER_OCTAL 'o'#define SPECIFIER_HEX 'x'#define SPECIFIER_HEX_UPPER 'X'#define SPECIFIER_FLOAT_E 'e'#define SPECIFIER_FLOAT_E_UPPER 'E'#define SPECIFIER_FLOAT_F 'f'#define SPECIFIER_FLOAT_F_UPPER 'F'#define SPECIFIER_FLOAT_G 'g'#define SPECIFIER_FLOAT_G_UPPER 'G'#define SPECIFIER_POINTER 'p'#define SPECIFIER_GROUP '['#define SPECIFIER_UNGROUP ']'#define SPECIFIER_COUNT 'n'#if defined(TRIO_UNIX98)# define SPECIFIER_CHAR_UPPER 'C'# define SPECIFIER_STRING_UPPER 'S'#endif#if defined(TRIO_C99)# define SPECIFIER_HEXFLOAT 'a'# define SPECIFIER_HEXFLOAT_UPPER 'A'#endif#if defined(TRIO_GNU)# define SPECIFIER_ERRNO 'm'#endif#if defined(TRIO_EXTENSION)# define SPECIFIER_BINARY 'b'# define SPECIFIER_BINARY_UPPER 'B'# define SPECIFIER_USER_DEFINED_BEGIN '<'# define SPECIFIER_USER_DEFINED_END '>'# define SPECIFIER_USER_DEFINED_SEPARATOR ':'#endif/* * QUALIFIERS: * * * Numbers = d,i,o,u,x,X * Float = a,A,e,E,f,F,g,G * String = s * Char = c * * * 9$ Position *      Use the 9th parameter. 9 can be any number between 1 and *      the maximal argument * * 9 Width *      Set width to 9. 9 can be any number, but must not be postfixed *      by '$' * * h  Short *    Numbers: *      (unsigned) short int * * hh Short short *    Numbers: *      (unsigned) char * * l  Long *    Numbers: *      (unsigned) long int *    String: *      as the S specifier *    Char: *      as the C specifier * * ll Long Long *    Numbers: *      (unsigned) long long int * * L  Long Double *    Float *      long double * * #  Alternative *    Float: *      Decimal-point is always present *    String: *      non-printable characters are handled as \number * *    Spacing * * +  Sign * * -  Alignment * * .  Precision * * *  Parameter *    print: use parameter *    scan: no parameter (ignore) * * q  Quad * * Z  size_t * * w  Widechar * * '  Thousands/quote *    Numbers: *      Integer part grouped in thousands *    Binary numbers: *      Number grouped in nibbles (4 bits) *    String: *      Quoted string * * j  intmax_t * t  prtdiff_t * z  size_t * * !  Sticky * @  Parameter (for both print and scan) */#define QUALIFIER_POSITION '$'#define QUALIFIER_SHORT 'h'#define QUALIFIER_LONG 'l'#define QUALIFIER_LONG_UPPER 'L'#define QUALIFIER_ALTERNATIVE '#'#define QUALIFIER_SPACE ' '#define QUALIFIER_PLUS '+'#define QUALIFIER_MINUS '-'#define QUALIFIER_DOT '.'#define QUALIFIER_STAR '*'#define QUALIFIER_CIRCUMFLEX '^'#if defined(TRIO_C99)# define QUALIFIER_SIZE_T 'z'# define QUALIFIER_PTRDIFF_T 't'# define QUALIFIER_INTMAX_T 'j'#endif#if defined(TRIO_BSD) || defined(TRIO_GNU)# define QUALIFIER_QUAD 'q'#endif#if defined(TRIO_GNU)# define QUALIFIER_SIZE_T_UPPER 'Z'#endif#if defined(TRIO_MISC)# define QUALIFIER_WIDECHAR 'w'#endif#if defined(TRIO_EXTENSION)# define QUALIFIER_QUOTE '\''# define QUALIFIER_STICKY '!'# define QUALIFIER_VARSIZE '&' /* This should remain undocumented */# define QUALIFIER_PARAM '@' /* Experimental */# define QUALIFIER_COLON ':' /* For scanlists */#endif/************************************************************************* * Internal structures *//* Parameters */typedef struct {  int type;  unsigned long flags;  int width;  int precision;  int base;  int varsize;  int indexAfterSpecifier;  union {    char *string;    void *pointer;    union {      SLONGEST as_signed;      LONGEST as_unsigned;    } number;    double doubleNumber;    double *doublePointer;    long double longdoubleNumber;    long double *longdoublePointer;    int errorNumber;  } data;  /* For the user-defined specifier */  char user_name[MAX_USER_NAME];  char user_data[MAX_USER_DATA];} parameter_T;/* General trio "class" */typedef struct _trio_T {  const void *location;  void (*OutStream)(struct _trio_T *, int);  void (*InStream)(struct _trio_T *, int *);  /*   * The number of characters that would have been written/read if   * there had been sufficient space.   */  int processed;  /*   * The number of characters that are actually written/read.   * Processed and committed with only differ for the *nprintf   * and *nscanf functions.   */  int committed;  int max;  int current;} trio_T;/* References (for user-defined callbacks) */typedef struct _reference_T {  trio_T *data;  parameter_T *parameter;} reference_T;/* Registered entries (for user-defined callbacks) */typedef struct _userdef_T {  struct _userdef_T *next;  trio_callback_t callback;  char *name;} userdef_T;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -