OpenCPN Partial API docs
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>
32 WX_DEFINE_OBJARRAY(ArrayOfTCDSources);
33 
34 TCDataSource::TCDataSource() {
35  m_pfactory = NULL;
36  pTCDS_Ascii_Harmonic = NULL;
37  pTCDS_Binary_Harmonic = NULL;
38 }
39 
40 TCDataSource::~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 
48 TC_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 
71  // Mark the index entries individually with owner
72  unsigned int max_index = GetMaxIndex();
73  for (unsigned int i = 0; i < max_index; i++) {
74  IDX_entry *pIDX = GetIndexEntry(i);
75  if (pIDX) {
76  pIDX->pDataSource = this;
77  strncpy(pIDX->source_ident, m_data_source_path.mb_str(),
78  MAXNAMELEN - 1);
79  pIDX->source_ident[MAXNAMELEN - 1] = '\0';
80  }
81  }
82  } else
83  err_code = TC_FILE_NOT_FOUND;
84 
85  return err_code;
86 }
87 
88 int TCDataSource::GetMaxIndex(void) {
89  if (m_pfactory)
90  return m_pfactory->GetMaxIndex();
91  else
92  return 0;
93 }
94 
95 IDX_entry *TCDataSource::GetIndexEntry(int n_index) {
96  if (m_pfactory) {
97  if (n_index < m_pfactory->GetMaxIndex())
98  return m_pfactory->GetIndexEntry(n_index);
99  else
100  return NULL;
101  } else
102  return NULL;
103 }
104 
105 TC_Error_Code TCDataSource::LoadHarmonicData(IDX_entry *pIDX) {
106  switch (pIDX->source_data_type) {
107  case SOURCE_TYPE_ASCII_HARMONIC:
108  return pTCDS_Ascii_Harmonic->LoadHarmonicData(pIDX);
109  break;
110 
111  case SOURCE_TYPE_BINARY_HARMONIC:
112  return pTCDS_Binary_Harmonic->LoadHarmonicData(pIDX);
113  break;
114 
115  default:
116  return TC_GENERIC_ERROR;
117  }
118 }
Definition: IDX_entry.h:41