[Aptitude-svn-commit] r3580 - in branches/aptitude-0.3/aptitude: . src/vscreen

Daniel Burrows dburrows@costa.debian.org
Sun Jul 3 16:16:54 UTC 2005


Author: dburrows
Date: Sun Jul  3 16:16:50 2005
New Revision: 3580

Modified:
   branches/aptitude-0.3/aptitude/ChangeLog
   branches/aptitude-0.3/aptitude/src/vscreen/vs_pager.cc
   branches/aptitude-0.3/aptitude/src/vscreen/vs_pager.h
Log:
Allow pager text to be in arbitrary encodings.

Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog	(original)
+++ branches/aptitude-0.3/aptitude/ChangeLog	Sun Jul  3 16:16:50 2005
@@ -1,5 +1,10 @@
 2005-07-03  Daniel Burrows  <dburrows@debian.org>
 
+	* src/vscreen/vs_pager.cc, src/vscreen/vs_pager.h:
+
+	  Use wide characters in the vs_pager, and transcode the input
+	  text from a (optionally specified) encoding.
+
 	* src/vscreen/transcode.cc, src/vscreen/transcode.h:
 
 	  Add transcode-with-punted-errors variants that read directly

Modified: branches/aptitude-0.3/aptitude/src/vscreen/vs_pager.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/src/vscreen/vs_pager.cc	(original)
+++ branches/aptitude-0.3/aptitude/src/vscreen/vs_pager.cc	Sun Jul  3 16:16:50 2005
@@ -25,15 +25,23 @@
 
 keybindings *vs_pager::bindings=NULL;
 
-vs_pager::vs_pager(const char *text, int len)
+vs_pager::vs_pager(const char *text, int len, const char *encoding)
   :vscreen_widget(), first_line(0), first_column(0), text_width(0)
 {
-  set_text(text, len);
+  set_text(text, len, encoding);
 
   do_layout.connect(sigc::mem_fun(*this, &vs_pager::layout_me));
 }
 
-vs_pager::vs_pager(const string &s)
+vs_pager::vs_pager(const string &s, const char *encoding)
+  :vscreen_widget(), first_line(0), first_column(0), text_width(0)
+{
+  set_text(s, encoding);
+
+  do_layout.connect(sigc::mem_fun(*this, &vs_pager::layout_me));
+}
+
+vs_pager::vs_pager(const wstring &s)
   :vscreen_widget(), first_line(0), first_column(0), text_width(0)
 {
   set_text(s);
@@ -43,37 +51,44 @@
 
 vs_pager::~vs_pager() {}
 
-void vs_pager::set_text(const string &s)
+void vs_pager::set_text(const string &s, const char *encoding)
+{
+  set_text(transcode(s, encoding));
+}
+
+void vs_pager::set_text(const char *txt, string::size_type len,
+			const char *encoding)
 {
-  set_text(s.c_str(), s.size());
+  // FIXME: get rid of the intermediate copy of the data if possible.
+  set_text(transcode(string(txt, len), encoding));
 }
 
-void vs_pager::set_text(const char *text, string::size_type len)
+void vs_pager::set_text(const wstring &s)
 {
-  string::size_type loc=0;
+  wstring::size_type loc=0;
 
   text_width=0;
 
   lines.clear();
 
-  while(loc<len)
+  while(loc<s.size())
     {
-      string curline;
+      wstring curline;
       col_count cur_width=0;
 
-      while(loc<len && text[loc]!='\n')
+      while(loc<s.size() && s[loc]!=L'\n')
 	{
-	  if(text[loc]=='\t')
+	  if(s[loc]==L'\t')
 	    cur_width+=8;
 	  else
 	    ++cur_width;
 
-	  curline+=text[loc];
+	  curline+=s[loc];
 
 	  ++loc;
 	}
 
-      if(loc<len)
+      if(loc<s.size())
 	++loc;
 
       text_width=max(cur_width, text_width);
@@ -164,11 +179,11 @@
     scroll_down(getmaxy());
 }
 
-void vs_pager::search_for(string s)
+void vs_pager::search_for(const wstring &s)
 {
-  if(s!="")
+  if(s!=L"")
     last_search=s;
-  else if(last_search=="")
+  else if(last_search==L"")
     {
       beep();
       return;
@@ -180,7 +195,7 @@
     {
       col_count loc=lines[i].find(last_search);
 
-      if(loc!=string::npos)
+      if(loc!=wstring::npos)
 	{
 	  first_line=i;
 	  do_line_signal();
@@ -245,7 +260,7 @@
 
   for(int y=0; y<height && first_line+y<lines.size(); ++y)
     {
-      const string &s=lines[first_line+y];
+      const wstring &s=lines[first_line+y];
       col_count x=0, curr=0;
 
       while(curr<s.size() && x<first_column+width)
@@ -299,7 +314,8 @@
 {
 }
 
-void vs_file_pager::load_file(const string &filename)
+void vs_file_pager::load_file(const string &filename,
+			      const char *encoding)
 {
   int fd=open(filename.c_str(), O_RDONLY, 0644);
 
@@ -331,7 +347,7 @@
 	      // FIXME: just display something in the widget itself?
 	    }
 	  else
-	    vs_pager::set_text(contents, buf.st_size);
+	    vs_pager::set_text(contents, buf.st_size, encoding);
 
 
 	  if(fd!=-1)
@@ -343,18 +359,24 @@
     }
 }
 
-void vs_file_pager::load_file(const wstring &filename)
+void vs_file_pager::load_file(const wstring &filename,
+			      const char *encoding)
 {
   string mbfilename;
 
   if(transcode(filename, mbfilename))
-    load_file(mbfilename);
+    load_file(mbfilename, encoding);
   else
     {
-      char buf[512];
+      wchar_t buf[512];
 
-      snprintf(buf, sizeof(buf),
-	       _("Unable to load filename: the string %ls has no multibyte representation."), filename.c_str());
+      swprintf(buf, sizeof(buf)/sizeof(wchar_t),
+	       transcode(_("Unable to load filename: the string %ls has no multibyte representation.")).c_str(), filename.c_str());
       set_text(buf);
     }
 }
+
+void vs_file_pager::load_file(const wstring &filename)
+{
+  load_file(filename, NULL);
+}

Modified: branches/aptitude-0.3/aptitude/src/vscreen/vs_pager.h
==============================================================================
--- branches/aptitude-0.3/aptitude/src/vscreen/vs_pager.h	(original)
+++ branches/aptitude-0.3/aptitude/src/vscreen/vs_pager.h	Sun Jul  3 16:16:50 2005
@@ -19,11 +19,11 @@
 class vs_pager:public vscreen_widget
 {
 public:
-  typedef std::vector<std::string>::size_type line_count;
-  typedef std::string::size_type col_count;
+  typedef std::vector<std::wstring>::size_type line_count;
+  typedef std::wstring::size_type col_count;
 private:
   /** The lines of text being displayed: */
-  std::vector<std::string> lines;
+  std::vector<std::wstring> lines;
 
   /** The first visible line. */
   line_count first_line;
@@ -35,7 +35,7 @@
   col_count text_width;
 
   /** The last string the user searched for (so we can repeat searches) */
-  std::string last_search;
+  std::wstring last_search;
 
   /** Handles resizing the widget. */
   void layout_me();
@@ -44,14 +44,22 @@
    *
    *  \param text the text to display
    *  \param len the length of the buffer
+   *  \param encoding the encoding of text, or \b NULL to use LC_CTYPE
    */
-  vs_pager(const char *text, int len);
+  vs_pager(const char *text, int len, const char *encoding=NULL);
 
   /** Create a vs_pager from a string.
    *
    *  \param s the text to display
+   *  \param encoding the encoding of s, or \b NULL to use LC_CTYPE
    */
-  vs_pager(const std::string &s);
+  vs_pager(const std::string &s, const char *encoding=NULL);
+
+  /** Create a vs_pager from a wide character string.
+   *
+   *  \param s the text to display
+   */
+  vs_pager(const std::wstring &s);
 
   /** Destroy this vs_pager. */
   virtual ~vs_pager();
@@ -60,11 +68,24 @@
    *
    *  \param text the text to display
    *  \param len the length of the buffer
+   *  \param encoding the encoding of text, or \b NULL to use LC_CTYPE
    */
-  virtual void set_text(const char *text, std::string::size_type len);
+  virtual void set_text(const char *text,
+			std::string::size_type len,
+			const char *encoding=NULL);
 
-  /** Change the displayed text. */
-  virtual void set_text(const std::string &s);
+  /** Change the displayed text.
+   *
+   *  \param s the text to display
+   *  \param encoding the encoding of s, or \b NULL to use LC_CTYPE
+   */
+  virtual void set_text(const std::string &s, const char *encoding=NULL);
+
+  /** Change the displayed text.
+   *
+   *  \param s the text to display
+   */
+  virtual void set_text(const std::wstring &s);
 
   /** Scroll the screen up by the given number of lines. */
   void scroll_up(line_count nlines);
@@ -95,10 +116,10 @@
    *
    *  \param s the string to search for
    */
-  void search_for(std::string s);
+  void search_for(const std::wstring &s);
 
   /** Return the last string which the user searched for. */
-  std::string get_last_search() {return last_search;}
+  std::wstring get_last_search() {return last_search;}
 
   line_count get_first_line() {return first_line;}
   line_count get_num_lines() {return lines.size();}
@@ -151,11 +172,29 @@
 
   vs_file_pager(const char *text, int len);
 
-  void load_file(const std::string &filename);
+  /** Loads the given file into the pager.
+   *
+   *  \param filename the name of the file to load
+   *  \param encoding the encoding of the file's contents; if \b NULL, LC_CTYPE
+   *                  is used.
+   */
+  void load_file(const std::string &filename, const char *encoding=NULL);
 
   /** Attempts to convert the string to a multibyte representation and
    *  then load it; a nonconvertible string is treated as any other
    *  load failure would be.
+   *
+   *  \param filename the name of the file to load
+   *  \param encoding the encoding of the file's contents; if \b NULL, LC_CTYPE is used.
+   */
+  void load_file(const std::wstring &filename, const char *encoding);
+
+  /** Attempts to convert the string to a multibyte representation and
+   *  then load it; a nonconvertible string is treated as any other
+   *  load failure would be.  The file is assumed to contain text in
+   *  the encoding specified by LC_CTYPE.
+   *
+   *  \param filename the name of the file to load
    */
   void load_file(const std::wstring &filename);
 };




More information about the Aptitude-svn-commit mailing list