00001
00042
#ifndef __LIBARC_H__
00043
#define __LIBARC_H__
00044
00045
#include <string>
00046
#include <exception>
00047
#include <iterator>
00048
00049
#include <netinet/in.h>
00050
#include <stddef.h>
00051
00052 namespace libarc {
00053
00055 static const int LIBARC_VERSION = 0x000201;
00057 static const char*
LIBARC_VERSION_STRING =
"0.2.1";
00058
00059
class Member_Iterator;
00060
class Media_Type;
00061
00063
00067 class Unsupported_ARC_Format_Exception :
public std::exception
00068 {
00069
public:
00071
00074 const char*
what() const throw()
00075 {
00076
return "Unsupported ARC File format";
00077 }
00078 };
00079
00081
00083 class ARC_File
00084 {
00085
public:
00087
00094
static ARC_File*
Create(
const char* arc_path);
00095
00097
void Destroy();
00098
00100
00104
virtual off_t
GetMemberCount(
const char* media_type = 0)
const = 0;
00105
00107
00115
virtual Member_Iterator*
GetMemberIterator(
const char* media_type = 0)
const = 0;
00116
00118
00126
virtual Member_Iterator* GetMemberIterator(
const Media_Type& media_type)
const = 0;
00127
00128
protected:
00129
virtual ~
ARC_File()
throw();
00130 };
00131
00133
00135 class ARC_Member
00136 {
00137
public:
00139
00142
virtual const Media_Type&
MediaType()
const = 0;
00144
00147
virtual const std::string&
URL()
const = 0;
00149
00152
virtual time_t
Date()
const = 0;
00154
00157
virtual in_addr_t
IPAddress()
const = 0;
00158
00160
00175
virtual const unsigned char*
GetData(off_t& length)
const = 0;
00176
00178
00184
virtual int HTTPStatus()
const = 0;
00185
00187
00190
virtual off_t
GetOffset()
const = 0;
00191
00193
00202
virtual off_t
GetRawSize()
const = 0;
00203
00205
00214
virtual off_t
GetSize()
const = 0;
00215
00217
00222
virtual void ReleaseData()
const = 0;
00223
00225
00234
virtual const std::string&
GetResponseHeader(
const char* name)
const = 0;
00235
00236
virtual ~
ARC_Member()
throw();
00237 };
00238
00240
00243 class Member_Iterator
00244 {
00245
public:
00247
void Destroy();
00249
virtual const ARC_Member*
Next() = 0;
00250
00251
protected:
00252
virtual ~
Member_Iterator();
00253 };
00254
00256
00260 class Media_Type
00261 {
00262
public:
00264
00270
Media_Type(
const char* mt = 0);
00271
00273
00278
Media_Type(
const Media_Type& mt);
00279
00287 const Media_Type&
operator=(
const char* mt)
00288 {
00289 parse_media_type(mt);
00290 }
00291
00293
00299 const Media_Type&
operator=(
const std::string& mt)
00300 {
00301 parse_media_type(mt.c_str());
00302 }
00303
00305
00308 const char*
Type()
const
00309
{
00310
return m_Type.c_str();
00311 }
00312
00314
00317 const char*
Subtype()
const
00318
{
00319
return m_Subtype.c_str();
00320 }
00321
00323
00331
bool compare(
const Media_Type& mt)
const;
00332
00333
private:
00334
void parse_media_type(
const char* mt);
00335
00336 std::string m_Type;
00337 std::string m_Subtype;
00338 };
00339
00341
00349 inline bool operator==(
const Media_Type& lhs,
const Media_Type& rhs)
00350 {
00351
return lhs.
compare(rhs);
00352 }
00353
00355
00362
template<
typename Function>
00363 void for_each(
Member_Iterator* iter, Function f)
00364 {
00365
const ARC_Member* m;
00366
while ((m = iter->
Next()) != 0) {
00367 f(m);
00368 }
00369 }
00370
00372
00384
template<
typename Predicate,
typename Function>
00385 void for_each_if(
Member_Iterator* iter, Predicate p, Function f)
00386 {
00387
const ARC_Member* m;
00388
while ((m = iter->
Next()) != 0) {
00389
if (p(m)) {
00390 f(m);
00391 }
00392 }
00393 }
00394
00396
00405
template<
typename Predicate>
00406 const ARC_Member*
find_if(
Member_Iterator* iter, Predicate p)
00407 {
00408
const ARC_Member* m;
00409
while ((m = iter->
Next()) != 0) {
00410
if (p(m)) {
00411
return m;
00412 }
00413 }
00414
return 0;
00415 }
00416
00418
00427
template<
typename Predicate>
00428 size_t
count_if(
Member_Iterator* iter, Predicate p)
00429 {
00430
const ARC_Member* m;
00431 size_t count = 0;
00432
00433
while ((m = iter->
Next()) != 0) {
00434
if (p(m)) {
00435 ++count;
00436 }
00437 }
00438
00439
return count;
00440 }
00441
00442 };
00443
00444
00445
#endif
00446
00447
00448
00449
00450
00451
00452