Beispiel #1
0
static void
writeText( ByteStream & str_out,
            const GUTF8String &textUTF8,
            const DjVuTXT::Zone &zone,
            const int WindowHeight )
{
//  DEBUG_MSG( "--zonetype=" << zone.ztype << "\n" );

  const GUTF8String xindent(indent( 2 * zone.ztype + 2 ));
  GPosition pos=zone.children;
  // Build attribute string
  if( ! pos )
  {
    GUTF8String coords;
    coords.format("coords=\"%d,%d,%d,%d\"",
      zone.rect.xmin, WindowHeight - 1 - zone.rect.ymin,
      zone.rect.xmax, WindowHeight - 1 - zone.rect.ymax);
    const int start=zone.text_start;
    const int end=textUTF8.firstEndSpace(start,zone.text_length);
    str_out.writestring(start_tag(zone.ztype,coords));
    str_out.writestring(textUTF8.substr(start,end-start).toEscaped());
    str_out.writestring(end_tag(zone.ztype));
  } else
  {
    writeText(str_out,textUTF8,zone.ztype,zone.children,WindowHeight);
  }
}
static void
display_chunks(ByteStream & out_str, IFFByteStream &iff,
	       const GUTF8String &head, DjVmInfo djvminfo)
{
  size_t size;
  GUTF8String id, fullid;
  GUTF8String head2 = head + "  ";
  GPMap<int,DjVmDir::File> djvmmap;
  int rawoffset;
  GMap<GUTF8String, int> counters;
  
  while ((size = iff.get_chunk(id, &rawoffset)))
  {
    if (!counters.contains(id)) counters[id]=0;
    else counters[id]++;
    
    GUTF8String msg;
    msg.format("%s%s [%d] ", (const char *)head, (const char *)id, size);
    out_str.format( "%s", (const char *)msg);
    // Display DJVM is when adequate
    if (djvminfo.dir)
    {
      GP<DjVmDir::File> rec = djvminfo.map[rawoffset];
      if (rec)
        {
          GUTF8String id = rec->get_load_name();
          GUTF8String title = rec->get_title();
          out_str.format( "{%s}", (const char*) id);
          if (rec->is_include())
            out_str.format(" [I]");
          if (rec->is_thumbnails())
            out_str.format(" [T]");
          if (rec->is_shared_anno())
            out_str.format(" [S]");
          if (rec->is_page())
            out_str.format(" [P%d]", rec->get_page_num()+1);
          if (id != title)
            out_str.format(" (%s)", (const char*)title);
        }
    }
    // Test chunk type
    iff.full_id(fullid);
    for (int i=0; disproutines[i].id; i++)
      if (fullid == disproutines[i].id || id == disproutines[i].id)
      {
        int n = msg.length();
        while (n++ < 14+(int) head.length()) putchar(out_str, ' ');
        if (!iff.composite()) out_str.format( "    ");
        (*disproutines[i].subr)(out_str, iff, head2,
                                size, djvminfo, counters[id]);
        break;
      }
      // Default display of composite chunk
      out_str.format( "\n");
      if (iff.composite())
        display_chunks(out_str, iff, head2, djvminfo);
      // Terminate
      iff.close_chunk();
  }
}
GURL 
getDjViewDataFile(const char *fname)
{
  GList<GURL> paths = DjVuMessage::GetProfilePaths();
  GUTF8String file = fname;

  // end hack alert
  static const char *osi = "/osi/";
  static const char *djview3 = "/djview3/";
  for (GPosition pos=paths; pos; ++pos)
    {
      GURL url = GURL(file, paths[pos]);
      GUTF8String urls = (const char*)url;
      int pos = urls.search(osi);
      if (pos >= 0)
        {
          GUTF8String urlx;
          urlx += urls.substr(0, pos);
          urlx += djview3;
          urlx += urls.substr(pos+strlen(osi), -1);
          GURL url = GURL::UTF8(urlx);
          if (url.is_file())
            return url;
        }
    }
  // end hack alert
  for (GPosition pos=paths; pos; ++pos)
    {
      GURL url = GURL(file, paths[pos]);
      if(url.is_file())
        return url;
    }
  return GURL();
}
Beispiel #4
0
// cwd([dirname])
// -- changes directory to dirname (when specified).
//    returns the full path name of the current directory. 
GUTF8String 
GOS::cwd(const GUTF8String &dirname)
{
#if defined(UNIX) || defined(macintosh) || defined(OS2)
  if (dirname.length() && chdir(dirname.getUTF82Native())==-1)//MBCS cvt
    G_THROW(errmsg());
  char *string_buffer;
  GPBuffer<char> gstring_buffer(string_buffer,MAXPATHLEN+1);
  char *result = getcwd(string_buffer,MAXPATHLEN);
  if (!result)
    G_THROW(errmsg());
  return GNativeString(result).getNative2UTF8();//MBCS cvt
#elif defined (WIN32)
  char drv[2];
  if (dirname.length() && _chdir(dirname.getUTF82Native())==-1)//MBCS cvt
    G_THROW(errmsg());
  drv[0]= dot ; drv[1]=0;
  char *string_buffer;
  GPBuffer<char> gstring_buffer(string_buffer,MAXPATHLEN+1);
  char *result = getcwd(string_buffer,MAXPATHLEN);
  GetFullPathName(drv, MAXPATHLEN, string_buffer, &result);
  return GNativeString(string_buffer).getNative2UTF8();//MBCS cvt
#else
#error "Define something here for your operating system"
#endif 
}
void
GIFFChunk::set_name(GUTF8String name)
{
  DEBUG_MSG("GIFFChunk::set_name(): name='" << name << "'\n");
  DEBUG_MAKE_INDENT(3);

  const int colon=name.search(':');
  if(colon>=0)
  {
    type=name.substr(0,colon);
    name=name.substr(colon+1,(unsigned int)-1);
    if(name.search(':')>=0)
      G_THROW( ERR_MSG("GIFFManager.one_colon") );
  }

  DEBUG_MSG("auto-setting type to '" << type << "'\n");

  if (name.contains(".[]")>=0)
    G_THROW( ERR_MSG("GIFFManager.bad_char") );

  strncpy(GIFFChunk::name, (const char *)name, 4);
  GIFFChunk::name[4]=0;
  for(int i=strlen(GIFFChunk::name);i<4;i++)
    GIFFChunk::name[i]=' ';
}
Beispiel #6
0
GUTF8String 
GUTF8String::create( void const * const buf,
  unsigned int size, const EncodeType encodetype )
{
  GUTF8String retval;
  retval.init(GStringRep::Unicode::create(buf,size,encodetype));
  return retval;
}
Beispiel #7
0
GUTF8String 
GUTF8String::create( void const * const buf,
  const unsigned int size, const GP<GStringRep::Unicode> &remainder)
{
  GUTF8String retval;
  retval.init(GStringRep::Unicode::create(buf,size,remainder));
  return retval;
}
Beispiel #8
0
GUTF8String 
GUTF8String::create( void const * const buf,
  const unsigned int size, const GUTF8String &encoding )
{
  GUTF8String retval;
  retval.init(GStringRep::Unicode::create(buf,size,encoding ));
  return retval;
}
//  A C function to perform a message lookup. Arguments are a buffer to received the
//  translated message, a buffer size (bytes), and a message_list. The translated
//  result is returned in msg_buffer encoded in UTF-8. In case of error, msg_buffer is
//  empty (i.e., msg_buffer[0] == '\0').
void 
DjVuMessageLite_LookUp( char *msg_buffer, const unsigned int buffer_size, const char *message )
{
  GUTF8String converted = DjVuMessageLite::LookUpUTF8( message );
  if( converted.length() >= buffer_size )
    msg_buffer[0] = '\0';
  else
    strcpy( msg_buffer, converted );
}
GUTF8String DocSettings::GetXML(bool skip_view_settings) const
{
	GUTF8String result;

	CString strHead;
	if (skip_view_settings)
	{
		strHead.Format(_T("<%s>\n"), pszTagContent);
	}
	else
	{
		strHead.Format(_T("<%s %s=\"%d\" %s=\"%d\" %s=\"%d\" %s=\"%d\" %s=\"%.2lf%%\"")
				_T(" %s=\"%d\" %s=\"%d\" %s=\"%d\" %s=\"%d\" %s=\"%d\" %s=\"%d\">\n"),
				pszTagSettings, pszAttrStartPage, nPage, pszAttrOffsetX, ptOffset.x,
				pszAttrOffsetY, ptOffset.y, pszAttrZoomType, nZoomType, pszAttrZoom, fZoom,
				pszAttrLayout, nLayout, pszAttrFirstPage, static_cast<int>(bFirstPageAlone),
				pszAttrRightToLeft, static_cast<int>(bRightToLeft),
				pszAttrDisplayMode, nDisplayMode, pszAttrRotate, nRotate,
				pszAttrOpenSidebarTab, nOpenSidebarTab);
	}

	result += MakeUTF8String(strHead);

	if (!bookmarks.empty())
	{
		result += MakeUTF8String(FormatString(_T("<%s>\n"), pszTagBookmarks));
		for (list<Bookmark>::const_iterator it = bookmarks.begin(); it != bookmarks.end(); ++it)
			result += (*it).GetXML();
		result += MakeUTF8String(FormatString(_T("</%s>\n"), pszTagBookmarks));
	}

	map<int, PageSettings>::const_iterator it;
	for (it = pageSettings.begin(); it != pageSettings.end(); ++it)
	{
		int nPage = (*it).first + 1;
		const PageSettings& settings = (*it).second;

		GUTF8String strPageXML = settings.GetXML();
		if (strPageXML.length() != 0)
		{
			result += MakeUTF8String(FormatString(_T("<%s %s=\"%d\" >\n"),
					pszTagPageSettings, pszAttrNumber, nPage));
			result += strPageXML;
			result += MakeUTF8String(FormatString(_T("</%s>\n"), pszTagPageSettings));
		}
	}

	if (skip_view_settings)
		result += MakeUTF8String(FormatString(_T("</%s>\n"), pszTagContent));
	else
		result += MakeUTF8String(FormatString(_T("</%s>\n"), pszTagSettings));

	return result;
}
Beispiel #11
0
GURL
DjVuErrorList::set_stream(GP<ByteStream> xibs)
{
  GUTF8String name;
  static unsigned long serial=0;
  pool=DataPool::create(xibs);
  name.format("data://%08lx/%08lx.djvu",
    ++serial,(unsigned long)(size_t)((const ByteStream *)xibs));
  pool_url=GURL::UTF8(name);
  return pool_url;
}
Beispiel #12
0
void 
JB2Dict::JB2Codec::Decode::code_comment(GUTF8String &comment)
{
      int size=CodeNum(0, BIGPOSITIVE, dist_comment_length);
      comment.empty();
      char *combuf = comment.getbuf(size);
      for (int i=0; i<size; i++) 
        {
          combuf[i]=CodeNum(0, 255, dist_comment_byte);
        }
      comment.getbuf();
}
// Expands a single message and inserts the arguments. Single_Message
// contains no separators (newlines), but includes all the parameters
// separated by tabs.
GUTF8String
DjVuMessageLite::LookUpSingle( const GUTF8String &Single_Message ) const
{
#if HAS_CTRL_C_IN_ERR_MSG
  if (Single_Message[0] != '\003')
    return Single_Message;
#endif
  //  Isolate the message ID and get the corresponding message text
  int ending_posn = Single_Message.contains("\t\v");
  if( ending_posn < 0 )
    ending_posn = Single_Message.length();
  GUTF8String msg_text;
  GUTF8String msg_number;
  const GUTF8String message=Single_Message.substr(0,ending_posn);
  LookUpID( message, msg_text, msg_number );

  //  Check whether we found anything
  if( !msg_text.length())
  {
    if(message == unrecognized)
    {
      msg_text = unrecognized_default;
    }else if(message == uparameter)
    {
      msg_text = uparameter_default;
    }else if(message == failed_to_parse_XML)
    {
      msg_text = failed_to_parse_XML_default;
    }else
    {
      return LookUpSingle(unrecognized + ("\t" + Single_Message));
    }
  }
    
  //  Insert the parameters (if any)
  unsigned int param_num = 0;
  while( (unsigned int)ending_posn < Single_Message.length() )
  {
    GUTF8String arg;
    const int start_posn = ending_posn+1;
    if(Single_Message[ending_posn] == '\v')
    {
      ending_posn=Single_Message.length();
      arg=LookUpSingle(Single_Message.substr(start_posn,ending_posn));
    }else
    {
      ending_posn = Single_Message.contains("\v\t",start_posn);
      if( ending_posn < 0 )
        ending_posn = Single_Message.length();
      arg=Single_Message.substr(start_posn, ending_posn-start_posn);
    }
    InsertArg( msg_text, ++param_num, arg);
  }
  //  Insert the message number
  InsertArg( msg_text, 0, msg_number );

  return msg_text;
}
Beispiel #14
0
void
lt_XMLTags::ParseValues(char const *t, GMap<GUTF8String,GUTF8String> &args,bool downcase)
{
  GUTF8String argn;
  char const *tt;
  while((argn=getargn(t,tt)).length())
  {
    if(downcase)
      argn=argn.downcase();
    args[argn]=getargv(tt,t).fromEscaped();
  }
}
int
DjVuANT::get_zoom(GLParser & parser)
      // Returns:
      //   <0 - special zoom (like ZOOM_STRETCH)
      //   =0 - not set
      //   >0 - numeric zoom (%%)
{
  int retval=ZOOM_UNSPEC;
  DEBUG_MSG("DjVuANT::get_zoom(): getting zoom factor ...\n");
  DEBUG_MAKE_INDENT(3);
  G_TRY
  {
    GP<GLObject> obj=parser.get_object(ZOOM_TAG);
    if (obj && obj->get_list().size()==1)
    {
      const GUTF8String zoom((*obj)[0]->get_symbol());
      DEBUG_MSG("zoom='" << zoom << "'\n");
     
      for(int i=0;(i<zoom_strings_size);++i)
      {
        if(zoom == zoom_strings[i])
        {
          retval=(-i);
          break;
        }
      }
      if(retval == ZOOM_UNSPEC)
      {
        if (zoom[0]!='d')
        {
          G_THROW( ERR_MSG("DjVuAnno.bad_zoom") );
        }else
        {
          retval=zoom.substr(1, zoom.length()).toInt(); //atoi((const char *) zoom+1);
        }
      }
    }
#ifndef NDEBUG
    if(retval == ZOOM_UNSPEC)
    {
      DEBUG_MSG("can't find any.\n");
    }
#endif // NDEBUG
  } G_CATCH_ALL {} G_ENDCATCH;
#ifndef NDEBUG
  if(retval == ZOOM_UNSPEC)
  {
    DEBUG_MSG("resetting zoom to 0 (UNSPEC)\n");
  }
#endif // NDEBUG
  return retval;
}
Beispiel #16
0
static inline int
finddirsep(const GUTF8String &fname)
{
#if defined(UNIX)
  return fname.rsearch('/',0);
#elif defined(WIN32) || defined(OS2)
  return fname.rcontains("\\/",0);
#elif defined(macintosh)
  return fname.rcontains(":/",0);
#else
#error "Define something here for your operating system"
#endif  
}
Beispiel #17
0
static inline int
finddirsep(const GUTF8String &fname)
{
#if defined(WIN32)
    return fname.rcontains("\\/",0);
#elif defined(UNIX)
    return fname.rsearch('/',0);
#elif defined(macintosh)
    return fname.rcontains(":/",0);
#else
    return fname.rsearch('/',0);
#endif
}
Beispiel #18
0
GP<DjVuFile>
lt_XMLParser::Impl::get_file(const GURL &url,GUTF8String id)
{
  GP<DjVuFile> dfile;
  GP<DjVuDocument> doc;
  GCriticalSectionLock lock(&xmlparser_lock);
  {
    GPosition pos=m_docs.contains(url.get_string());
    if(pos)
    {
      doc=m_docs[pos];
    }else
    {
      doc=DjVuDocument::create_wait(url);
      if(! doc->wait_for_complete_init())
      {
        G_THROW(( ERR_MSG("XMLAnno.fail_init") "\t")+url.get_string() );
      }
      m_docs[url.get_string()]=doc;
    }
    if(id.is_int())
    {
      const int xpage=id.toInt(); //atoi((char const *)page); 
      if(xpage>0)
        id=doc->page_to_id(xpage-1);
    }else if(!id.length())
    { 
      id=doc->page_to_id(0);
    }
  }
  const GURL fileurl(doc->id_to_url(id));
  GPosition dpos(m_files.contains(fileurl.get_string()));
  if(!dpos)
  {
    if(!doc->get_id_list().contains(id))
    {
      G_THROW( ERR_MSG("XMLAnno.bad_page") );
    }
    dfile=doc->get_djvu_file(id,false);
    if(!dfile)
    {
      G_THROW( ERR_MSG("XMLAnno.bad_page") );
    }
    m_files[fileurl.get_string()]=dfile;
  }else
  {
    dfile=m_files[dpos];
  }
  return dfile;
}
Beispiel #19
0
GUTF8String
GOS::getenv(const GUTF8String &name)
{
  GUTF8String retval;
  if(name.length())
  {
    const char *env=::getenv(name.getUTF82Native());
    if(env)
    {
      retval=GNativeString(env);
    }
  }
  return retval;
}
void DjVuSource::ReadAnnotations(GP<ByteStream> pInclStream,
		set<GUTF8String>& processed, GP<ByteStream> pAnnoStream)
{
	// Look for shared annotations
	GUTF8String strInclude;
	char buf[1024];
	int nLength;
	while ((nLength = pInclStream->read(buf, 1024)))
		strInclude += GUTF8String(buf, nLength);

	// Eat '\n' in the beginning and at the end
	while (strInclude.length() > 0 && strInclude[0] == '\n')
		strInclude = strInclude.substr(1, static_cast<unsigned int>(-1));

	while (strInclude.length() > 0 && strInclude[static_cast<int>(strInclude.length()) - 1] == '\n')
		strInclude.setat(strInclude.length() - 1, 0);

	if (strInclude.length() > 0 && processed.find(strInclude) == processed.end())
	{
		processed.insert(strInclude);

		GURL urlInclude = m_pDjVuDoc->id_to_url(strInclude);
		GP<DataPool> pool = m_pDjVuDoc->request_data(NULL, urlInclude);
		GP<ByteStream> stream = pool->get_stream();
		GP<IFFByteStream> iff(IFFByteStream::create(stream));

		// Check file format
		GUTF8String chkid;
		if (!iff->get_chunk(chkid) ||
			(chkid != "FORM:DJVI" && chkid != "FORM:DJVU" &&
			chkid != "FORM:PM44" && chkid != "FORM:BM44"))
		{
			return;
		}

		// Find chunk with page info
		while (iff->get_chunk(chkid) != 0)
		{
			GP<ByteStream> chunk_stream = iff->get_bytestream();

			if (chkid == "INCL")
			{
				ReadAnnotations(pInclStream, processed, pAnnoStream);
			}
			else if (chkid == "FORM:ANNO")
			{
				pAnnoStream->copy(*chunk_stream);
			}
			else if (chkid == "ANTa" || chkid == "ANTz")
			{
				const GP<IFFByteStream> iffout = IFFByteStream::create(pAnnoStream);
				iffout->put_chunk(chkid);
				iffout->copy(*chunk_stream);
				iffout->close_chunk();
			}

			iff->seek_close_chunk();
		}
	}
}
Beispiel #21
0
void
display(const GURL &url)
{
   DjVuDumpHelper helper;
   GP<ByteStream> ibs = ByteStream::create(url, "rb");
   GP<ByteStream> obs = helper.dump(ibs);
   GUTF8String str;
   size_t size = obs->size();
   char *buf = str.getbuf(obs->size());
   obs->seek(0);
   obs->readall(buf, size);
   GNativeString ns = str;
   fputs((const char*)ns, outputf);
}
Beispiel #22
0
void
DjVuTXT::Zone::normtext(const char *instr, GUTF8String &outstr)
{
  if (text_length == 0)
    {
      // Descend collecting text below
      text_start = outstr.length();
      for (GPosition i=children; i; ++i)
        children[i].normtext(instr, outstr);
      text_length = outstr.length() - text_start;
      // Ignore empty zones
      if (text_length == 0)
        return;
    }
  else
    {
      // Collect text at this level
      int new_start = outstr.length();
      outstr = outstr + GUTF8String(instr+text_start, text_length);
      text_start = new_start;
      // Clear textual information on lower level nodes
      for (GPosition i=children; i; ++i)
        children[i].cleartext();
    }
  // Determine standard separator
  char sep;
  switch (ztype)
    {
    case COLUMN:
      sep = end_of_column; break;
    case REGION:
      sep = end_of_region; break;
    case PARAGRAPH: 
      sep = end_of_paragraph; break;
    case LINE:
      sep = end_of_line; break;
    case WORD:
      sep = ' '; break;
    default:
      return;
    }
  // Add separator if not present yet.
  if (outstr[text_start+text_length-1] != sep)
    {
      outstr = outstr + GUTF8String(&sep, 1);
      text_length += 1;
    }
}
void DictionaryInfo::ReadLangTo(const GUTF8String& str, bool bEncoded)
{
	strLangToCode = "";
	langToLoc.clear();

	if (str.length() == 0)
		return;

	string strTemp = str;
	if (bEncoded)
		Base64Decode(strTemp);

	strLangToRaw = strTemp.c_str();

	stringstream sin(strTemp.c_str());
	XMLParser parser;
	if (parser.Parse(sin))
	{
		wstring strCode;
		if (parser.GetRoot()->GetAttribute(pszAttrCode, strCode))
			strLangToCode = MakeUTF8String(strCode);

		ReadLocalizedStrings(langToLoc, *parser.GetRoot());
	}
}
Beispiel #24
0
GUTF8String 
GUTF8String::create(void const * const buf,const unsigned int size,
    const EncodeType encodetype, const GUTF8String &encoding)
{
  return encoding.length()
    ?create(buf,size,encodetype)
    :create(buf,size,encoding);
}
Beispiel #25
0
void 
lt_XMLParser::Impl::ChangeTextOCR(
  const GUTF8String &value,
  const int width,
  const int height,
  const GP<DjVuFile> &dfile)
{
  if(value.length() && value.downcase() != "false")
  {
    const GP<ByteStream> bs=OCRcallback(value,DjVuImage::create(dfile));
    if( bs && bs->size() )
    {
      const GP<lt_XMLTags> tags(lt_XMLTags::create(bs));
      ChangeText(width,height,*dfile,*tags);
    }
  }
}
Beispiel #26
0
// helper function for args
static void 
intList(GUTF8String coords, GList<int> &retval)
{
  int pos=0;
  while(coords.length())
  {
    int epos;
    unsigned long i=coords.toLong(pos,epos,10);
    if(epos>=0)
    {
      retval.append(i);
      const int n=coords.nextNonSpace(epos);
      if(coords[n] != ',')
        break;
      pos=n+1;
    }
  }
}
Beispiel #27
0
static unsigned long
convertToColor(const GUTF8String &s)
{
  unsigned long retval=0;
  if(s.length())
  {
    int endpos;
    if(s[0] == '#')
    {
      retval=s.substr(1,-1).toULong(0,endpos,16);
    }
    if(endpos < 0)
    {
      G_THROW( (ERR_MSG("XMLAnno.bad_color") "\t")+s );
    }
  }
  return retval;
}
Beispiel #28
0
static int
CountLines(const GUTF8String &str)
{
  int retval=0;
  static const unsigned long lf='\n';
  for(int pos=0;(pos=str.search(lf,pos)+1)>0;++retval)
    EMPTY_LOOP;
  return retval;
}
GUTF8String
DjVuANT::get_xmlmap(const GUTF8String &name,const int height) const
{
  GUTF8String retval("<MAP name=\""+name.toEscaped()+"\" >\n");
  for(GPosition pos(map_areas);pos;++pos)
  {
    retval+=map_areas[pos]->get_xmltag(height);
  }
  return retval+"</MAP>\n";
}
Beispiel #30
0
GPList<DjVuPort>
DjVuPortcaster::prefix_to_ports(const GUTF8String &prefix)
{
  GPList<DjVuPort> list;
  {
    int length=prefix.length();
    if (length)
    {
      GCriticalSectionLock lock(&map_lock);
      for(GPosition pos=a2p_map;pos;++pos)
        if (!prefix.cmp(a2p_map.key(pos), length))
        {
          DjVuPort * port=(DjVuPort *) a2p_map[pos];
          GP<DjVuPort> gp_port=is_port_alive(port);
          if (gp_port) list.append(gp_port);
        }
    }
  }
  return list;
}