1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef OOX_HELPER_TEXTINPUTSTREAM_HXX
25 #define OOX_HELPER_TEXTINPUTSTREAM_HXX
26 
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <rtl/ustring.hxx>
29 
30 namespace com { namespace sun { namespace star {
31     namespace io { class XInputStream; }
32     namespace io { class XTextInputStream; }
33     namespace uno { class XComponentContext; }
34 } } }
35 
36 namespace oox {
37 
38 class BinaryInputStream;
39 
40 // ============================================================================
41 
42 class TextInputStream
43 {
44 public:
45     explicit            TextInputStream(
46                             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
47                             const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStrm,
48                             rtl_TextEncoding eTextEnc );
49 
50     explicit            TextInputStream(
51                             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
52                             BinaryInputStream& rInStrm,
53                             rtl_TextEncoding eTextEnc );
54 
55                         ~TextInputStream();
56 
57     /** Returns true, if no more text is available in the stream.
58      */
59     bool                isEof() const;
60 
61     /** Reads a text line from the stream.
62 
63         If the last line in the stream is not terminated with line-end
64         character(s), the stream will immediately go into EOF state and return
65         the text line. Otherwise, if the last character in the stream is a
66         line-end character, the next call to this function will turn the stream
67         into EOF state and return an empty string.
68      */
69     ::rtl::OUString     readLine();
70 
71     /** Reads a text portion from the stream until the specified character is
72         found.
73 
74         If the end of the stream is not terminated with the specified
75         character, the stream will immediately go into EOF state and return the
76         remaining text portion. Otherwise, if the last character in the stream
77         is the specified character (and caller specifies to read and return it,
78         see parameter bIncludeChar), the next call to this function will turn
79         the stream into EOF state and return an empty string.
80 
81         @param cChar
82             The separator character to be read to.
83 
84         @param bIncludeChar
85             True = if found, the specified character will be read from stream
86                 and included in the returned string.
87             False = the specified character will neither be read from the
88                 stream nor included in the returned string, but will be
89                 returned as first character in the next call of this function
90                 or readLine().
91      */
92     ::rtl::OUString     readToChar( sal_Unicode cChar, bool bIncludeChar );
93 
94     // ------------------------------------------------------------------------
95 
96     /** Creates a UNO text input stream object from the passed UNO input stream.
97      */
98     static ::com::sun::star::uno::Reference< ::com::sun::star::io::XTextInputStream >
99                         createXTextInputStream(
100                             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
101                             const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStrm,
102                             rtl_TextEncoding eTextEnc );
103 
104     // ------------------------------------------------------------------------
105 private:
106     void                init(
107                             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
108                             const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStrm,
109                             rtl_TextEncoding eTextEnc );
110 
111     /** Adds the pending character in front of the passed string, if existing. */
112     ::rtl::OUString     createFinalString( const ::rtl::OUString& rString );
113 
114 private:
115     ::com::sun::star::uno::Reference< ::com::sun::star::io::XTextInputStream >
116                         mxTextStrm;
117     sal_Unicode         mcPendingChar;
118 };
119 
120 // ============================================================================
121 
122 } // namespace oox
123 
124 #endif
125