[r20]: cbrpager-maemo / branches / MAEMO_0_9_20 / src / garchivereader.c Maximize Restore History

Download this file

garchivereader.c    195 lines (175 with data), 4.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* garchivereader.c - routines to abstract archive reading logic
*
* Copyright (C) 2008 Benoit Goudreault-Emond (bgoudreaultemond@gmail.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <string.h>
#include <ctype.h>
#include <strings.h> /* for strcasecmp */
#include "garchivereader.h"
static void
g_archive_reader_base_init(gpointer g_class)
{
static gboolean initialized = FALSE;
if(!initialized)
initialized = TRUE;
}
GType
g_archive_reader_get_type(void)
{
static GType type = 0;
if(type)
return type;
static const GTypeInfo info = {
sizeof(GArchiveReaderClass),
g_archive_reader_base_init,
NULL,
NULL,
NULL,
NULL,
0,
0,
NULL
};
type = g_type_register_static(G_TYPE_INTERFACE,
"GArchiveReader",
&info,
0);
return type;
}
/* methods */
int
g_archive_reader_register_path(GArchiveReader* reader,
const gchar* path)
{
return G_ARCHIVE_READER_GET_CLASS(reader)->register_path(reader, path);
}
GArray*
g_archive_reader_get_file_list(GArchiveReader* reader)
{
return G_ARCHIVE_READER_GET_CLASS(reader)->get_file_list(reader);
}
GdkPixbuf*
g_archive_reader_get_page (GArchiveReader* reader,
int page_number,
gdouble* w,
gdouble* h)
{
return G_ARCHIVE_READER_GET_CLASS(reader)->get_page(reader,
page_number,
w,
h);
}
void
g_archive_reader_set_sort_strategy(GArchiveReader* reader,
SortStrategy strategy)
{
return G_ARCHIVE_READER_GET_CLASS(reader)->set_sort_strategy(reader,
strategy);
}
gchar*
g_archive_reader_get_last_error(GArchiveReader* reader)
{
return G_ARCHIVE_READER_GET_CLASS(reader)->get_last_error(reader);
}
int
g_archive_reader_check_magic (const gchar* path,
const magic_sign* magic)
{
FILE* f;
char sign[20];
int retval = 0;
if(!(f = fopen(path, "rb")))
{
return -1;
}
if(!fseek(f, magic->offs, SEEK_SET) &&
fread(sign, 1, magic->len, f) == magic->len)
{
/* If the read went well, we need to compare the characters */
/* strstr works here, but only if no \0's are in the string */
/* and if we first terminate the string read too */
sign[magic->len] = '\0';
if(!strcmp(sign, magic->sign))
{
fclose(f);
return 1;
}
}
fclose(f);
return 0;
}
static gint
compare_ascii(gconstpointer first, gconstpointer second)
{
return strcmp(*((const char**)first), *((const char**)second));
}
static gint
compare_insensitive(gconstpointer first, gconstpointer second)
{
return strcasecmp(*((const char**)first), *((const char**)second));
}
static gint
compare_natural(gconstpointer first, gconstpointer second)
{
const gchar* lhs = *(const gchar**)first;
const gchar* rhs = *(const gchar**)second;
int current_lhs_number = 0;
int current_rhs_number = 0;
while(*lhs || *rhs) {
char lhs_char = isalpha(*lhs) ? tolower(*lhs) : *lhs;
char rhs_char = isalpha(*rhs) ? tolower(*rhs) : *rhs;
int lhs_digit = isdigit(lhs_char);
int rhs_digit = isdigit(rhs_char);
if(!lhs_digit && !rhs_digit) {
if(lhs_char < rhs_char)
return -1;
else if(lhs_char > rhs_char)
return 1;
else {
if(lhs_char)
++lhs;
if(rhs_char)
++rhs;
}
} else {
if(lhs_digit) {
current_lhs_number = current_lhs_number * 10 + (lhs_char - '0');
++lhs;
}
if(rhs_digit) {
current_rhs_number = current_rhs_number * 10 + (rhs_char - '0');
++rhs;
}
}
}
/* if we are here, need to compare numbers */
return current_lhs_number - current_rhs_number;
}
GCompareFunc
g_archive_reader_translate_strategy(SortStrategy strategy)
{
static GCompareFunc functions[] = {
compare_ascii,
compare_insensitive,
compare_natural
};
g_return_val_if_fail(strategy >= 0, NULL);
g_return_val_if_fail(strategy < 3, NULL);
return functions[strategy];
}