OpenCPN Partial API docs
Loading...
Searching...
No Matches
TCDataSource.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 *
5 ***************************************************************************
6 * Copyright (C) 2013 by David S. Register *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 **************************************************************************/
23
24#include <wx/log.h>
25#include <wx/filename.h>
26
27#include "TCDataSource.h"
28#include "TCDS_Ascii_Harmonic.h"
29#include "TCDS_Binary_Harmonic.h"
30
31#include <wx/arrimpl.cpp>
32WX_DEFINE_OBJARRAY(ArrayOfTCDSources);
33
34TCDataSource::TCDataSource() {
35 m_pfactory = NULL;
36 pTCDS_Ascii_Harmonic = NULL;
37 pTCDS_Binary_Harmonic = NULL;
38}
39
40TCDataSource::~TCDataSource() {
41 wxLogMessage(_T("UnLoading Tide/Current data source: %s"),
42 m_data_source_path.c_str());
43
44 delete pTCDS_Ascii_Harmonic;
45 delete pTCDS_Binary_Harmonic;
46}
47
48TC_Error_Code TCDataSource::LoadData(const wxString &data_file_path) {
49 m_data_source_path = data_file_path;
50 wxLogMessage(_T("Loading Tide/Current data source: %s"),
51 m_data_source_path.c_str());
52
53 wxFileName fname(data_file_path);
54
55 if (!fname.FileExists()) return TC_FILE_NOT_FOUND;
56
57 if (fname.GetExt() == _T("IDX") || fname.GetExt() == _T("idx")) {
59 m_pfactory = dynamic_cast<TCDataFactory *>(pdata);
60 pTCDS_Ascii_Harmonic = pdata;
61 } else if (fname.GetExt() == _T("tcd") || fname.GetExt() == _T("TCD")) {
63 m_pfactory = dynamic_cast<TCDataFactory *>(pdata);
64 pTCDS_Binary_Harmonic = pdata;
65 }
66
67 TC_Error_Code err_code;
68 if (m_pfactory) {
69 err_code = m_pfactory->LoadData(data_file_path);
70 if (err_code != TC_NO_ERROR) {
71 wxLogMessage("Error loading tide/current data.");
72 return err_code;
73 }
74
75 // Mark the index entries individually with owner
76 unsigned int max_index = GetMaxIndex();
77 for (unsigned int i = 0; i < max_index; i++) {
78 IDX_entry *pIDX = GetIndexEntry(i);
79 if (pIDX) {
80 pIDX->pDataSource = this;
81 strncpy(pIDX->source_ident, m_data_source_path.mb_str(),
82 MAXNAMELEN - 1);
83 pIDX->source_ident[MAXNAMELEN - 1] = '\0';
84 }
85 }
86 } else
87 err_code = TC_FILE_NOT_FOUND;
88
89 return err_code;
90}
91
92int TCDataSource::GetMaxIndex(void) {
93 if (m_pfactory)
94 return m_pfactory->GetMaxIndex();
95 else
96 return 0;
97}
98
99IDX_entry *TCDataSource::GetIndexEntry(int n_index) {
100 if (m_pfactory) {
101 if (n_index < m_pfactory->GetMaxIndex())
102 return m_pfactory->GetIndexEntry(n_index);
103 else
104 return NULL;
105 } else
106 return NULL;
107}
108
109TC_Error_Code TCDataSource::LoadHarmonicData(IDX_entry *pIDX) {
110 switch (pIDX->source_data_type) {
111 case SOURCE_TYPE_ASCII_HARMONIC:
112 return pTCDS_Ascii_Harmonic->LoadHarmonicData(pIDX);
113 break;
114
115 case SOURCE_TYPE_BINARY_HARMONIC:
116 return pTCDS_Binary_Harmonic->LoadHarmonicData(pIDX);
117 break;
118
119 default:
120 return TC_GENERIC_ERROR;
121 }
122}
Represents an index entry for tidal and current data.
Definition IDX_entry.h:49
TCDataSource * pDataSource
Pointer to the associated data source.
Definition IDX_entry.h:56
char source_ident[MAXNAMELEN]
Identifier of the source (typically file name)
Definition IDX_entry.h:57
source_data_t source_data_type
Format of the source data (ASCII or binary)
Definition IDX_entry.h:55