1 : /******************************************************************************
2 : *
3 : * Purpose: Implementation of the CPCIDSK_TEX class.
4 : *
5 : ******************************************************************************
6 : * Copyright (c) 2010
7 : * PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
8 : *
9 : * Permission is hereby granted, free of charge, to any person obtaining a
10 : * copy of this software and associated documentation files (the "Software"),
11 : * to deal in the Software without restriction, including without limitation
12 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 : * and/or sell copies of the Software, and to permit persons to whom the
14 : * Software is furnished to do so, subject to the following conditions:
15 : *
16 : * The above copyright notice and this permission notice shall be included
17 : * in all copies or substantial portions of the Software.
18 : *
19 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 : * DEALINGS IN THE SOFTWARE.
26 : ****************************************************************************/
27 :
28 : #include "pcidsk_exception.h"
29 : #include "segment/cpcidsk_tex.h"
30 : #include <cassert>
31 : #include <cstring>
32 :
33 : using namespace PCIDSK;
34 :
35 : /************************************************************************/
36 : /* CPCIDSK_TEX() */
37 : /************************************************************************/
38 :
39 0 : CPCIDSK_TEX::CPCIDSK_TEX( PCIDSKFile *file, int segment,
40 : const char *segment_pointer )
41 0 : : CPCIDSKSegment( file, segment, segment_pointer )
42 :
43 : {
44 0 : }
45 :
46 : /************************************************************************/
47 : /* ~CPCIDSK_TEX() */
48 : /************************************************************************/
49 :
50 0 : CPCIDSK_TEX::~CPCIDSK_TEX()
51 :
52 : {
53 0 : }
54 :
55 : /************************************************************************/
56 : /* ReadText() */
57 : /************************************************************************/
58 :
59 0 : std::string CPCIDSK_TEX::ReadText()
60 :
61 : {
62 0 : PCIDSKBuffer seg_data;
63 :
64 0 : seg_data.SetSize( (int) GetContentSize() );
65 :
66 0 : ReadFromFile( seg_data.buffer, 0, seg_data.buffer_size );
67 :
68 : int i;
69 0 : char *tbuffer = (char *) seg_data.buffer;
70 :
71 0 : for( i = 0; i < seg_data.buffer_size; i++ )
72 : {
73 0 : if( tbuffer[i] == '\r' )
74 0 : tbuffer[i] = '\n';
75 :
76 0 : if( tbuffer[i] == '\0' )
77 0 : break;
78 : }
79 :
80 0 : return std::string( (const char *) seg_data.buffer, i );
81 : }
82 :
83 : /************************************************************************/
84 : /* WriteText() */
85 : /************************************************************************/
86 :
87 0 : void CPCIDSK_TEX::WriteText( const std::string &text_in )
88 :
89 : {
90 : // Transform all \n's to \r's (chr(10) to char(13)).
91 0 : unsigned int i, i_out = 0;
92 0 : std::string text = text_in;
93 :
94 0 : for( i = 0; i < text.size(); i++ )
95 : {
96 0 : if( text[i] == '\0' )
97 : {
98 0 : text.resize( i );
99 0 : break;
100 : }
101 :
102 0 : if( text[i] == '\n' && text[i+1] == '\r' )
103 : {
104 0 : text[i_out++] = '\r';
105 0 : i++;
106 : }
107 0 : else if( text[i] == '\r' && text[i+1] == '\n' )
108 : {
109 0 : text[i_out++] = '\r';
110 0 : i++;
111 : }
112 0 : else if( text[i] == '\n' )
113 0 : text[i_out++] = '\r';
114 : else
115 0 : text[i_out++] = text[i];
116 : }
117 :
118 0 : text.resize( i_out );
119 :
120 : // make sure we have a newline at the end.
121 :
122 0 : if( i_out > 0 && text[i_out-1] != '\r' )
123 0 : text += "\r";
124 :
125 : // We really *ought* to ensure the rest of the segment
126 : // is zeroed out to properly adhere to the specification.
127 : // It might also be prudent to ensure the segment grows
128 : // in 32K increments to avoid "move to end of file churn"
129 : // if several text segments are growing a bit at a time
130 : // though this is uncommon.
131 :
132 0 : WriteToFile( text.c_str(), 0, text.size() + 1 );
133 0 : }
|