summaryrefslogtreecommitdiffstats
path: root/conversion/text.cc
blob: 82ec81bbcec589841d7c63dbf605f6c2fe8bafd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/

#include <iostream>
#include <fstream>

#include "text.h"
#include "nitf.h"
#include "utilities.h"

using namespace std;
using namespace RasNITF;
	
RasNITF::text::text()
{
    m_text_data = NULL;
    m_txshd = NULL;
}

RasNITF::text::~text()
{
    if(m_text_data != NULL)
	delete m_text_data;
    m_text_data = NULL;
    
    if(m_txshd != NULL)
	delete m_txshd;
    m_txshd = NULL;

}
		
int RasNITF::text::read_file(istream &hNITF, long th_length, long text_length)
{
    int charsread = 0;
    long starting_position = 0;
	
    header_length=th_length;	
    starting_position = hNITF.tellg();
    charsread += read_verify2(hNITF, m_te, 2 + 7 + 3 + 14 + 80 + 1 + 2 + 11 + 2 + 20 + 2 + 8+ 4+ 1+ 8+ 43 + 1 + 40 + 1 + 8+ 15+ 1+ 3);
	
    // read txshdl

    charsread += read_verify2(hNITF, m_txshdl, 5);
    n_txshdl = charptrtolong(m_txshdl, 5);
    
    if (n_txshdl > 0) {
	n_txshdl -= 3;
	charsread += read_verify2(hNITF, m_txsofl, 3);
	m_txshd = new char[n_txshdl];
	if(m_txshd == NULL) cerr<<"ERROR: could not allocate memory";
	charsread += read_verify2(hNITF, m_txshd, n_txshdl);
	n_txshdl += 3;
	
    } 

    // check if we are where we should be
    if((long)(hNITF.tellg()) != starting_position + th_length) exit(2);
    
    // store the text data
    
    m_text_data = new char[text_length];
    if(m_text_data == NULL) cerr<<"ERROR: could not allocate memory";
    charsread += read_verify2(hNITF, m_text_data, text_length);
    data_length = text_length;
    
    return charsread;
} 

int RasNITF::text::write_file(ofstream &fNITF)
{

    fNITF.write(m_te, 2); 
    fNITF.write(m_textid, 7);
    fNITF.write(m_txtalvl, 3);	
    fNITF.write(m_txtdt, 14);
    fNITF.write(m_txtitl, 80);
    fNITF.write(m_tsclas, 1);
    fNITF.write(m_tsclsy, 2);
    fNITF.write(m_tscode, 11);
    fNITF.write(m_tsctlh, 2);
    fNITF.write(m_tsrel, 20);
    fNITF.write(m_tsdctp, 2);
    fNITF.write(m_tsdcdt, 8);	
    fNITF.write(m_tsdcxm, 4);
    fNITF.write(m_tsdg, 1);
    fNITF.write(m_tsdgdt, 8);	
    fNITF.write(m_tscltx, 43);
    fNITF.write(m_tscatp, 1);
    fNITF.write(m_tscaut, 40);
    fNITF.write(m_tscrsn, 1);
    fNITF.write(m_tssrdt, 8);	
    fNITF.write(m_tsctln, 15);
    fNITF.write(m_encryp, 1);
    fNITF.write(m_txtfmt, 3);
    
    fNITF.write(m_txshdl, 5);
    
    if (n_txshdl > 0) {
	fNITF.write(m_txsofl, 3);
	fNITF.write(m_txshd, n_txshdl - 3);
    }
    
    if( m_text_data != NULL) { 
	fNITF.write( m_text_data, data_length);
    }
    
    //TODO:
    return 0;
    
}

string RasNITF::text::get_lt() const {
   return text_dl;
}

string RasNITF::text::get_ltsh() const {
   return text_hl;
}