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