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
|
/* This file is part of indexlib.
* Copyright (C) 2005 Luís Pedro Coelho <[email protected]>
*
* Indexlib is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation and available as file
* GPL_V2 which is distributed along with indexlib.
*
* Indexlib 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of this program with any edition of
* the TQt library by Trolltech AS, Norway (or with modified versions
* of TQt that use the same license as TQt), and distribute linked
* combinations including the two. You must obey the GNU General
* Public License in all respects for all of the code used other than
* TQt. If you modify this file, you may extend this exception to
* your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from
* your version.
*/
#include "create.h"
#include "quotes.h"
#include "path.h"
#include "version.h"
#include <fstream>
#include <unistd.h>
namespace {
indexlib::index_type::type type_of( const char* basename ) {
std::ifstream info( path_concat( basename, "info" ).c_str() );
if ( !info ) return indexlib::index_type::none;
std::string type;
std::string marker;
std::string ver;
int major, minor;
char sep;
std::getline( info, marker );
info >> ver >> major >> sep >> minor;
info >> type;
if ( !info ) return indexlib::index_type::none;
if ( type == "quotes" ) return indexlib::index_type::quotes;
if ( type == "ifile" ) return indexlib::index_type::ifile;
return indexlib::index_type::none;
}
}
std::auto_ptr<indexlib::index> indexlib::create( const char* basename, indexlib::index_type::type flags ) {
using namespace indexlib::version;
if ( type_of( basename ) != indexlib::index_type::none ) return std::auto_ptr<indexlib::index>( 0 );
try {
if ( basename[ strlen( basename ) - 1 ] == '/' && !isdir( basename ) ) {
if ( !indexlib::detail::mkdir_trailing( basename ) ) return std::auto_ptr<indexlib::index>( 0 );
}
std::ofstream info( path_concat( basename, "info" ).c_str() );
info << marker << std::endl;
info << "version " << major << '.' << minor << "\n";
if ( flags == index_type::quotes ) {
info << "quotes" << std::endl;
return std::auto_ptr<indexlib::index>( new quotes( basename ) );
}
if ( flags == index_type::ifile ) {
info << "ifile" << std::endl;
return std::auto_ptr<indexlib::index>( new ifile( basename ) );
}
} catch ( const std::exception& e ) {
std::cerr << "index creation failed: " << e.what() << std::endl;
}
return std::auto_ptr<indexlib::index>( 0 );
}
std::auto_ptr<indexlib::index> indexlib::open( const char* basename, unsigned flags ) {
using namespace indexlib;
switch ( type_of( basename ) ) {
case index_type::ifile: return std::auto_ptr<indexlib::index>( new ifile( basename ) );
case index_type::quotes: return std::auto_ptr<indexlib::index>( new quotes( basename ) );
case index_type::none:
if ( flags == open_flags::fail_if_nonexistant ) return std::auto_ptr<indexlib::index>();
return create( basename, index_type::type( flags ) );
}
logfile() << format( "%s:%s: Unexpected code reached!\n" ) % __FILE__ % __LINE__;
return std::auto_ptr<indexlib::index>( 0 );
}
bool indexlib::exists( const char* basename ) {
return basename && ( type_of( basename ) == indexlib::index_type::none );
}
void indexlib::remove( const char* basename ) {
assert( basename );
if ( !basename ) return;
using namespace indexlib;
switch ( type_of( basename ) ) {
case index_type::ifile: ifile::remove( basename );
break;
case index_type::quotes: quotes::remove( basename );
break;
case index_type::none: /* do nothing */;
}
::unlink( path_concat( basename, "info" ).c_str() );
}
|