Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / ezdop / src / host / hunter / src / samplelog.cc
1 /*
2  Copyright 2006 Johnathan Corgan.
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 version 2
6  as published by the Free Software Foundation.
7  
8  This software is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with GNU Radio; see the file COPYING.  If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street,
16  Boston, MA 02110-1301, USA.
17 */
18
19 // Application level includes
20 #include "samplelog.h"
21
22 // wxWidgets includes
23 #include <wx/file.h>
24 #include <wx/log.h>
25
26 // System level includes
27 #include <cmath>
28
29 SampleLog::SampleLog()
30 {
31     m_file = NULL;
32     m_save = -1;
33 }
34
35 void SampleLog::Load(wxString filename)
36 {
37     m_file = new wxFile(filename.c_str(), wxFile::read_write);
38     wxString line;
39             
40     if (m_file && m_file->IsOpened()) {
41         while (readline(line)) {
42             Sample sample(line); // can't use inline temporary in next line
43             Add(sample);      // Locking is handled in Add
44         }
45         
46         m_save = -1;
47     }
48 }
49
50 bool SampleLog::readline(wxString &line)
51 {
52     char ch;
53     size_t count;
54
55     line.Empty();
56     count = m_file->Read(&ch, 1);
57     while (count == 1 && ch != '\n') {
58         line.Append(ch);
59         count = m_file->Read(&ch, 1);
60     }
61
62     return !line.IsEmpty();
63 }
64
65 void SampleLog::Add(Sample &sample)
66 {
67     wxMutexLocker locker(m_mutex);
68     
69     if (m_save < 0)
70         m_save = m_samples.size();
71
72     m_samples.push_back(sample);
73     return;
74 }
75
76 bool SampleLog::Save(wxString &filename)
77 {
78     wxASSERT(!m_file);  // Save called with filename when it already exists is an error
79     wxLogError(_T("SampleLog::Save: called with %s when file already exists"), filename.c_str());
80     
81     m_filename = filename;
82     m_file = new wxFile(m_filename.c_str(), wxFile::write);
83
84     return Save();
85 }
86
87 bool SampleLog::Save()
88 {
89     wxASSERT(m_file);
90     if (m_save < 0)
91         return false;
92
93     wxMutexLocker locker(m_mutex);
94     
95     char str[256];
96     if (m_file && m_file->IsOpened()) {
97         for (int i = m_save; i < m_samples.size(); i++) {
98             m_samples[i].Dump(str, 255);
99             m_file->Write(str, strlen(str));
100             m_file->Write("\n", strlen("\n"));
101         }
102         m_save = -1;
103         return true;
104     }
105
106     return false;
107 }