[glom] CSV Import: Minor code style changes.



commit 4828bb2db06d20846bc316c70efcefa7598ba442
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Sep 18 10:44:10 2009 +0200

    CSV Import: Minor code style changes.
    
    * glom/dialog_import_csv.cc:
    * glom/dialog_import_csv_progress.cc:
    * glom/import_csv.[h]cc]: Prefix the state enum with STATE_.
    Use 0 instead of NULL because this is C++. Remove some debug output.
    Make signal type and variable names consistent with other code.
    * tests/import/test_signals.cc: Add a last std::endl.

 ChangeLog                          |   11 +++++++++++
 glom/dialog_import_csv.cc          |   24 ++++++++++++------------
 glom/dialog_import_csv_progress.cc |    8 ++++----
 glom/import_csv.cc                 |   18 +++++++++---------
 glom/import_csv.h                  |   25 ++++++++++++-------------
 tests/import/test_signals.cc       |    3 ++-
 6 files changed, 50 insertions(+), 39 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index f3a17a2..06084c3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2009-09-18  Murray Cumming  <murrayc murrayc com>
 
+	CSV Import: Minor code style changes.
+
+	* glom/dialog_import_csv.cc:
+	* glom/dialog_import_csv_progress.cc:
+	* glom/import_csv.[h]cc]: Prefix the state enum with STATE_. 
+	Use 0 instead of NULL because this is C++. Remove some debug output.
+	Make signal type and variable names consistent with other code.
+	* tests/import/test_signals.cc: Add a last std::endl.
+
+2009-09-18  Murray Cumming  <murrayc murrayc com>
+
 	Tests: Stop using valgrind by default.
 
 	* Makefile_tests.am: Do not run all tests under valgrind. valgrind is 
diff --git a/glom/dialog_import_csv.cc b/glom/dialog_import_csv.cc
index 6204041..7a0ff65 100644
--- a/glom/dialog_import_csv.cc
+++ b/glom/dialog_import_csv.cc
@@ -130,7 +130,7 @@ Dialog_Import_CSV::Dialog_Import_CSV(BaseObjectType* cobject, const Glib::RefPtr
   for(guint i = 0; i < N_ENCODINGS; ++ i)
   {
     iter = m_encoding_model->append();
-    if(ENCODINGS[i].name != NULL)
+    if(ENCODINGS[i].name != 0)
     {
       (*iter)[m_encoding_columns.m_col_name] = _(ENCODINGS[i].name);
       (*iter)[m_encoding_columns.m_col_charset] = ENCODINGS[i].charset;
@@ -247,7 +247,7 @@ void Dialog_Import_CSV::import(const Glib::ustring& uri, const Glib::ustring& in
     // Query the display name of the file to set in the title:
     m_file->query_info_async(sigc::mem_fun(*this, &Dialog_Import_CSV::on_query_info), G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
 
-    set_parser_state(CsvParser::PARSING);
+    set_parser_state(CsvParser::STATE_PARSING);
   }
 }
 
@@ -289,16 +289,16 @@ void Dialog_Import_CSV::clear()
   m_fields.clear();
   m_file.reset();
   m_filename.clear();
-  m_buffer.reset(NULL);
+  m_buffer.reset(0);
   m_parser->clear();
-  //m_parser.reset(NULL);
+  //m_parser.reset(0);
   m_encoding_info->set_text("");
   m_sample_view->set_sensitive(false);
   m_encoding_combo->set_sensitive(false);
   m_first_line_as_title->set_sensitive(false);
   m_sample_rows->set_sensitive(false);
 
-  set_parser_state(CsvParser::NONE);
+  set_parser_state(CsvParser::STATE_NONE);
   validate_primary_key();
 }
 
@@ -397,7 +397,7 @@ void Dialog_Import_CSV::on_stream_read(const Glib::RefPtr<Gio::AsyncResult>& res
     // If the parser does not exist yet, then create a new parser, except when the
     // current encoding does not work for the file ,in which case the user must first
     // choose another encoding.
-    else if(!m_parser.get() && get_parser_state() != CsvParser::ENCODING_ERROR)
+    else if(!m_parser.get() && get_parser_state() != CsvParser::STATE_ENCODING_ERROR)
     {
       begin_parse();
     }
@@ -410,7 +410,7 @@ void Dialog_Import_CSV::on_stream_read(const Glib::RefPtr<Gio::AsyncResult>& res
     else
     {
       // Finished reading
-      m_buffer.reset(NULL);
+      m_buffer.reset(0);
       m_parser->m_stream.reset();
       m_file.reset();
     }
@@ -450,7 +450,7 @@ void Dialog_Import_CSV::on_stream_read(const Glib::RefPtr<Gio::AsyncResult>& res
       else
       {
         // Finished reading
-        m_buffer.reset(NULL);
+        m_buffer.reset(0);
         m_parser->m_stream.reset();
         m_file.reset();
       }
@@ -482,7 +482,7 @@ void Dialog_Import_CSV::on_encoding_changed()
 
   // Reset current parsing process
   // TODO: Troublesome. Parser now contains a bit more members, not sure we can simply reset it like that.
-  //m_parser.reset(NULL);
+  //m_parser.reset(0);
   //m_parser->clear();
   //m_parser->set_encoding(get_current_encoding().c_str());
 
@@ -616,7 +616,7 @@ void Dialog_Import_CSV::begin_parse()
   m_parser->clear();
 
   m_parser->set_encoding(get_current_encoding().c_str());
-  set_parser_state(CsvParser::PARSING);
+  set_parser_state(CsvParser::STATE_PARSING);
 
   // Allow the Import button to be pressed when a field for the primary key
   // field is set. When the import button is pressed without the file being
@@ -635,7 +635,7 @@ void Dialog_Import_CSV::on_encoding_error()
   m_sample_view->set_model(m_sample_model); // Empty model
 
   // TODO: move into parser.
-  set_parser_state(CsvParser::ENCODING_ERROR);
+  set_parser_state(CsvParser::STATE_ENCODING_ERROR);
 
   // Don't allow the import button to be pressed when an error occured. This
   // would not make sense since we cleared all the parsed row data anyway.
@@ -915,7 +915,7 @@ void Dialog_Import_CSV::set_parser_state(CsvParser::State state)
 
 void Dialog_Import_CSV::validate_primary_key()
 {
-  if(get_parser_state() == (CsvParser::NONE | CsvParser::ENCODING_ERROR))
+  if(get_parser_state() == (CsvParser::STATE_NONE | CsvParser::STATE_ENCODING_ERROR))
   {
     m_error_label->hide();
     set_response_sensitive(Gtk::RESPONSE_ACCEPT, false);
diff --git a/glom/dialog_import_csv_progress.cc b/glom/dialog_import_csv_progress.cc
index 9296fa0..0ec5075 100644
--- a/glom/dialog_import_csv_progress.cc
+++ b/glom/dialog_import_csv_progress.cc
@@ -59,14 +59,14 @@ void Dialog_Import_CSV_Progress::import(Dialog_Import_CSV& data_source)
 
   switch(data_source.get_parser_state())
   {
-  case CsvParser::PARSING:
+  case CsvParser::STATE_PARSING:
     // Wait for the parsing to finish. We do not start importing before the file has been
     // parsed completely since we would not to rollback our changes in case of a
     // parsing error.
     m_progress_bar->set_text(Glib::ustring::compose(_("Parsing CSV file %1"), data_source.get_filename()));
     m_ready_connection = data_source.signal_state_changed().connect(sigc::mem_fun(*this, &Dialog_Import_CSV_Progress::on_state_changed));
     break;
-  case CsvParser::PARSED:
+  case CsvParser::STATE_PARSED:
     begin_import();
     break;
   default:
@@ -108,11 +108,11 @@ void Dialog_Import_CSV_Progress::on_state_changed()
 {
   switch(m_data_source->get_state())
   {
-  case CsvParser::ENCODING_ERROR:
+  case CsvParser::STATE_ENCODING_ERROR:
     // Cancel on error
     response(Gtk::RESPONSE_CANCEL);
     break;
-  case CsvParser::PARSED:
+  case CsvParser::STATE_PARSED:
     // Begin importing when fully parsed
     begin_import();
     //m_progress_connection = Glib::signal_idle().connect(sigc::mem_fun(*this, &Dialog_Import_CSV_Progress::on_idle_import));
diff --git a/glom/import_csv.cc b/glom/import_csv.cc
index b0363c5..06d5908 100644
--- a/glom/import_csv.cc
+++ b/glom/import_csv.cc
@@ -57,7 +57,7 @@ CsvParser::CsvParser(const char* encoding)
   m_input_position(0),
   m_idle_connection(),
   m_line_number(0),
-  m_state(NONE),
+  m_state(STATE_NONE),
   m_stream(),
   m_rows()
 {}
@@ -67,14 +67,14 @@ CsvParser::~CsvParser()
   m_idle_connection.disconnect();
 }
 
-CsvParser::SignalEncodingError CsvParser::signal_encoding_error() const
+CsvParser::type_signal_encoding_error CsvParser::signal_encoding_error() const
 {
-  return m_encoding_error;
+  return m_signal_encoding_error;
 }
 
-CsvParser::SignalLineScanned CsvParser::signal_line_scanned() const
+CsvParser::type_signal_line_scanned CsvParser::signal_line_scanned() const
 {
-  return m_line_scanned;
+  return m_signal_line_scanned;
 }
 
 void CsvParser::set_encoding(const char* encoding)
@@ -140,7 +140,7 @@ Glib::ustring::const_iterator CsvParser::advance_field(const Glib::ustring::cons
   }
 
   // TODO: Throw error if still inside a quoted string?
-  std::cout << "debug: field=" << field << std::endl;
+  //std::cout << "debug: field=" << field << std::endl;
   return walk;
 }
 
@@ -155,7 +155,7 @@ void CsvParser::clear()
   // Disconnect signal handlers, too? Nah, I don't think so ...
   //m_idle_connection.disconnect();
   m_line_number = 0;
-  m_state = NONE;
+  m_state = STATE_NONE;
 }
 
 bool CsvParser::on_idle_parse()
@@ -303,8 +303,8 @@ bool CsvParser::on_idle_parse()
       signal_line_scanned().emit(m_current_line, m_line_number);
     }
 
-    // Parsed whole file, done
-    m_state = PARSED;
+    // We have parsed the whole file. We have finished.
+    m_state = STATE_PARSED;
   }
 
   // Continue if there are more bytes to process
diff --git a/glom/import_csv.h b/glom/import_csv.h
index 380f12d..109e7f1 100644
--- a/glom/import_csv.h
+++ b/glom/import_csv.h
@@ -43,12 +43,11 @@ public:
 
   ~CsvParser();
 
-  // TODO: prefix enums with STATE_* ?
   enum State {
-    NONE,
-    PARSING,
-    ENCODING_ERROR,
-    PARSED
+    STATE_NONE, 
+    STATE_PARSING,  /**< Parsing is in progress. */
+    STATE_ENCODING_ERROR, /**< An error happened while parsing. */
+    STATE_PARSED /**< Finished parsing. */
   };
 
   static const gunichar DELIMITER = ',';
@@ -57,14 +56,14 @@ public:
   static bool next_char_is_quote(const Glib::ustring::const_iterator& iter, const Glib::ustring::const_iterator& end);
   static Glib::ustring::const_iterator advance_field(const Glib::ustring::const_iterator& iter, const Glib::ustring::const_iterator& end, Glib::ustring& field);
 
-  // Signals
-  typedef sigc::signal<void> SignalEncodingError;
-  SignalEncodingError signal_encoding_error() const;
+  // Signals:
+  typedef sigc::signal<void> type_signal_encoding_error;
+  type_signal_encoding_error signal_encoding_error() const;
 
-  typedef sigc::signal<void, std::string, unsigned int> SignalLineScanned;
-  SignalLineScanned signal_line_scanned() const;
+  typedef sigc::signal<void, std::string, unsigned int> type_signal_line_scanned;
+  type_signal_line_scanned signal_line_scanned() const;
 
-  // Make parser object reusable.
+  /// Make parser object reusable.
   void clear();
 
   // In order to not make the UI feel sluggish during larger imports we parse
@@ -93,8 +92,8 @@ public:
   typedef std::vector<type_row_strings> type_rows;
   type_rows m_rows;
 
-  SignalEncodingError m_encoding_error;
-  SignalLineScanned m_line_scanned;
+  type_signal_encoding_error m_signal_encoding_error;
+  type_signal_line_scanned m_signal_line_scanned;
 };
 
 } //namespace Glom
diff --git a/tests/import/test_signals.cc b/tests/import/test_signals.cc
index 61eaf86..e0866ae 100644
--- a/tests/import/test_signals.cc
+++ b/tests/import/test_signals.cc
@@ -8,6 +8,7 @@ namespace {
 
 typedef std::vector<std::string> Encodings;
 
+/// This takes a @a size argument so we can test parsing of null bytes.
 void set_parser_contents(Glom::CsvParser& parser, const char* input, guint size)
 {
   // Do not read terminating null byte.
@@ -156,7 +157,7 @@ int main()
     parser.clear();
   }
 
-  std::cout << results.rdbuf();
+  std::cout << results.rdbuf() << std::endl;
   return (test_ignore_quoted_newlines &&
           test_ignore_empty_lines &&
           test_wrong_encoding &&



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]