~ubuntu-branches/ubuntu/maverick/apvlv/maverick

« back to all changes in this revision

Viewing changes to src/ApvlvInfo.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ritter
  • Date: 2010-04-07 11:13:08 UTC
  • mfrom: (1.3.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100407111308-6yozdlrcyv6bs5aq
Tags: 0.0.9.5-1
New upstream release (Closes: #576081, #576082)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* This file is part of the apvlv package
 
3
* Copyright (C) <2008>  <Alf>
 
4
*
 
5
* Contact: Alf <naihe2010@gmail.com>
 
6
*
 
7
* This program is free software; you can redistribute it and/or modify
 
8
* it under the terms of the GNU General Public License as published by
 
9
* the Free Software Foundation; either version 2 of the License, or
 
10
* (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
*
 
17
* You should have received a copy of the GNU General Public License along
 
18
* with this program; if not, write to the Free Software Foundation, Inc.,
 
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
*
 
21
*/
 
22
/* @CFILE ApvlvInfo.cpp 
 
23
*
 
24
*  Author: Alf <naihe2010@gmail.com>
 
25
*/
 
26
/* @date Created: 2010/02/23 15:00:42 Alf*/
 
27
 
 
28
#include "ApvlvInfo.hpp"
 
29
 
 
30
#include <stdlib.h>
 
31
#include <string.h>
 
32
 
 
33
#include <fstream>
 
34
#include <sstream>
 
35
 
 
36
namespace apvlv
 
37
{
 
38
  ApvlvInfo *gInfo = NULL;
 
39
 
 
40
    ApvlvInfo::ApvlvInfo (const char *filename)
 
41
  {
 
42
    mFileName = filename;
 
43
 
 
44
    mFileHead = NULL;
 
45
    mFileMax = 10;
 
46
 
 
47
    ifstream is (mFileName.c_str (), ios::in);
 
48
    if (is.is_open ())
 
49
      {
 
50
        string line;
 
51
        const char *p;
 
52
 
 
53
        while (getline (is, line))
 
54
          {
 
55
            p = line.c_str ();
 
56
 
 
57
            if (*p != '\''      /* the ' */
 
58
                || !isdigit (*(p + 1))) /* the digit */
 
59
              {
 
60
                continue;
 
61
              }
 
62
 
 
63
            ini_add_position (p);
 
64
          }
 
65
 
 
66
        mFileHead = g_slist_reverse (mFileHead);
 
67
 
 
68
        is.close ();
 
69
      }
 
70
  }
 
71
 
 
72
  ApvlvInfo::~ApvlvInfo ()
 
73
  {
 
74
    while (mFileHead != NULL)
 
75
      {
 
76
        infofile *fp = (infofile *) (mFileHead->data);
 
77
        delete fp;
 
78
        mFileHead = g_slist_next (mFileHead);
 
79
      }
 
80
    g_slist_free (mFileHead);
 
81
  }
 
82
 
 
83
  bool ApvlvInfo::update ()
 
84
  {
 
85
    ofstream os (mFileName.c_str (), ios::out);
 
86
    if (!os.is_open ())
 
87
      {
 
88
        return false;
 
89
      }
 
90
 
 
91
    int i;
 
92
    GSList *lfp;
 
93
    infofile *fp;
 
94
    for (i = 0, lfp = mFileHead;
 
95
         i < mFileMax && lfp != NULL; ++i, lfp = g_slist_next (lfp))
 
96
      {
 
97
        fp = (infofile *) (lfp->data);
 
98
        if (fp)
 
99
          {
 
100
            os << "'" << i << "\t";
 
101
            os << fp->page << "\t";
 
102
            os << fp->rate << "\t";
 
103
            os << fp->file << endl;
 
104
          }
 
105
      }
 
106
 
 
107
    os.close ();
 
108
    return true;
 
109
  }
 
110
 
 
111
  infofile *ApvlvInfo::file (int id)
 
112
  {
 
113
    infofile *fp = (infofile *) g_slist_nth_data (mFileHead, id);
 
114
    return fp;
 
115
  }
 
116
 
 
117
  infofile *ApvlvInfo::file (const char *filename)
 
118
  {
 
119
    GSList *lfp;
 
120
    infofile *fp;
 
121
 
 
122
    for (lfp = mFileHead; lfp != NULL; lfp = g_slist_next (lfp))
 
123
      {
 
124
        fp = (infofile *) (lfp->data);
 
125
        if (fp->file == filename)
 
126
          {
 
127
            break;
 
128
          }
 
129
      }
 
130
 
 
131
    if (lfp == NULL)
 
132
      {
 
133
        fp = new infofile;
 
134
        fp->page = 0;
 
135
        fp->rate = 0.0;
 
136
        fp->file = filename;
 
137
        mFileHead = g_slist_insert_before (mFileHead, mFileHead, fp);
 
138
      }
 
139
    else
 
140
      {
 
141
        mFileHead = g_slist_remove (mFileHead, fp);
 
142
        mFileHead = g_slist_insert_before (mFileHead, mFileHead, fp);
 
143
      }
 
144
 
 
145
    return fp;
 
146
  }
 
147
 
 
148
  bool ApvlvInfo::file (int page, double rate, const char *filename)
 
149
  {
 
150
    infofile *fp;
 
151
 
 
152
    fp = file (filename);
 
153
    if (fp == NULL)
 
154
      {
 
155
        return false;
 
156
      }
 
157
 
 
158
    fp->page = page;
 
159
    fp->rate = rate;
 
160
    update ();
 
161
 
 
162
    return true;
 
163
  }
 
164
 
 
165
  bool ApvlvInfo::ini_add_position (const char *str)
 
166
  {
 
167
    const char *p;
 
168
 
 
169
    p = strchr (str + 2, '\t'); /* Skip the ' and the digit */
 
170
    if (p == NULL)
 
171
      {
 
172
        return false;
 
173
      }
 
174
 
 
175
    while (*p != '\0' && !isdigit (*p))
 
176
      {
 
177
        p++;
 
178
      }
 
179
    int page = atoi (p);
 
180
 
 
181
    p = strchr (p, '\t');
 
182
    if (p == NULL)
 
183
      {
 
184
        return false;
 
185
      }
 
186
 
 
187
    while (*p != '\0' && !isdigit (*p))
 
188
      {
 
189
        p++;
 
190
      }
 
191
    double rate = atof (p);
 
192
 
 
193
    p = strchr (p, '\t');
 
194
    if (p == NULL)
 
195
      {
 
196
        return false;
 
197
      }
 
198
 
 
199
    while (*p != '\0' && isspace (*p))
 
200
      {
 
201
        p++;
 
202
      }
 
203
    if (*p == '\0')
 
204
      {
 
205
        return false;
 
206
      }
 
207
 
 
208
    infofile *fp = new infofile;
 
209
    fp->page = page;
 
210
    fp->rate = rate;
 
211
    fp->file = p;
 
212
    mFileHead = g_slist_insert_before (mFileHead, mFileHead, fp);
 
213
    return true;
 
214
  }
 
215
};