cfl_token.h
Go to the documentation of this file.
1/** @file cfl_token.h @brief Class Token */
2
3/* FAU Dscrete Event Systems Library (libfaudes)
4
5Copyright (C) 2006 Bernd Opitz
6Copyright (C) 2006, 2020, 2024 Thomas Moor
7Exclusive copyright is granted to Klaus Schmidt
8
9This library is free software; you can redistribute it and/or
10modify it under the terms of the GNU Lesser General Public
11License as published by the Free Software Foundation; either
12version 2.1 of the License, or (at your option) any later version.
13
14This library is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public
20License along with this library; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23
24#ifndef FAUDES_TOKEN_H
25#define FAUDES_TOKEN_H
26
27#include "cfl_definitions.h"
28#include "cfl_utils.h"
29#include <string>
30#include <iostream>
31#include <fstream>
32#include <assert.h>
33
34namespace faudes {
35
36/**
37 * Tokens model atomic data for stream IO.
38 *
39 * A Token models a string or numeric datum that can be read from a
40 * or written to a C++ stream. The class itself implements the representation of
41 * the data including its type and parsing from/to C++ streams. For section handling
42 * see TokenReader and TokenWriter.
43 *
44 * There is no one-to-one correspondence between elementary faudes data types like
45 * string or index with a particlular representation. The class Token therefore takes
46 * a relaxed approach and indicates possible interpretations when reading from a stream
47 * and requiering specific directions when writing.
48 *
49 * See TokenType for a list of supported formats/interpretations.
50 *
51 * @ingroup TokenIO
52 */
53
55public:
56
57 friend class TokenWriter;
58 friend class TokenReader;
59
60 /**
61 * Empty constructor, constructs None token
62 */
63 Token(void);
64
65 /**
66 * Copy constructor
67 */
68 Token(const Token& rToken);
69
70
71 /**
72 * Token destructor
73 */
74 ~Token(void);
75
76 /** Assignment operator */
77 Token& operator= (const Token& rOther);
78
79 /**
80 * Token types:
81 */
82 enum TokenType {
83 None= 0x000, ///< Invalid/empty token
84 Begin= 0x001, ///< <label> (begin of section)
85 End= 0x002, ///< <\\label> (end of section)
86 String= 0x004, ///< any string, space separated or quoted, must start with a letter
87 Option= 0x008, ///< +xyZ+ (option string, may not contain a "+")
88 Integer= 0x010, ///< 1234 (non-negative integer)
89 Integer16=0x020, ///< 0x12fff ("0x" makes an integer an Integer16)
90 Boolean= 0x040, ///< True/False
91 Float= 0x080, ///< -12.34 ("-" or "." turns an integer to a float)
92 Binary= 0x100, ///< =ABhlkjj= (base64 encoded binary data)
93 Cdata= 0x200 ///< <![CDATA[ ... ]]> verbatim markup
94 };
95
96
97 /**
98 * Initialize None token
99 *
100 */
101 void SetNone(void);
102
103 /**
104 * Initialize as String token
105 *
106 * @param rName
107 * String to fill the Token
108 */
109 void SetString(const std::string& rName);
110
111 /**
112 * Initialize as Begin token
113 *
114 * @param rName
115 * Title of section to fill the Token
116 */
117 void SetBegin(const std::string& rName);
118
119 /**
120 * Initialize as End token
121 *
122 * @param rName
123 * Title of section to fill the Token
124 */
125 void SetEnd(const std::string& rName);
126
127 /**
128 * Initialize as empty-tag token
129 *
130 * @param rName
131 * Title of section to fill the Token
132 */
133 void SetEmpty(const std::string& rName);
134
135 /**
136 * Initialize as Option token
137 *
138 * @param rName
139 * Option to fill the Token
140 */
141 void SetOption(const std::string& rName);
142
143 /**
144 * Initialize as Integer token
145 *
146 * @param number
147 * Number to fill the Token
148 */
149 void SetInteger(const Int number);
150
151 /**
152 * Initialize as Integer16 token
153 *
154 * @param number
155 * Number to fill the Token
156 */
157 void SetInteger16(const Int number);
158
159 /**
160 * Initialize as Boolean token
161 *
162 * @param number
163 * Number to fill the Token
164 */
165 void SetBoolean(const Int number);
166
167 /**
168 * Initialize as Float token
169 *
170 * @param number
171 * Number to fill the Token
172 */
173 void SetFloat(const faudes::Float number);
174
175 /**
176 * Initialize Binary token.
177 * This method allocates a copy of the data.
178 * For writing only, you may use the TokenWriter interface
179 * to avoid the local copy.
180 *
181 * @param data
182 * Reference to raw data record
183 * @param len
184 * Number of bytes in record
185 */
186 void SetBinary(const char* data, std::size_t len);
187
188
189 /**
190 * Clear End type (resolve empty section)
191 *
192 */
193 void ClrEnd(void);
194
195 /**
196 * Get integer value of a numeric token
197 *
198 * @return
199 * Token's integer value
200 */
201 Int IntegerValue(void) const;
202
203 /**
204 * Get float value of a numeric token
205 *
206 * @return
207 * Token float value
208 */
209 faudes::Float FloatValue(void) const;
210
211 /**
212 * Get string value of a name token
213 *
214 * @return
215 * Token's string value
216 */
217 const std::string& StringValue(void) const;
218
219 /**
220 * Get option value of a name token
221 *
222 * @return
223 * Token's option value
224 */
225 const std::string& OptionValue(void) const;
226
227 /**
228 * Preceeding space when writing to stream
229 *
230 * @return
231 * preceeding string
232 *
233 */
234 const std::string& PreceedingSpace(void) const;
235
236 /**
237 * Preceeding space when writing to stream
238 *
239 * @param sep
240 * set new value
241 */
242 void PreceedingSpace(const std::string& sep);
243
244 /**
245 * Get token Type
246 *
247 * This method is for backward compatibility only. It returns
248 * a token type with only one bit set to indicate the
249 * type as in libfaudes up to version 2.17. To test
250 * for possible token interpretations, use the 2.17 interface
251 * IsInteger(), IsString() etc.
252 * @return
253 * Type of token
254 */
255 TokenType Type(void) const;
256
257 /**
258 * Test token Type
259 *
260 * @return
261 * True on match
262 */
263 bool IsNone(void) const;
264
265 /**
266 * Test token Type
267 *
268 * @return
269 * True on match
270 */
271 bool IsInteger(void) const;
272
273 /**
274 * Test token Type
275 *
276 * @return
277 * True on match
278 */
279 bool IsInteger16(void) const;
280
281 /**
282 * Test token Type
283 *
284 * @return
285 * True on match
286 */
287 bool IsBoolean(void) const;
288
289 /**
290 * Test token Type
291 *
292 * @return
293 * True on match
294 */
295 bool IsFloat(void) const;
296
297 /**
298 * Test token Type
299 *
300 * @return
301 * True on match
302 */
303 bool IsOption(void) const;
304
305 /**
306 * Test token Type
307 *
308 * @return
309 * True on match
310 */
311 bool IsString(void) const;
312
313 /**
314 * Test token Type
315 *
316 * @return
317 * True on match
318 */
319 bool IsBinary(void) const;
320
321 /**
322 * Test token Type
323 *
324 * @return
325 * True on match
326 */
327 bool IsCdata(void) const;
328
329 /**
330 * Test token Type
331 *
332 * @return
333 * True on match
334 */
335 bool IsBegin(void) const;
336
337 /**
338 * Test token Type
339 *
340 * @param tag
341 * Section tag to test for
342 * @return
343 * True on match
344 */
345 bool IsBegin(const std::string& tag) const;
346
347 /**
348 * Test token Type
349 *
350 * @return
351 * True on match
352 */
353 bool IsEnd(void) const;
354
355 /**
356 * Test token Type
357 *
358 * @param tag
359 * Section tag to test for
360 * @return
361 * True on match
362 */
363 bool IsEnd(const std::string& tag) const;
364
365 /**
366 * Test token Type
367 *
368 * @return
369 * True on match
370 */
371 bool IsEmpty(void) const;
372
373 /**
374 * Clear all attributes.
375 *
376 */
377 void ClearAttributes();
378
379 /**
380 * Clear attribute.
381 *
382 * @param name
383 * Attribute name
384 */
385 void ClrAttribute(const std::string& name);
386
387 /**
388 * Insert named attribute, no type.
389 * Note: only begin tags can have attributes.
390 *
391 * @param name
392 * Attribute name
393 * @param value
394 * Attribute value
395 */
396 void InsAttribute(const std::string& name, const std::string& value);
397
398 /**
399 * Insert named attribute with string value.
400 * Note: only begin tags can have attributes.
401 *
402 * @param name
403 * Attribute name
404 * @param value
405 * Attribute value
406 */
407 void InsAttributeString(const std::string& name, const std::string& value);
408
409 /**
410 * Insert named attribute with integer value.
411 * Note: only begin tags can have attributes.
412 *
413 * @param name
414 * Attribute name
415 * @param value
416 * Attribute value
417 */
418 void InsAttributeInteger(const std::string& name, Int value);
419
420 /**
421 * Insert named attribute with integer value.
422 * Note: only begin tags can have attributes.
423 *
424 * @param name
425 * Attribute name
426 * @param value
427 * Attribute value
428 */
429 void InsAttributeInteger16(const std::string& name, Int value);
430
431 /**
432 * Insert named attribute with boolean value.
433 * Note: only begin tags can have attributes.
434 *
435 * @param name
436 * Attribute name
437 * @param value
438 * Attribute value
439 */
440 void InsAttributeBoolean(const std::string& name, Int value);
441
442 /**
443 * Insert named attribute with integer value.
444 * Note: only begin tags can have attributes.
445 *
446 * @param name
447 * Attribute name
448 * @param value
449 * Attribute value
450 */
451 void InsAttributeFloat(const std::string& name, faudes::Float value);
452
453 /**
454 * Test attibute existence.
455 *
456 * @param name
457 * Attribute name
458 * @return
459 * True is attribute exists and matches type.
460 */
461 bool ExistsAttributeString(const std::string& name);
462
463 /**
464 * Test attibute existence.
465 *
466 * @param name
467 * Attribute name
468 * @return
469 * True is attribute exists and matches type.
470 */
471 bool ExistsAttributeInteger(const std::string& name);
472
473 /**
474 * Test attibute existence.
475 *
476 * @param name
477 * Attribute name
478 * @return
479 * True is attribute exists and matches type.
480 */
481 bool ExistsAttributeFloat(const std::string& name);
482
483 /**
484 * Access attribute value
485 *
486 * @param name
487 * Attribute name
488 * @return
489 * String value of specified attribute
490 */
491 const std::string& AttributeStringValue(const std::string& name);
492
493 /**
494 * Access attribute value
495 *
496 * @param name
497 * Attribute name
498 * @return
499 * Integer value of specified attribute (return 0 if it does not exist)
500 */
501 Int AttributeIntegerValue(const std::string& name);
502
503
504 /**
505 * Access attribute value
506 *
507 * @param name
508 * Attribute name
509 * @return
510 * String value of specified attribute
511 */
512 faudes::Float AttributeFloatValue(const std::string& name);
513
514
515 /**
516 * Read Token from input stream
517 *
518 * @param pStream
519 * Pointer to std::ifstream
520 * @param fcomments
521 * when true, faudes comments are skipped
522 * @exception Exception
523 * - ios exceptions (eg file io error)
524 * @return
525 * line count
526 */
527 int Read(std::istream* pStream, bool fcomments=true);
528
529 /**
530 * Write Token to output stream
531 *
532 * @param pStream
533 * Pointer to ostream
534 * @exception Exception
535 * - ios exceptions (eg file io error,)
536 */
537 void Write(std::ostream* pStream) const;
538
539
540 /** Write specified binary data as base64 string to output stream
541 *
542 * @param pStream
543 * Reference to std::ostream
544 * @param len
545 * Number of bytes to write
546 * @param pData
547 * Data to write
548 */
549 static void WriteBinary(std::ostream* pStream, const char* pData, std::size_t len);
550
551
552 /** Write a std::string value to an output stream.
553 *
554 * This method writes a string as verbatim character data using the
555 * markup "<[!CDATA[ ... ]]> i.e. incl all control characters.
556 * The flag is used to insert extra formating at the begin and the end
557 * of the markup.
558 *
559 * @param pStream
560 * Reference to std::ostream
561 * @param rString
562 * String to write
563 * @param lfflag
564 * add extra linefeeds etc
565 */
566 static void WriteVerbatim(std::ostream* pStream, const std::string& rString, bool lfflag=0);
567
568
569 /** Write a std::string value to an output stream.
570 *
571 * This method replace critical characters by their XML entities and
572 * streams the resulting string. No whitespace etc added.
573 *
574 * @param pStream
575 * Reference to std::ostream
576 * @param outstr
577 * String to stream
578 * @return
579 * Number of characters written.
580 */
581 static int WriteEscapedString(std::ostream* pStream, const std::string& outstr);
582
583
584 /** Read a std::string value from an input file stream.
585 *
586 * Read an XML escaped string until excluding the specified stop character.
587 *
588 * @param pStream
589 * Reference to std::istream
590 * @param stop
591 * Stop character
592 * @param rString
593 * Reference to result.
594 *
595 * @return
596 * Line count or -1 for error
597 */
598 static int ReadEscapedString(std::istream* pStream, char stop, std::string& rString);
599
600 /** Read chracter data from an input file stream.
601 *
602 * Reads the stream until the next "<" character.
603 * The plain character data is returned, no entities re-substituted.
604 *
605 * If fcomments is set, faudes-comments are ignored.
606 *
607 * @param pStream
608 * Reference to std::istream
609 * @param rString
610 * Reference to result.
611 * @param fcomments
612 * when true, faudes comments are skipped
613 *
614 * @return
615 * Line count or -1 for error
616 */
617 static int ReadCharacterData(std::istream* pStream, std::string& rString, bool fcomments);
618
619 /**
620 * Pretty print string representation
621 *
622 * Convenience functio for inspection/debugging
623 *
624 * @return
625 * Printable string representation
626 *
627 */
628 std::string Str(void) const;
629
630
631private:
632
633 /** Token type */
634 int mType;
635
636 /** Token std::string value (for any token type) */
637 std::string mStringValue;
638
639 /** Token std::string value (if token is of type Option) */
640 std::string mOptionValue;
641
642 /** Token integer value (if Token is of type Integer or Integer16) */
644
645 /** Token float value (if Token is of type Float or Integer) */
647
648 /** Preceeding space (cosmetic) */
649 std::string mPreceedingSpace;
650
651 /** Attribute value */
653 public:
654 AttributeValue(void) {mType=None;}
655 std::string mStringValue;
658 int mType;
659 unsigned int mSort;
660 };
661
662 /** Attribute value map */
663 std::map<std::string, AttributeValue> mAttributes;
664
665 /** Attribute sort index (for nice output only) */
667
668 /** Convenience typedef */
669 typedef std::map<std::string, AttributeValue>::iterator aiterator;
670 typedef std::map<std::string, AttributeValue>::const_iterator caiterator;
671
672 /** Interpret attribute value from string */
673 void InterpretAttribute(aiterator ait);
674
675 /** Interpret string a s number */
676 bool InterpretNumber(const std::string& numstr, int& type, Int& ival, faudes::Float& fval);
677
678 /** Interpret string a s number */
679 bool InterpretNumber(void);
680
681 /** Write a std::string value to an output stream.
682 *
683 * This method writes the string enclosed by a the specified
684 * delimiter, typically '"' or ' '. Relevant XML entities are
685 * replaced by references, e.g. &amp;lt; &amp;&amp; etc. A
686 * single white space is added as a sepqrqtor.
687 *
688 * @param pStream
689 * Reference to std::ostream
690 * @param delim
691 * Delimiter
692 */
693 void WriteString(std::ostream* pStream, const std::string& delim) const;
694
695 /** Write my binary data as base64 string to output stream
696 *
697 * @param pStream
698 * Reference to std::ostream
699 */
700 void WriteBinary(std::ostream* pStream) const;
701
702 /** Read a std::string value from an input file stream.
703 *
704 * This method assumes that the string was written in the format
705 * of WriteString, i.e. either enclosed by single stop characters or
706 * with a trailing space as stop character. For practical reasons,
707 * it is assumed that the first stop character has been allready read.
708 *
709 * @param pStream
710 * Reference to std::istream
711 * @param stop
712 * Stop character
713 *
714 * @return
715 * Line count or -1 for error
716 */
717 int ReadString(std::istream* pStream, char stop);
718
719 /** Read and interpret attribute definitions of begin tags from an input file stream.
720 *
721 *
722 * @param pStream
723 * Reference to std::istream
724 *
725 * @return
726 * Line count or -1 for error
727 */
728 int ReadAttributes(std::istream* pStream);
729
730 /** Read and interpret markup an input file stream.
731 *
732 * This method will identify begin and end tags. Any other XML
733 * markup is meant to be gracefully ignored.
734 *
735 * @param pStream
736 * Reference to std::istream
737 *
738 * @return
739 * Line count or -1 for error
740 */
741 int ReadMarkup(std::istream* pStream);
742
743 /**
744 * Read a base64 binary string from an input file stream
745 *
746 * @param pStream
747 * Reference to std::istream
748 *
749 * @return
750 * Line count or -1 for error
751 */
752 int ReadBinary(std::istream* pStream);
753
754 /**
755 * Read (ignore) spaces on input file stream
756 *
757 * @param pStream
758 * Reference to std::istream
759 * @param fcomments
760 * when true, read space swallows fcomments
761 *
762 * @return
763 * Number of lines read
764 */
765 int ReadSpace(std::istream* pStream, bool fcomments=true);
766};
767
768} // namespace faudes
769
770#endif
771
772
#define FAUDES_API
std::map< std::string, AttributeValue >::const_iterator caiterator
Definition cfl_token.h:670
faudes::Float mFloatValue
Definition cfl_token.h:646
std::string mOptionValue
Definition cfl_token.h:640
int mAttributeCount
Definition cfl_token.h:666
std::map< std::string, AttributeValue > mAttributes
Definition cfl_token.h:663
std::map< std::string, AttributeValue >::iterator aiterator
Definition cfl_token.h:669
std::string mPreceedingSpace
Definition cfl_token.h:649
std::string mStringValue
Definition cfl_token.h:637
double Float
long int Int

libFAUDES 2.33k --- 2025.09.16 --- c++ api documentaion by doxygen