⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aportis.cxx

📁 一个小的非常好用的电子书阅读器源码
💻 CXX
字号:
#include <stdio.h>#include <string.h>#include "Aportis.h"Aportis::Aportis() : fin(NULL) { /*printf("constructing:%x\n",fin);*/ }CList<Bkmk>* Aportis::getbkmklist(){  if (bCompressed != 4) return NULL;  CList<Bkmk>* t = new CList<Bkmk>;  size_t cur = ftell(fin);  for (int i = 0; i < nRecs2; i++)    {      DWORD dwPos;      fseek(fin, 0x56 + 8*i, SEEK_SET);      fread(&dwPos, 4, 1, fin);      dwPos = SwapLong(dwPos);      fseek(fin,dwPos,SEEK_SET);      unsigned char ch;      fread(&ch,1,1,fin);      if (ch != 241)      {	char name[17];	name[16] = '\0';	fseek(fin,dwPos,SEEK_SET);	fread(name,1,16,fin);	unsigned long lcn;	fread(&lcn,sizeof(lcn),1,fin);	lcn = SwapLong(lcn);	t->push_back(Bkmk(name,lcn));      }    }  fseek(fin, cur, SEEK_SET);  return t;}int Aportis::openfile(char *src){  //  printf("In openfile\n");  int ret = 0;  //  printf("closing fin:%x\n",fin);  if (fin != NULL) fclose(fin);  tDocHeader head;  //  printf("opening fin\n");  fin = fopen(src,"rb");  if (fin==0)    {      return -1;    }  // just holds the first few chars of the file  //	char buf[0x100];  fread(&head, 1, sizeof(head), fin);  if (head.dwType != 0x64414552 //   'dAER'      || head.dwCreator != 0x74584554) // 'tXET')    {      return -2;    }  // point to start of index  fseek(fin, 0x4E, SEEK_SET);  // read the location of the first record  DWORD dwPos;  fread(&dwPos, 4, 1, fin);  dwPos = SwapLong(dwPos);  fseek(fin, dwPos+1, SEEK_SET);  fread(&bCompressed, 1, 1, fin);  if (bCompressed!=1 && bCompressed!=2 && bCompressed != 4) {    ret = bCompressed;    bCompressed = 2;  }	  fseek(fin,0,SEEK_END);  dwLen = ftell(fin);  nRecs2 = nRecs = SwapWord(head.wNumRecs) - 1;  switch (bCompressed)    {    case 4:      {	dwTLen = 0;	int i;	for (i = 0; i < nRecs; i++)	  {	    unsigned int bs = GetBS(i);	    if (bs == 0) break;	    else dwTLen += bs;	  }	nRecs = i;	BlockSize = 0;      }      break;    case 1:    case 2:    default:      fseek(fin,0x56+8*nRecs+6,SEEK_SET);      fread(&dwTLen, 4, 1, fin);      dwTLen = SwapLong(dwTLen);      fseek(fin,0x56+8*nRecs+12,SEEK_SET);      fread(&BlockSize, 2, 1, fin);      BlockSize = SwapWord(BlockSize);      if (BlockSize == 0)	{	  BlockSize = 4096;	  printf("WARNING: Blocksize not set in source file\n");	}    }  // this is the main record buffer  // it knows how to stretch to accomodate the decompress  currentrec = 0;  cbptr = 0;  outptr = 0;  refreshbuffer();  return ret;}int Aportis::getch(){  if (bCompressed == 1)    {      if ((dwRecLen == 0) && !refreshbuffer()) return EOF;      else	{	  int c = getc(fin);	  dwRecLen--;	  currentpos++;	  return c;	}    }  if (outptr != cbptr)    {      currentpos++;      return (circbuf[outptr = (outptr + 1) % 2048]);    }  if ((dwRecLen == 0) && !refreshbuffer()) return EOF;  currentpos++;  unsigned int c;  // take a char from the input buffer  c = getc(fin);  dwRecLen--;  // separate the char into zones: 0, 1...8, 9...0x7F, 0x80...0xBF, 0xC0...0xFF  // codes 1...8 mean copy that many chars; for accented chars & binary  if (c == 0)    {      circbuf[outptr = cbptr = (cbptr+1)%2048] = c;      return c;    }  else if (c >= 0x09 && c <= 0x7F)    {      circbuf[outptr = cbptr = (cbptr+1)%2048] = c;      return c;    }  else if (c >= 0x01 && c <= 0x08)    {      dwRecLen -= c;      while(c--)	{	  circbuf[cbptr = (cbptr+1)%2048] = getc(fin);	}      return circbuf[outptr = (outptr+1)%2048];    }  else if (c >= 0x80 && c <= 0xBF)    {      int m,n;      c <<= 8;      c += getc(fin);      dwRecLen--;      m = (c & 0x3FFF) >> COUNT_BITS;      n = c & ((1<<COUNT_BITS) - 1);      n += 3;      while (n--)	{	  cbptr = (cbptr+1)%2048;	  circbuf[cbptr] = circbuf[(cbptr+2048-m)%2048];	}      return circbuf[outptr = (outptr+1)%2048];    }  else if (c >= 0xC0 && c <= 0xFF)    {      circbuf[cbptr = (cbptr+1)%2048] = ' ';      circbuf[cbptr = (cbptr+1)%2048] = c^0x80;      return circbuf[outptr = (outptr+1)%2048];    }}unsigned int Aportis::GetBS(unsigned int bn){  DWORD dwPos;  WORD fs;  fseek(fin, 0x56 + 8*bn, SEEK_SET);  fread(&dwPos, 4, 1, fin);  dwPos = SwapLong(dwPos);  fseek(fin,dwPos,SEEK_SET);  unsigned char ch;  fread(&ch,1,1,fin);  if (ch == 241)    {      fread(&fs,sizeof(fs),1,fin);      fs = SwapWord(fs);    }  else    fs = 0;  return fs;}unsigned int Aportis::locate(){  if (bCompressed == 4)    {      size_t cur = ftell(fin);      unsigned int clen = 0;      for (unsigned int i = 0; i < currentrec-1; i++)	{	  unsigned int bs = GetBS(i);	  if (bs == 0) break;	  clen += bs;	}      fseek(fin,cur,SEEK_SET);      return clen+currentpos;    }  else    return (currentrec-1)*BlockSize+currentpos;}void Aportis::locate(unsigned int n){  unsigned int offset;  //  currentrec = (n >> OFFBITS);  switch (bCompressed)    {    case 4:      {	DWORD clen = 0;	offset = n;	unsigned int i;	for (i = 0; i < nRecs; i++)	  {	    unsigned int bs = GetBS(i);	    if (bs == 0) break;	    clen += bs;	    if (clen > n) break;	    offset = n - clen;	  }	currentrec = i;      }      break;    case 1:    case 2:    default:      currentrec = n / BlockSize;      offset = n % BlockSize;    }  outptr = cbptr;  refreshbuffer();  while (currentpos < offset && getch() != EOF);}bool Aportis::refreshbuffer(){  if (currentrec < nRecs)    {      // read the record offset      fseek(fin, 0x56 + 8*currentrec, SEEK_SET);      DWORD dwPos;      fread(&dwPos, 4, 1, fin);      dwPos = SwapLong(dwPos);      // read start of next record      fseek(fin, 0x5E + 8*currentrec, SEEK_SET);      fread(&dwRecLen, 4, 1, fin);      dwRecLen = SwapLong(dwRecLen);      // for the last, use the file len      if (currentrec==nRecs2-1) dwRecLen = dwLen;      dwRecLen -= dwPos;      fseek(fin,dwPos,SEEK_SET);      if (bCompressed == 4)	{	  unsigned char t[3];	  fread(t,1,3,fin);	  if (t[0] != 241)	    {	      printf("You shouldn't be here!\n");	      return false;	    }	  dwRecLen -= 3;	}      /*	int n = fread(t.buf, 1, dwRecLen, fin);	t.len = n;	//		if(bCompressed)	t.Decompress();	t.buf[t.Len()] = '\0';      */      currentpos = 0;      currentrec++;      return true;    }  else {    return false;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -