Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Purpose: Implementation of the CPCIDSK_TEX class. 4 : * 5 : ****************************************************************************** 6 : * Copyright (c) 2010 7 : * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada. 8 : * 9 : * SPDX-License-Identifier: MIT 10 : ****************************************************************************/ 11 : 12 : #include "pcidsk_exception.h" 13 : #include "segment/cpcidsk_tex.h" 14 : #include <cassert> 15 : #include <cstring> 16 : 17 : using namespace PCIDSK; 18 : 19 : PCIDSK_TEX::~PCIDSK_TEX() = default; 20 : 21 : /************************************************************************/ 22 : /* CPCIDSK_TEX() */ 23 : /************************************************************************/ 24 : 25 0 : CPCIDSK_TEX::CPCIDSK_TEX( PCIDSKFile *fileIn, int segmentIn, 26 0 : const char *segment_pointer ) 27 0 : : CPCIDSKSegment( fileIn, segmentIn, segment_pointer ) 28 : 29 : { 30 0 : } 31 : 32 : /************************************************************************/ 33 : /* ~CPCIDSK_TEX() */ 34 : /************************************************************************/ 35 : 36 0 : CPCIDSK_TEX::~CPCIDSK_TEX() 37 : 38 : { 39 0 : } 40 : 41 : /************************************************************************/ 42 : /* ReadText() */ 43 : /************************************************************************/ 44 : 45 0 : std::string CPCIDSK_TEX::ReadText() 46 : 47 : { 48 0 : PCIDSKBuffer seg_data; 49 : 50 0 : seg_data.SetSize( (int) GetContentSize() ); 51 : 52 0 : ReadFromFile( seg_data.buffer, 0, seg_data.buffer_size ); 53 : 54 : int i; 55 0 : char *tbuffer = (char *) seg_data.buffer; 56 : 57 0 : for( i = 0; i < seg_data.buffer_size; i++ ) 58 : { 59 0 : if( tbuffer[i] == '\r' ) 60 0 : tbuffer[i] = '\n'; 61 : 62 0 : if( tbuffer[i] == '\0' ) 63 0 : break; 64 : } 65 : 66 0 : return std::string( (const char *) seg_data.buffer, i ); 67 : } 68 : 69 : /************************************************************************/ 70 : /* WriteText() */ 71 : /************************************************************************/ 72 : 73 0 : void CPCIDSK_TEX::WriteText( const std::string &text_in ) 74 : 75 : { 76 : // Transform all \n's to \r's (chr(10) to char(13)). 77 0 : unsigned int i, i_out = 0; 78 0 : std::string text = text_in; 79 : 80 0 : for( i = 0; i < text.size(); i++ ) 81 : { 82 0 : if( text[i] == '\0' ) 83 : { 84 0 : text.resize( i ); 85 0 : break; 86 : } 87 : 88 0 : if( text[i] == '\n' && text[i+1] == '\r' ) 89 : { 90 0 : text[i_out++] = '\r'; 91 0 : i++; 92 : } 93 0 : else if( text[i] == '\r' && text[i+1] == '\n' ) 94 : { 95 0 : text[i_out++] = '\r'; 96 0 : i++; 97 : } 98 0 : else if( text[i] == '\n' ) 99 0 : text[i_out++] = '\r'; 100 : else 101 0 : text[i_out++] = text[i]; 102 : } 103 : 104 0 : text.resize( i_out ); 105 : 106 : // make sure we have a newline at the end. 107 : 108 0 : if( i_out > 0 && text[i_out-1] != '\r' ) 109 0 : text += "\r"; 110 : 111 : // We really *ought* to ensure the rest of the segment 112 : // is zeroed out to properly adhere to the specification. 113 : // It might also be prudent to ensure the segment grows 114 : // in 32K increments to avoid "move to end of file churn" 115 : // if several text segments are growing a bit at a time 116 : // though this is uncommon. 117 : 118 0 : WriteToFile( text.c_str(), 0, text.size() + 1 ); 119 0 : }