00001 /* 00002 * Copyright (C) 2007 Jolien Creighton, Peter Shawhan 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with with program; see the file COPYING. If not, write to the 00016 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00017 * MA 02111-1307 USA 00018 */ 00019 00020 /********************************** <lalVerbatim file="StringInputHV"> 00021 Author: Creighton, T. D. 00022 $Id: StringInput.h,v 1.5 2007/09/21 19:24:56 jolien Exp $ 00023 **************************************************** </lalVerbatim> */ 00024 00025 /********************************************************** <lalLaTeX> 00026 00027 \section{Header \texttt{StringInput.h}} 00028 \label{s:StringInput.h} 00029 00030 Provides routines to parse \verb@CHARVector@s into other LAL 00031 datatypes. 00032 00033 \subsection*{Synopsis} 00034 \begin{verbatim} 00035 #include "StringInput.h" 00036 \end{verbatim} 00037 00038 \noindent This header provides prototypes for routines that construct 00039 LAL data structures using the data from a character string. As in 00040 standard C, a \emph{string} is a block of non-null bytes of arbitrary 00041 length, terminated by a null byte \verb@'\0'@, and referred to by a 00042 value of type \verb@CHAR *@ pointing to the first byte in the string. 00043 It is not to be confused with a \verb@CHARVector@, a LAL structure 00044 referring to a block of data of a specified length, which may or may 00045 not contain one or more instances of \verb@'\0'@. 00046 00047 In general, the routines under this header will have string inputs of 00048 type \verb@const CHAR *@ (in order to allow, for instance, string 00049 literals to be used as inputs), but will allocate \verb@CHARVector@ 00050 structures to store string outputs. Unless otherwise specified, these 00051 outputs are guaranteed to contain at least one \verb@'\0'@ character, 00052 so their \verb@data@ fields are valid strings. It is the 00053 responsibility of the calling routine to ensure that the string input 00054 contains a terminating \verb@'\0'@ within the memory segment pointed 00055 to by the \verb@CHAR *@ input, in order to avoid segmentation 00056 violation. 00057 00058 These routines are intended to work in conjunction with the functions 00059 in \verb@StreamInput.h@ to add LAL robustness to otherwise ad-hoc data 00060 input routines. However, the functions in \verb@StringInput.h@ are 00061 fully LAL-compliant and use only LAL types, so they are included in 00062 \verb@liblal@ proper. 00063 00064 ******************************************************* </lalLaTeX> */ 00065 00066 #ifndef _STRINGINPUT_H 00067 #define _STRINGINPUT_H 00068 00069 #include <lal/LALStdlib.h> 00070 #include <lal/LALStdio.h> 00071 00072 #ifdef __cplusplus 00073 extern "C" { 00074 #pragma } 00075 #endif 00076 00077 NRCSID( STRINGINPUTH, "$Id: StringInput.h,v 1.5 2007/09/21 19:24:56 jolien Exp $"); 00078 00079 /********************************************************** <lalLaTeX> 00080 \subsection*{Error conditions} 00081 ****************************************** </lalLaTeX><lalErrTable> */ 00082 #define STRINGINPUTH_ENUL 1 00083 #define STRINGINPUTH_EOUT 2 00084 #define STRINGINPUTH_EMEM 3 00085 00086 #define STRINGINPUTH_MSGENUL "Unexpected null pointer in arguments" 00087 #define STRINGINPUTH_MSGEOUT "Output handle points to a non-null pointer" 00088 #define STRINGINPUTH_MSGEMEM "Memory allocation error" 00089 /******************************************** </lalErrTable><lalLaTeX> 00090 00091 \subsection*{Constants} 00092 \idx[Constant]{LAL\_INT2\_FORMAT} 00093 \idx[Constant]{LAL\_INT4\_FORMAT} 00094 \idx[Constant]{LAL\_INT8\_FORMAT} 00095 \idx[Constant]{LAL\_UINT2\_FORMAT} 00096 \idx[Constant]{LAL\_UINT4\_FORMAT} 00097 \idx[Constant]{LAL\_UINT8\_FORMAT} 00098 \idx[Constant]{LAL\_REAL4\_FORMAT} 00099 \idx[Constant]{LAL\_REAL8\_FORMAT} 00100 00101 The following constants are format strings that can be used by the 00102 various C \verb@scanf()@ or \verb@printf()@ functions to parse or 00103 write sequences of characters corresponding to base LAL datatypes. 00104 Since the C datatypes (\verb@short@, \verb@int@, \verb@long@, 00105 \verb@long long@, \verb@float@, \verb@double@, etc.) do not have fixed 00106 mappings to LAL base datatypes (\verb@INT2@, \verb@INT4@, \verb@INT8@, 00107 \verb@REAL4@, \verb@REAL8@, etc.), the appropriate format strings for 00108 each LAL datatype must be determined at configuration time and set at 00109 compile time. 00110 00111 These format strings give only the conversion character preceded by 00112 any length modifier according to the type (\verb@short@, \verb@long@, 00113 etc.). In particular they do \emph{not} contain the initial 00114 \verb@'%'@ character that initiates the conversion specification. 00115 However, being \verb@#define@d string literals, they can be combined 00116 with \verb@"%"@ string literals or more complicated format strings 00117 through implicit concatenation. Thus to scan \verb@string@ for a 00118 \verb@UINT4@ number \verb@n@ one would write: 00119 \begin{verbatim} 00120 sscanf( string, "%" LAL_UINT4_FORMAT, &n ); 00121 \end{verbatim} 00122 Similarly, to print a \verb@REAL8@ number \verb@x@ with 12 digits 00123 following the decimal place, one could use the following: 00124 \begin{verbatim} 00125 printf( "%.12" LAL_REAL8_FORMAT, x ); 00126 \end{verbatim} 00127 Of course, floating-point numbers are more commonly printed using the 00128 \verb@"%e"@ conversion specifier, which does not generally require 00129 type-dependent length modifiers. 00130 00131 \begin{center} 00132 \begin{tabular}{|ll|} 00133 \hline 00134 Name & Usual value \\ 00135 \hline 00136 \tt LAL\_INT2\_FORMAT & {\tt "hd"} \\ 00137 \tt LAL\_INT4\_FORMAT & {\tt "d"} or {\tt "ld"} \\ 00138 \tt LAL\_INT8\_FORMAT & {\tt "ld"} or {\tt "lld"} \\ 00139 \tt LAL\_UINT2\_FORMAT & {\tt "hu"} \\ 00140 \tt LAL\_UINT4\_FORMAT & {\tt "u"} or {\tt "lu"} \\ 00141 \tt LAL\_UINT8\_FORMAT & {\tt "lu"} or {\tt "llu"} \\ 00142 \tt LAL\_REAL4\_FORMAT & {\tt "f"} \\ 00143 \tt LAL\_REAL8\_FORMAT & {\tt "lf"} \\ 00144 \hline 00145 \end{tabular} 00146 \end{center} 00147 ******************************************************* </lalLaTeX> */ 00148 00149 /********************************************************** <lalLaTeX> 00150 00151 \subsection*{Types} 00152 00153 \subsubsection*{Structure \texttt{TokenList}} 00154 \idx[Type]{TokenList} 00155 00156 This structure stores a number of null-terminated strings of arbitrary 00157 length. The entire list is stored flattened in a \verb@CHARVector@, 00158 and individual tokens are pointed to by a \verb@CHAR *[]@ handle. The 00159 fields are: 00160 00161 \begin{description} 00162 \item[\texttt{UINT4 nTokens}] The number of tokens in the list. 00163 00164 \item[\texttt{CHAR **tokens}] A list of pointers to the individual 00165 tokens. The elements \verb@tokens[0..nTokens-1]@ point to tokens, and 00166 the element \verb@tokens[nTokens]@ is explicitly \verb@NULL@ (as is 00167 the convention for an \verb@argv@ argument list). 00168 00169 \item[\texttt{CHARVector *list}] The flattened list of tokens, 00170 separated by (and terminated with) \verb@'\0'@ characters. 00171 \end{description} 00172 00173 ******************************************************* </lalLaTeX> */ 00174 00175 typedef struct tagTokenList { 00176 UINT4 nTokens; /* number of tokens */ 00177 CHAR **tokens; /* list of pointers to tokens */ 00178 CHARVector *list; /* flattened list of null-terminated tokens */ 00179 } TokenList; 00180 00181 /* <lalLaTeX> 00182 \vfill{\footnotesize\input{StringInputHV}} 00183 </lalLaTeX> */ 00184 00185 /* Function prototypes. */ 00186 00187 /* <lalLaTeX> 00188 \newpage\input{StringTokenC} 00189 </lalLaTeX> */ 00190 void 00191 LALCreateTokenList( LALStatus *status, 00192 TokenList **list, 00193 const CHAR *string, 00194 const CHAR *delimiters ); 00195 00196 void 00197 LALDestroyTokenList( LALStatus *status, 00198 TokenList **list ); 00199 00200 /* <lalLaTeX> 00201 \newpage\input{StringConvertC} 00202 </lalLaTeX> */ 00203 void 00204 LALStringToU2( LALStatus *status, UINT2 *value, const CHAR *string, CHAR **endptr ); 00205 00206 void 00207 LALStringToU4( LALStatus *status, UINT4 *value, const CHAR *string, CHAR **endptr ); 00208 00209 void 00210 LALStringToU8( LALStatus *status, UINT8 *value, const CHAR *string, CHAR **endptr ); 00211 00212 void 00213 LALStringToI2( LALStatus *status, INT2 *value, const CHAR *string, CHAR **endptr ); 00214 00215 void 00216 LALStringToI4( LALStatus *status, INT4 *value, const CHAR *string, CHAR **endptr ); 00217 00218 void 00219 LALStringToI8( LALStatus *status, INT8 *value, const CHAR *string, CHAR **endptr ); 00220 00221 void 00222 LALStringToS( LALStatus *status, REAL4 *value, const CHAR *string, CHAR **endptr ); 00223 00224 void 00225 LALStringToD( LALStatus *status, REAL8 *value, const CHAR *string, CHAR **endptr ); 00226 00227 void 00228 LALStringToC( LALStatus *status, COMPLEX8 *value, const CHAR *string, CHAR **endptr ); 00229 00230 void 00231 LALStringToZ( LALStatus *status, COMPLEX16 *value, const CHAR *string, CHAR **endptr ); 00232 00233 void 00234 LALStringToGPS( LALStatus *status, LIGOTimeGPS *value, const CHAR *string, CHAR **endptr ); 00235 00236 #ifdef __cplusplus 00237 #pragma { 00238 } 00239 #endif 00240 00241 #endif /* _STRINGINPUT_H */
1.5.2