Angelika Langer - Training & Consulting
HOME | COURSES | TALKS | ARTICLES | GENERICS | LAMBDAS | IOSTREAMS | ABOUT | NEWSLETTER | CONTACT | Twitter | Lanyrd | Linkedin
 
HOME 
GENERICS 
LAMBDAS 

IOSTREAMS

    BOOK PAGE
    ERRATA
    ABSTRACT
    CONTENT
    PREFACE
    EXCERPT
    QUOTES
    AUTHORS
    REVIEWS
    CODE
    FORUM
    SEMINAR   
 

ABOUT 
NEWSLETTER 
CONTACT 
Table of Contents

 
           

Standard C++ IOStreams and Locales

Advanced Programmer's Guide and Reference

Angelika Langer & Klaus Kreft
Addison-Wesley, January 2000
ISBN 0-201-18395-1

Table of Contents
Preface xv
Foreword xix
Guide to Readers xxi

PART I: STREAM INPUT AND OUTPUT

1. IOStreams Basics 3

1.1 Input and Output 3
1.2 Formatted Input/Output 12
1.2.1 The Predefined Global Streams 12
1.2.2 The Input and Output Operators 13
1.2.3 The Format Parameters of a Stream 16
1.2.4 Manipulators 22
1.2.5 The Locale of a Stream 27
1.2.6 Comparison Between Formatted Input and Output 28
1.2.7 Peculiarities of Formatted Input 29
1.3 The Stream State 31
1.3.1 The Stream State Flags 31
1.3.2 Checking the Stream State 34
1.3.3 Catching Stream Exceptions 36
1.3.4 Resetting the Stream State 38
1.4 File Input/Output 39
1.4.1 Creating, Opening, Closing and Destroying File Streams 39
1.4.2 The Open Modes 41
1.4.3 Bidirectional File Streams 45
1.5 In-Memory Input/Output 47
1.6 Unformatted Input/Output 48
1.7 Stream Positioning 51
1.8 Synchronization of Streams 54
1.8.1 Means of Synchronization 54
1.8.1.1 Synchronization via flush() and sync() 54
1.8.1.2 Synchronization using the unitbuf Format Flag 55
1.8.1.3 Synchronization by Tying Streams 55
1.8.2 Synchronizing the Predefined Standard Streams 56
1.8.2.1 Synchronization Among the Predefined Standard Streams 57
1.8.2.2 Synchronization with the C Standard I/O 57
1.8.2.3 Synchronization with the External Device 58
1.8.2.4 Synchronization Between Predefined Standard for Narrow and Wide Characters 58

2. The Architecture of IOStreams 61

2.1 The Stream Class 63
2.1.1 Class Heirarchy 63
2.1.1.1 The Stream Base Classes 63
2.1.1.2 The General Stream Classes 66
2.1.1.3 The Concrete Stream Classes 68
2.1.2 How Streams Maintain their Stream Buffer 69
2.1.3 Copying and Assignment of Streams 72
2.1.4 How Streams Maintain Their Locale 76
2.1.5 Collaboration Among Streams, Stream Buffers, and Locales 79
2.2 The Stream Buffer Classes 84
2.2.1 Class Heirarchy 85
2.2.2 The Stream Buffer Abstraction 86
2.2.3 String Stream Buffers 91
2.2.4 File Stream Buffers 97
2.3 Character Types and Character Traits 109
2.3.1 Character Representations 109
2.3.2 Character Traits 114
2.3.2.1 Requirements of a Character Traits Type 114
2.3.2.1.1 The End-of-File Character 114
2.3.2.1.2 Copying, Finding, and Comparing Characters 116
2.3.2.1.3 Conversion State 116
2.3.2.1.4 Stream Positions 116
2.3.2.2 The Predefined Standard Character Traits 118
2.3.3 Character Types 119
2.3.3.1 Requirements for Character Types 120
2.4 Stream Iterators and Stream Buffer Iterators 123
2.4.1 The Concepts of Iterators in the Standard Library 123
2.4.2 Stream Iterators 126
2.4.2.1 Output Stream Iterator 127
2.4.2.2 Input Stream Iterator 129
2.4.2.3 Stream Iterators are One-Pass Iterators 132
2.4.3 Stream Buffer Iterators 134
2.4.3.1 Output Stream Buffer Iterator 134
2.4.3.2 Input Stream Buffer Iterator 136
2.5 Additional Stream Storage and Stream Callbacks 140
2.5.1 Additional Stream Storage 140
2.5.2 Stream Callbacks 142

3. Advanced IOStreams Usage 145

3.1 Input and Output of User-Defined Types 145
3.1.1 The Signature of Inserters and Extractors 146
3.1.2 First Inserters and Extractors 146
3.1.3 Refinements 150
3.1.3.1 Format control 150
3.1.3.2 Prefix and Suffix Operations 151
3.1.3.3 Error Indication 152
3.1.3.4 Internationalization 154
3.1.3.5 I/O Operations 155
3.1.4 Refined Inserters and Extractors 156
3.1.4.1 Internationalization 156
3.1.4.2 Prefix and Suffix Operations 158
3.1.4.3 Format Control 159
3.1.4.4. Error Indication 161
3.1.4.5 Using the Refined Inserter and Extractor 168
3.1.5 Generic Inserters and Extractors 169
3.1.6 Simple Versus Refined Approach 174
3.2 User-Defined Manipulators 176
3.2.1 Manipulators Without Parameters 177
3.2.2 Manipulators with Parameters 179
3.2.1.1 Straightforward Manipulator Implementations 179
3.2.2.2 Generalized Technique: Using a Manipulator Base Template 180
3.2.2.3 Variants of a Manipulator Implementation 184
3.2.2.4 Refinements 186
3.2.2.4.1 Manipulator Base Template with Error Handling 186
3.2.2.4.2 Manipulators with State 189
3.2.2.5 The Standard Manipulator Base Type smanip 191
3.3 Extending Stream Functionality 191
3.3.1 Using Stream Storage for Private Use: iword, pword, and xalloc 192
3.3.1.1 Initializing and Maintaining the iword/pword Index 195
3.3.1.2 Implementing the Date Inserter 195
3.3.1.3 Implementing the Maniuplator 197
3.3.1.4 Using Stream Callbacks for Memory Management 200
3.3.1.5 Error Indication of Stream Callback Functions 204
3.3.1.5.1 Extending the Example 205
3.3.1.6 Using the New Functionality 207
3.3.1.7 Evaluation of the iword/pword Approach 207
3.3.2 Creating New Stream Classes by Derivation 209
3.3.2.1 Deriving the New Stream Classes by Derivation 210
3.3.2.2 Implementing the Date Inserter and the Manipulator 218
3.3.2.2.1 Implementing the Date Inserter 218
3.3.2.2.2 Implementing the Manipulator 221
3.3.2.2.3 Using the New Functionality 221
3.3.2.3 Summary 222
3.3.3 Comparing Both Solutions--iword/pword Versus Derivation 223
3.4 Adding Stream Buffer Functionality 225
3.4.1 Deriving from the Stream Buffer Base Class 225
3.4.1.1 Core Functionality of Stream Buffers Character Transportation 226
3.4.1.1.1 Stream Buffer for Unbuffered Character Transport 229
3.4.1.1.2 Stream Buffer for Buffered Character Transport 235
3.4.1.2 Optional Functionality of Stream Buffers 244
3.4.1.3 Providing New Stream Classes Along with New Stream Buffer Classes 245
3.4.2 Deriving from Concrete Stream Buffer Classes 246

PART I I: INTERNATIONALIZATION

4. Introduction to Internationalization and Localization 251

4.1 Internationalization and Localization 252
4.2 Cultural Conventions 253
4.2.1 Language 253
4.2.2 Numerical Values 253
4.2.3 Monetary Values 254
4.2.4 Time and Date 254
4.2.5 Sorting Words 255
4.2.6 Messages 256
4.2.7 Character Encodings 257
4.2.7.1 Terms and Definitions 257
4.2.7.2 Character Codesets 258
4.2.7.3 Character Encoding Schemes 259
4.2.7.3.1 Japanese Multibyte Encoding Schemes 260
4.2.7.4 Uses of Multibyte Encodings and Wide Characters 262
4.2.7.5 Code Conversions 263

5. Locales 265

5.1 Creating Locale Objects 267
5.1.1 Named Locales 267
5.1.2 Combined Locales 268
5.1.3 The Global Locale 270
Retrieving Facets from a Locale 271
5.2.1 has_facet 272
5.2.2 use_facet 272

6. Standard Facets 275

6.1 Alphabet- and Language-Related Facets 276
6.1.1 Character Classification 276
6.1.1.1 Character Classification 276
6.1.1.2 Character Conversion to Upper- and Lowercase 278
6.1.1.3 Character Conversion Between charT and char 279
6.1.1.4 Special Properties of ctype 279
6.1.2 String Collation 280
6.1.3 Code Conversion 282
6.1.4 Message Catalogs 285
6.2 Formatting and Parsing Facets 286
6.2.1 Numerical and Boolean Values 286
6.2.1.1 The numpunct Facet 287
6.2.1.2 The num_put Facet 287
6.2.1.3 The num_get Facet 289
6.2.2 Monetary Values 290
6.2.2.1 The moneypunct Facet 291
6.2.2.2 The money_put Facet 292
6.2.2.3 The money_get Facet 294
6.2.3 Date and Time Values 295
6.2.3.1 The time_put Facet 295
6.2.3.2 The time_get Facet 297
6.3 Grouping of Standard Facets in a Locale 299
6.3.1 The Standard Facet Families 299
6.3.1.1 The Standard Facet Base Class Templates 299
6.3.1.2 The Derived by name Facets 300
6.3.1.3 Behavior of the Base Class Facets 300
6.3.1.4 Mandatory Facet Types 302
6.3.2 Locale Categories 304
6.3.3 Diagram: Facets and Categories 305
6.4 Advanced Usage of the Standard Facets 306
6.4.1 Indirect Use of a Facet Through a Stream 306
6.4.2 Use of a Facet Through a Locale 307
6.4.3 Direct Use of the Facet Independent of a Locale 309

7. The Archtecture of the Locale Framework 311

7.1 Class Hierarchy 311
7.2 Identification and Lookup of Facets in a Locale 311
7.2.1 Facet Identification 312
7.2.2 Facet Lookup 315
7.2.2.1 Retrieval of Facets from a Locale 315
7.2.2.2 Storing Facets in a Locale 319
7.2.2.3 The Rationale Behind the Use of the Two-Phase Polymorphism 320
7.3 Memory Management of Facets in Locale 320
7.3.1 The Facet Reference Counter 321
7.3.2 Immutability of Facets in a Locale 326

8. User-Defined Facets 327

8.1 Adding a User-Defined Facet to an Existing Facet Family 327
8.2 Defining a New Facet Family 331
Reference Guide 343
Introduction 343
1. Locale 346
header file 346
global functions 349
codecvt 352
codecvt_base 358
codecvt_byname 360
collate 362
collate_byname 365
ctype 367
ctype 373
cytpe_base 377
ctype_byname 379
locale 381
messages 387
messages_base 390
messages_byname 391
money_base 393
money_get 395
moneypunct 398
moneypunct_byname 403
money_put 405
num_get 408
numpunct 414
numpunct_byname 417
num_put 419
time_base 423
time_get 425
time_get_byname 430
time_put 431
time_put_byname 434
time_base 436
tm 438
2. Character Traits 440
header file 440
char_traits 442
char_traits 443
char_traits 447 

3 IOStreams 451
header file 451
global type definitions 464
global objects 465
basic_filebuf 467
basic_fstream 471
basic_ifstream 473
basic_ios 475
basic_iostream 480
basic_istream 481
basic_istringstream 492
basic_ofstream 494
basic_ostream 496
basic_ostringstream 506
basic_streambuf 508
basic_stringbuf 517
basic_stringstream 521
fpos 523
ios_base 524
manipulators 538

4. Stream Iterators 540
header file 540
istreambuf_iterator 542
istream_iterator 546
iterator 549
iterator category tags 550
ostreambuf_iterator 551
ostream_iterator 55 

5. Other I/O Operations 555
bitset 555
complex 557
basic_string 558 

Appendix A: Parsing and Extraction of Numerical and bool Values 563
A.1 Parsing Numerical Values 564
A.2 Parsing bool Values 566
A.3 Conversion Specifier and Length Modifier 566
Appendix B: Formatting of Numerical and bool Values 569
B.1 Formatting Numerical Values 570
B.2 Formatting bool Values 572
B.3 Conversion Specifiers, Qualifiers, and Length Modifiers 573
Appendix C: strftime() Conversion Specifiers 575
Appendix D: Correspondences Between C and C++IOStreams 579
D.1 File Open Modes 579
D.2 Stream Positions 580
Appendix E: Differences Between Classic and Standard IOStreams 581
E.1 Templatizing the IOStreams Classes 582
E.2 Splitting Base Class ios 583
E.3 Indicating Errors 584
E.4 Internationalizing IOStreams 585
E.5 Removing_withassign Classes 585
E.6 Removing File Descriptors 586
E.7 String Streams: Replacing strstream by stringstream 587
E.8 Changes to the Stream Buffer Classes 588
E.9 Minor changes 590
Appendix F: Relationship Between C and C++ Locales 591
F.1 Locale Categories in C and C++ 591
F.2 The Global Locale in C and C++ 592
Appendix G: New C++ Features and Idoms 595
G.1 Bitmask Types 595
G.2 POD--Plain Old Data 596
G.3 Explicit Constructors 596
G.4 Template Specialization 599
G.5 Default Template Arguments 604
G.6 Explicit Template Argument Specification 607
G.7 The Typename Keyword 609
G.8 Dynamic Cast 612
G.9 Function try Blocks 616
G.10 Standard Exceptions 619
G.11 Numeric Limits 620
G.12 C++ Strings 620
Bibliography 623
Index 635
 
  © Copyright 1995-2003 by Angelika Langer.  All Rights Reserved.    URL: < http://www.AngelikaLanger.com/IOStreams/contents.htm  last update: 23 Oct 2003