Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/user/user.tex

    rc185ca9 re71b09a  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Mon Feb 12 11:50:26 2024
    14 %% Update Count     : 6199
     13%% Last Modified On : Tue Jan 30 09:02:41 2024
     14%% Update Count     : 6046
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    41774177The \CFA header file for the I/O library is \Indexc{fstream.hfa}.
    41784178
    4179 
    4180 \subsubsection{Stream Output}
    4181 
    41824179For implicit formatted output, the common case is printing a series of variables separated by whitespace.
    41834180\begin{cquote}
     
    42584255Note, \CFA stream variables ©stdin©, ©stdout©, ©stderr©, ©exit©, and ©abort© overload C variables ©stdin©, ©stdout©, ©stderr©, and functions ©exit© and ©abort©, respectively.
    42594256
    4260 
    4261 \subsubsection{Stream Input}
    4262 
    42634257For implicit formatted input, the common case is reading a sequence of values separated by whitespace, where the type of an input constant must match with the type of the input variable.
    42644258\begin{cquote}
    42654259\begin{lrbox}{\myboxA}
    42664260\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    4267 char c;   int i;   double d
     4261int x;   double y   char z;
    42684262\end{cfa}
    42694263\end{lrbox}
     
    42724266\multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CC}}       & \multicolumn{1}{c}{\textbf{Python}}   \\
    42734267\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    4274 sin | c | i | d;
     4268sin | x | y | z;
    42754269\end{cfa}
    42764270&
    42774271\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    4278 cin >> c >> i >> d;
     4272cin >> x >> y >> z;
    42794273\end{cfa}
    42804274&
    42814275\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    4282 c = input();   i = int(input());   d = float(input());
     4276x = int(input());  y = float(input());  z = input();
    42834277\end{cfa}
    42844278\\
    42854279\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    4286 ®A® ®1® ®2.5®
     4280®1® ®2.5® ®A®
    42874281
    42884282
     
    42904284&
    42914285\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    4292 ®A® ®1® ®2.5®
     4286®1® ®2.5® ®A®
    42934287
    42944288
     
    42964290&
    42974291\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    4298 ®A®
    42994292®1®
    43004293®2.5®
     4294®A®
    43014295\end{cfa}
    43024296\end{tabular}
     
    43154309For floating-point types, any number of decimal digits, optionally preceded by a sign (©+© or ©-©), optionally containing a decimal point, and optionally followed by an exponent, ©e© or ©E©, with signed (optional) decimal digits.
    43164310Floating-point values can also be written in hexadecimal format preceded by ©0x© or ©0X© with hexadecimal digits and exponent denoted by ©p© or ©P©.
    4317 In all cases, whitespace characters are skipped until an appropriate value is found.
    4318 \begin{cfa}[belowskip=0pt]
    4319 char ch;  int i;  float f; double d;  _Complex double cxd;
    4320 sin | ch | i | f | d | cxd;
    4321 X   42   1234.5     0xfffp-2    3.5+7.1i
    4322 \end{cfa}
    4323 It is also possible to scan and ignore specific strings and whitespace using a string format.
    4324 \begin{cfa}[belowskip=0pt]
    4325 sin | "abc def";                                                §\C{// space matches arbitrary whitespace (2 blanks, 2 tabs)}§
    4326 \end{cfa}
    4327 \begin{cfa}[showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt]
    4328 ®abc            def®
    4329 \end{cfa}
    4330 A non-whitespace format character reads the next input character, compares the format and input characters, and if equal, the input character is discarded and the next format character is tested.
    4331 Note, a single whitespace in the format string matches \Textbf{any} quantity of whitespace characters from the stream (including none).
    4332 
    4333 For the C-string type, the default input format is any number of \Textbf{non-whitespace} characters.
    4334 There is no escape character supported in an input string, but any Latin-1 character can be typed directly in the input string.
    4335 For example, if the following non-whitespace output is redirected into a file by the shell:
    4336 \begin{cfa}[belowskip=0pt]
    4337 sout | "\n\t\f\0234\x23";
    4338 \end{cfa}
    4339 it can be read back from the file by redirecting the file as input using:
    4340 \begin{cfa}[belowskip=0pt]
    4341 char s[64];
    4342 sin | wdi( sizeof(s), s );                              §\C{// must specify string size}§
    4343 \end{cfa}
    4344 The input string is always null terminated ©'\0'© in the input variable.
    4345 Because of potential buffer overrun when reading C strings, strings are restricted to work with input manipulators \see{\VRef{s:InputManipulators}}.
    4346 As well, there are multiple input-manipulators for scanning complex input string formats, \eg a quoted character or string.
    4347 
    4348 \Textbf{In all cases, if an invalid data value is not found for a type or format string, the exception ©missing_data© is raised and the input variable is unchanged.}
    4349 For example, when reading an integer and the string ©"abc"© is found, the exception ©missing_data© is raised to ensure the program does not proceed erroneously.
    4350 If a valid data value is found, but it is larger than the capacity of the input variable, such reads are undefined.
    4351 
    4352 
    4353 \subsubsection{Stream Files}
    4354 
    4355 \VRef[Figure]{f:IOStreamFunctions} shows the I/O stream operations for interacting with files other than ©sin©, ©sout©, and ©cerr©.
     4311In all cases, all whitespace characters are skipped until an appropriate value is found.
     4312\Textbf{If an appropriate value is not found, the exception ©missing_data© is raised.}
     4313
     4314For the C-string type, there are two input forms: any number of \Textbf{non-whitespace} characters or a quoted sequence containing any characters except the closing quote, \ie there is no escape character supported in the string..
     4315In both cases, the string is null terminated ©'\0'©.
     4316For the quoted string, the start and end quote characters can be any character and do not have to match \see{\ref{XXX}}.
     4317
     4318\VRef[Figure]{f:IOStreamFunctions} shows the I/O stream operations for interacting with files other than ©cin©, ©cout©, and ©cerr©.
    43564319\begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt]
    43574320\item
     
    49694932
    49704933\subsection{Input Manipulators}
    4971 \label{s:InputManipulators}
    4972 
    4973 A string variable \emph{must} be large enough to contain the input sequence.
    4974 To force programmers to consider buffer overruns for C-string input, C-strings may only be read with a width field, which should specify a size less than or equal to the C-string size, \eg:
    4975 \begin{cfa}
    4976 char line[64];
    4977 sin | wdi( ®sizeof(line)®, line );              §\C{// must specify string size}§
    4978 \end{cfa}
    4979 
    4980 Certain input manipulators support a \newterm{scanset}, which is a simple regular expression, where the matching set contains any Latin-1 character (8-bits) or character ranges using minus.
     4934
     4935The following \Index{manipulator}s control scanning of input values (reading), and only affect the format of the argument.
     4936
     4937Certain manipulators support a \newterm{scanset}, which is a simple regular expression, where the matching set contains any Latin-1 character (8-bits) or character ranges using minus.
    49814938For example, the scanset \lstinline{"a-zA-Z -/?§"} matches any number of characters between ©'a'© and ©'z'©, between ©'A'© and ©'Z'©, between space and ©'/'©, and characters ©'?'© and (Latin-1) ©'§'©.
    49824939The following string is matched by this scanset:
    49834940\begin{cfa}
    4984 !&%$  abAA () ZZZ  ??§\S§  xx§\S\S§
    4985 \end{cfa}
    4986 To match a minus, make it the first character in the set, \eg ©"©{\color{red}\raisebox{-1pt}{\texttt{-}}}©0-9"©.
    4987 Other complex forms of regular-expression matching are unsupported.
    4988 
    4989 The following \Index{manipulator}s control scanning of input values (reading) and only affect the format of the argument.
     4941!&%$  abAA () ZZZ  ??  xx§\S\S\S§
     4942\end{cfa}
     4943To match a minus, put it as the first character, ©"-0-9"©.
     4944Other complex forms of regular-expression matching are not supported.
     4945
     4946A string variable \emph{must} be large enough to contain the input sequence.
     4947To force programmers to consider buffer overruns for C-string input, C-strings can only be read with a width field, which should specify a size less than or equal to the C-string size, \eg:
     4948\begin{cfa}
     4949char line[64];
     4950sin | wdi( ®sizeof(line)®, line ); // must specify size
     4951\end{cfa}
     4952
     4953Currently, there is no mechanism to detect if a value read exceeds the capwhen Most types are finite sized, \eg integral types only store value that fit into their corresponding storage, 8, 16, 32, 64, 128 bits.
     4954Hence, an input value may be too large, and the result of the read is often considered undefined, which leads to difficlt to locate runtime errors.
     4955All reads in \CFA check if values do not fit into the argument variable's type and raise the exception
     4956All types are
    49904957
    49914958\begin{enumerate}
    49924959\item
    4993 \Indexc{skip}( \textit{scanset} )\index{manipulator!skip@©skip©}, ©skip©( $N$ )
    4994 consumes either the \textit{scanset} or the next $N$ characters, including newlines.
    4995 If the match successes, the input characters are ignored, and input continues with the next character.
     4960\Indexc{skip}( scanset )\index{manipulator!skip@©skip©}, ©skip©( $N$ )
     4961The first form uses a scanset to skip matching characters.
     4962The second form skips the next $N$ characters, including newline.
     4963If the match successes, the input characters are discarded, and input continues with the next character.
    49964964If the match fails, the input characters are left unread.
    49974965\begin{cfa}[belowskip=0pt]
    4998 char scanset[§\,§] = "abc";
    4999 sin | "abc§\textvisiblespace§" | skip( scanset ) | skip( 5 ); §\C{// match and skip input sequence}§
     4966char sk[§\,§] = "abc";
     4967sin | "abc " | skip( sk ) | skip( 5 ); // match input sequence
    50004968\end{cfa}
    50014969\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    5002 ®abc   abc  xxx®
    5003 \end{cfa}
    5004 Again, the blank in the format string ©"abc©\textvisiblespace©"© matches any number of whitespace characters.
    5005 
    5006 \item
    5007 \Indexc{wdi}( \textit{maximum}, ©T & v© )\index{manipulator!wdi@©wdi©}
    5008 For all types except ©char *©, whitespace is skipped and the longest sequence of non-whitespace characters matching an appropriate typed (©T©) value is read, converted into its corresponding internal form, and written into the ©T© variable.
    5009 \textit{maximum} is the maximum number of characters read for the current value rather than the longest sequence.
     4970®abc   ®
     4971®abc  ®
     4972®xx®
     4973\end{cfa}
     4974
     4975\item
     4976\Indexc{wdi}( maximum, variable )\index{manipulator!wdi@©wdi©}
     4977For all types except ©char *©, whitespace is skipped until an appropriate value is found for the specified variable type.
     4978maximum is the maximum number of characters read for the current operation.
    50104979\begin{cfa}[belowskip=0pt]
    50114980char ch;   char ca[3];   int i;   double d;   
     
    50164985\end{cfa}
    50174986Here, ©ca[0]© is type ©char©, so the width reads 3 characters \Textbf{without} a null terminator.
    5018 If an input value is not found for a variable, the exception ©missing_data© is raised, and the input variable is unchanged.
    50194987
    50204988Note, input ©wdi© cannot be overloaded with output ©wd© because both have the same parameters but return different types.
     
    50224990
    50234991\item
    5024 \Indexc{wdi}( $maximum\ size$, ©char s[]© )\index{manipulator!wdi@©wdi©}
    5025 For type ©char *©, whitespace is skippped and the longest sequence of non-whitespace characters is read, without conversion, and written into the string variable (null terminated).
    5026 $maximum\ size$ is the maximum number of characters in the string variable.
    5027 If the non-whitespace sequence of input characters is greater than $maximum\ size - 1$ (null termination), the exception ©cstring_length© is raised.
     4992\Indexc{wdi}( maximum size, ©char s[]© )\index{manipulator!wdi@©wdi©}
     4993For type ©char *©, maximum is the maximum number of characters read for the current operation.
     4994Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence
    50284995\begin{cfa}[belowskip=0pt]
    5029 char cs[10];
    5030 sin | wdi( sizeof(cs), cs );
     4996char cstr[10];
     4997sin | wdi( sizeof(cstr), cstr );
    50314998\end{cfa}
    50324999\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    5033 ®012345678®
    5034 \end{cfa}
    5035 Nine non-whitespace character are read and the null character is added to make ten.
    5036 
    5037 \item
    5038 \Indexc{wdi}( $maximum\ size$, $maximum\ read$, ©char s[]© )\index{manipulator!wdi@©wdi©}
    5039 This manipulator is the same as the previous one, except $maximum$ $read$ is the maximum number of characters read for the current value rather than the longest sequence, where $maximum\ read$ $\le$ $maximum\ size$.
     5000®abcd1233.456E+2®
     5001\end{cfa}
     5002
     5003\item
     5004\Indexc{wdi}( maximum size, maximum read, ©char s[]© )\index{manipulator!wdi@©wdi©}
     5005For type ©char *©, maximum is the maximum number of characters read for the current operation.
    50405006\begin{cfa}[belowskip=0pt]
    5041 char cs[10];
    5042 sin | wdi( sizeof(cs), 9, cs );
     5007char ch;   char ca[3];   int i;   double d;   
     5008sin | wdi( sizeof(ch), ch ) | wdi( sizeof(ca), ca[0] ) | wdi( 3, i ) | wdi( 8, d );  // c == 'a', ca == "bcd", i == 123, d == 345.6
    50435009\end{cfa}
    50445010\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    5045 ®012345678®9
    5046 \end{cfa}
    5047 The exception ©cstring_length© is not raised, because the read stops reading after nine characters.
    5048 
    5049 \item
    5050 \Indexc{getline}( $wdi\ manipulator$, ©const char delimiter = '\n'© )\index{manipulator!getline@©getline©}
    5051 consumes the scanset ©"[^D]D"©, where ©D© is the ©delimiter© character, which reads all characters from the current input position to the delimiter character into the string (null terminated), and consumes and ignores the delimiter.
    5052 If the delimiter character is omitted, it defaults to ©'\n'© (newline).
     5011®abcd1233.456E+2®
     5012\end{cfa}
     5013
     5014\item
     5015\Indexc{ignore}( reference-value )\index{manipulator!ignore@©ignore©}
     5016For all types, the data is read from the stream depending on the argument type but ignored, \ie it is not stored in the argument.
    50535017\begin{cfa}[belowskip=0pt]
    5054 char cs[10];
    5055 sin | getline( wdi( sizeof(cs), cs ) );
    5056 sin | getline( wdi( sizeof(cs), cs ), 'X' ); §\C{// X is the line delimiter}§
     5018double d;
     5019sin | ignore( d );  // d is unchanged
    50575020\end{cfa}
    50585021\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    5059 ®abc ?? #@%®
    5060 ®abc ?? #@%X® w
    5061 \end{cfa}
    5062 The same value is read for both input strings.
    5063 
    5064 \item
    5065 \Indexc{quoted}( ©char & ch©, ©const char Ldelimiter = '\''©, ©const char Rdelimiter = '\0'© )\index{manipulator!quoted@©quoted©}
    5066 consumes the string ©"LCR"©, where ©L© is the left ©delimiter© character, ©C© is the value in ©ch©, and ©R© is the right delimiter character, which skips whitespace, consumes and ignores the left delimiter, reads a single character into ©ch©, and consumes and ignores the right delimiter (3 characters).
    5067 If the delimit character is omitted, it defaults to ©'\''© (single quote).
     5022®  -75.35e-4® 25
     5023\end{cfa}
     5024
     5025\item
     5026\Indexc{incl}( scanset, wdi-input-string )\index{manipulator!incl@©incl©}
     5027For C-string types only, the scanset matches any number of characters \emph{in} the set.
     5028Matching characters are read into the C input-string and null terminated.
    50685029\begin{cfa}[belowskip=0pt]
    5069 char ch;
    5070 sin | quoted( ch );   sin | quoted( ch, '"' );   sin | quoted( ch, '[', ']' );
    5071 \end{cfa}
    5072 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    5073 ®   'a'  "a"[a]®
    5074 \end{cfa}
    5075 
    5076 \item
    5077 \begin{sloppypar}
    5078 \Indexc{quoted}( $wdi\ manipulator$, ©const char Ldelimiter = '\''©, ©const char Rdelimiter = '\0'© )\index{manipulator!quoted@©quoted©}
    5079 consumes the scanset ©"L[^R]R"©, where ©L© is the left ©delimiter© character and ©R© is the right delimiter character, which skips whitespace, consumes and ignores the left delimiter, reads characters until the right-delimiter into the string variable (null terminated), and consumes and ignores the right delimiter.
    5080 If the delimit character is omitted, it defaults to ©'\''© (single quote).
    5081 \end{sloppypar}
    5082 \begin{cfa}[belowskip=0pt]
    5083 char cs[10];
    5084 sin | quoted( wdi( sizeof(cs), cs ) ); §\C[3in]{// " is the start/end delimiter}§
    5085 sin | quoted( wdi( sizeof(cs), cs ), '\'' ); §\C{// ' is the start/end delimiter}§
    5086 sin | quoted( wdi( sizeof(cs), cs ), '[', ']' ); §\C{// [ is the start and ] is the end delimiter}\CRT§
    5087 \end{cfa}
    5088 \begin{cfa}[showspaces=true]
    5089 ®   "abc"  'abc'[abc]®
    5090 \end{cfa}
    5091 
    5092 \item
    5093 \Indexc{incl}( scanset, $wdi\ manipulator$ )\index{manipulator!incl@©incl©}
    5094 consumes the scanset, which reads all the scanned characters into the string variable (null terminated).
    5095 \begin{cfa}[belowskip=0pt]
    5096 char cs[10];
    5097 sin | incl( "abc", cs );
     5030char s[10];
     5031sin | incl( "abc", s );
    50985032\end{cfa}
    50995033\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    51025036
    51035037\item
    5104 \Indexc{excl}( scanset, $wdi\ manipulator$ )\index{manipulator!excl@©excl©}
    5105 consumes the \emph{not} scanset, which reads all the scanned characters into the string variable (null terminated).
     5038\Indexc{excl}( scanset, wdi-input-string )\index{manipulator!excl@©excl©}
     5039For C-string types, the scanset matches any number of characters \emph{not in} the set.
     5040Non-matching characters are read into the C input-string and null terminated.
    51065041\begin{cfa}[belowskip=0pt]
    5107 char cs[10];
    5108 sin | excl( "abc", cs );
     5042char s[10];
     5043sin | excl( "abc", s );
    51095044\end{cfa}
    51105045\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    51125047\end{cfa}
    51135048
    5114 \item
    5115 \Indexc{ignore}( ©T & v© or ©const char cs[]© or $string\ manipulator$ )\index{manipulator!ignore@©ignore©}
    5116 consumes the appropriate characters for the type and ignores them, so the input variable is unchanged.
    5117 \begin{cfa}
    5118 double d;
    5119 char cs[10];
    5120 sin | ignore( d );                                              §\C{// d is unchanged}§
    5121 sin | ignore( cs );                                             §\C{// cs is unchanged, no wdi required}§
    5122 sin | ignore( quoted( wdi( sizeof(cs), cs ) ) ); §\C{// cs is unchanged}§
    5123 \end{cfa}
    5124 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    5125 ®  -75.35e-4 25 "abc"®
    5126 \end{cfa}
     5049\Indexc{quoted}( char delimit, wdi-input-string )\index{manipulator!quoted@©quoted©}
     5050Is an ©excl© with scanset ©"delimit"©, which consumes all characters up to the delimit character.
     5051If the delimit character is omitted, it defaults to ©'\n'© (newline).
     5052
     5053\item
     5054\Indexc{getline}( char delimit, wdi-input-string )\index{manipulator!getline@©getline©}
     5055Is an ©excl© with scanset ©"delimit"©, which consumes all characters up to the delimit character.
     5056If the delimit character is omitted, it defaults to ©'\n'© (newline).
    51275057\end{enumerate}
    51285058
Note: See TracChangeset for help on using the changeset viewer.