kviewshell
DjVuNavDir.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifdef HAVE_CONFIG_H
00058 # include "config.h"
00059 #endif
00060 #if NEED_GNUG_PRAGMAS
00061 # pragma implementation
00062 #endif
00063
00064 #include "DjVuNavDir.h"
00065 #include "debug.h"
00066 #include "GException.h"
00067 #include "GOS.h"
00068 #include "ByteStream.h"
00069 #include "GURL.h"
00070 #include <ctype.h>
00071
00072
00073
00074 #ifdef HAVE_NAMESPACES
00075 namespace DJVU {
00076 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00077 }
00078 #endif
00079 #endif
00080
00081
00082
00083 DjVuNavDir::DjVuNavDir(const GURL &dirURL)
00084 {
00085 if (!dirURL) G_THROW( ERR_MSG("DjVuNavDir.zero_dir") );
00086 baseURL=dirURL.base();
00087 }
00088
00089 DjVuNavDir::DjVuNavDir(ByteStream & str, const GURL &dirURL)
00090 {
00091 if (!dirURL) G_THROW( ERR_MSG("DjVuNavDir.zero_dir") );
00092
00093 baseURL=GURL(dirURL).base();
00094
00095 decode(str);
00096 }
00097
00098 void
00099 DjVuNavDir::decode(ByteStream & str)
00100 {
00101 GCriticalSectionLock lk(&lock);
00102
00103 GList<GUTF8String> tmp_page2name;
00104 int eof=0;
00105 while(!eof)
00106 {
00107 char buffer[1024];
00108 char * ptr;
00109 for(ptr=buffer;ptr-buffer<1024;ptr++)
00110 if ((eof=!str.read(ptr, 1)) || *ptr=='\n') break;
00111 if (ptr-buffer==1024) G_THROW( ERR_MSG("DjVuNavDir.long_line") );
00112 *ptr=0;
00113 if (!strlen(buffer)) continue;
00114
00115 if (!tmp_page2name.contains(buffer))
00116 tmp_page2name.append(buffer);
00117 };
00118
00119
00120 int pages=tmp_page2name.size();
00121 page2name.resize(pages-1);
00122
00123 int cnt;
00124 GPosition pos;
00125 for(pos=tmp_page2name, cnt=0;pos;++pos, cnt++)
00126 page2name[cnt]=tmp_page2name[pos];
00127
00128
00129 for(cnt=0;cnt<pages;cnt++)
00130 {
00131 name2page[page2name[cnt]]=cnt;
00132 url2page[GURL::UTF8(page2name[cnt],baseURL)]=cnt;
00133 }
00134 }
00135
00136 #ifndef NEED_DECODER_ONLY
00137 void
00138 DjVuNavDir::encode(ByteStream & str)
00139 {
00140 GCriticalSectionLock lk(&lock);
00141
00142 for(int i=0;i<page2name.size();i++)
00143 {
00144 GUTF8String & name=page2name[i];
00145 str.writall((const char*)name, name.length());
00146 str.writall("\n", 1);
00147 };
00148 }
00149 #endif //NEED_DECODER_ONLY
00150
00151 int
00152 DjVuNavDir::get_pages_num(void) const
00153 {
00154 GCriticalSectionLock lk((GCriticalSection *)&lock);
00155
00156 return page2name.size();
00157 }
00158
00159 int
00160 DjVuNavDir::name_to_page(const char * name) const
00161 {
00162 GCriticalSectionLock lk((GCriticalSection *)&lock);
00163
00164 if (!name2page.contains(name)) return -1;
00165 return name2page[name];
00166 }
00167
00168 int
00169 DjVuNavDir::url_to_page(const GURL & url) const
00170 {
00171 GCriticalSectionLock lk((GCriticalSection *)&lock);
00172
00173 if (!url2page.contains(url)) return -1;
00174 return url2page[url];
00175 }
00176
00177 GUTF8String
00178 DjVuNavDir::page_to_name(int page) const
00179 {
00180 GCriticalSectionLock lk((GCriticalSection *)&lock);
00181
00182 if (page<0)
00183 G_THROW( ERR_MSG("DjVuNavDir.neg_page") );
00184 if (page>=page2name.size())
00185 G_THROW( ERR_MSG("DjVuNavDir.large_page") );
00186 return page2name[page];
00187 }
00188
00189 GURL
00190 DjVuNavDir::page_to_url(int page) const
00191 {
00192 GCriticalSectionLock lk((GCriticalSection *)&lock);
00193
00194 return GURL::UTF8(page_to_name(page),baseURL);
00195 }
00196
00197 void
00198 DjVuNavDir::insert_page(int where, const char * name)
00199 {
00200 GCriticalSectionLock lk((GCriticalSection *)&lock);
00201
00202 int pages=page2name.size();
00203 if (where<0) where=pages;
00204
00205 page2name.resize(pages);
00206 for(int i=pages;i>where;i--)
00207 page2name[i]=page2name[i-1];
00208 page2name[where]=name;
00209 name2page[name]=where;
00210 url2page[GURL::UTF8(name,baseURL)]=where;
00211 }
00212
00213 #ifndef NEED_DECODER_ONLY
00214 void
00215 DjVuNavDir::delete_page(int page_num)
00216 {
00217 GCriticalSectionLock lk((GCriticalSection *)&lock);
00218
00219 int pages=page2name.size();
00220
00221 if (page_num<0 || page_num>=pages)
00222 G_THROW( ERR_MSG("DjVuNavDir.bad_page") );
00223
00224 for(int i=page_num;i<pages-1;i++)
00225 page2name[i]=page2name[i+1];
00226 page2name.resize(--pages-1);
00227 }
00228 #endif
00229
00230
00231
00232 #ifdef HAVE_NAMESPACES
00233 }
00234 # ifndef NOT_USING_DJVU_NAMESPACE
00235 using namespace DJVU;
00236 # endif
00237 #endif