[sanexx-commit] SF.net SVN: sanexx: [15]
Status: Pre-Alpha
Brought to you by:
paddy_hack
|
From: <pad...@us...> - 2006-03-12 09:24:33
|
Revision: 15 Author: paddy_hack Date: 2006-03-12 01:24:16 -0800 (Sun, 12 Mar 2006) ViewCVS: http://svn.sourceforge.net/sanexx/?rev=15&view=rev Log Message: ----------- r30@qed: olaf | 2006-03-12 17:05:36 +0900 Added a test for a device-name option and required it to display the device specific options when combined with --help. Implemented with still experimental API in the only frontend we provide. The library has been hacked up to support that API and pass the test. Modified Paths: -------------- trunk/include/sane++ trunk/lib/sane++.cc trunk/src/scanimage.cc trunk/tests/frontend-cli-options Property Changed: ---------------- / Property changes on: ___________________________________________________________________ Name: svk:merge - 52428bda-890d-0410-88ad-be188b7e1831:/local:29 + 52428bda-890d-0410-88ad-be188b7e1831:/local:30 Modified: trunk/include/sane++ =================================================================== --- trunk/include/sane++ 2006-03-12 09:24:03 UTC (rev 14) +++ trunk/include/sane++ 2006-03-12 09:24:16 UTC (rev 15) @@ -31,6 +31,7 @@ namespace sane { +using std::endl; using std::list; using std::ostream; using std::string; @@ -41,7 +42,17 @@ class device { public: + device (const string& device_name = string ()); + ~device (void); + class info; + class option; + + list<option> get_options (void); + +private: + SANE_Handle _handle; + list<option> _option; }; list<device::info> get_devices (void); @@ -64,6 +75,23 @@ ostream& operator<< (ostream& os, const device::info& dev_info); +class device::option +{ +public: + class descriptor; + + const descriptor& description () const; + +private: + SANE_Int _index; + const SANE_Option_Descriptor *_descriptor; + + option (SANE_Int i, const SANE_Option_Descriptor *sod = NULL); + + friend list<option> device::get_options (void); + friend ostream& operator<< (ostream& os, const device::option& dev_opt); +}; + } // namespace sane #endif /* !included_sanexx */ Modified: trunk/lib/sane++.cc =================================================================== --- trunk/lib/sane++.cc 2006-03-12 09:24:03 UTC (rev 14) +++ trunk/lib/sane++.cc 2006-03-12 09:24:16 UTC (rev 15) @@ -61,6 +61,47 @@ } +device::device (const string& device_name) + : _handle (NULL) +{ + SANE_Status s = sane_open (device_name.c_str (), &_handle); + + if (SANE_STATUS_GOOD != s) + throw; // something appropriate +} + +device::~device (void) +{ + sane_close (_handle); + _handle = NULL; +} + + +list<device::option> +device::get_options (void) +{ + if (_option.empty ()) { // lazy initialisation + SANE_Int count = 0; + SANE_Status s = sane_control_option (_handle, 0, + SANE_ACTION_GET_VALUE, + &count, NULL); + + if (SANE_STATUS_GOOD != s) + throw; + + for (SANE_Int i = 0; i < count; ++i) + { + const SANE_Option_Descriptor *sod = NULL; + sod = sane_get_option_descriptor (_handle, i); + + _option.push_back (device::option (i, sod)); + } + } + + return _option; +} + + string device::info::name () const { @@ -99,4 +140,27 @@ << dev_info.type (); } + +device::option::option (SANE_Int index, + const SANE_Option_Descriptor *descriptor) + : _index (index), _descriptor (descriptor) +{ +} + +ostream& +operator<< (ostream& os, const device::option& dev_opt) +{ + const SANE_Option_Descriptor *sod = dev_opt._descriptor; + + if (SANE_TYPE_GROUP == sod->type) { + os << " " << sod->title << ":"; + } else { + os << " --" << sod->name + << endl + << " " << sod->desc; + } + + return os; +} + } // namespace sane Modified: trunk/src/scanimage.cc =================================================================== --- trunk/src/scanimage.cc 2006-03-12 09:24:03 UTC (rev 14) +++ trunk/src/scanimage.cc 2006-03-12 09:24:16 UTC (rev 15) @@ -28,7 +28,7 @@ #include <cstdlib> #include <cstring> -#include <libgen.h> +#include <libgen.h> /* for a POSIX basename() */ #include <iostream> #include <boost/program_options.hpp> @@ -42,7 +42,8 @@ int display_version (const string& program_name); int display_help (const string& program_name, - const po::options_description& visible); + const po::options_description& visible, + const string& device_name = string ()); int list_devices (const string& program_name, const po::options_description& options); @@ -59,6 +60,8 @@ "display this help message and exit") ("list-devices", "display a list of available devices") + ("device-name", po::value<string> (), + "set the name of the device to use") ; po::variables_map vm; @@ -70,7 +73,13 @@ } if (vm.count ("help")) { - return display_help (program_name, visible); + string device_name; + + if (vm.count ("device-name")) { + device_name = vm["device-name"].as<string> (); + } + + return display_help (program_name, visible, device_name); } if (vm.count ("list-devices")) { @@ -98,14 +107,40 @@ int display_help (const string& program_name, - const po::options_description& visible) + const po::options_description& visible, + const string& device_name) { cout << "Usage: " << program_name << " [OPTION]..." << endl << "Acquire image data from a raster imaging device." << endl << "Image data is written to standard output." << endl << endl - << visible - << endl + << visible; + + if (!device_name.empty ()) { + cout << endl + << "Device Options (" << device_name << "):" << endl; + + using namespace sane; + + init (); + device *dev = new device (device_name); + // the new/delete is to work around a + // segfault with calling exit () when + // there are still instances around + + list<device::option> dev_opt_list = dev->get_options (); + list<device::option>::const_iterator opt = dev_opt_list.begin (); + + for (++opt; // skip number of options supported + opt != dev_opt_list.end (); ++opt) { + cout << *opt << endl; + } + + delete dev; + exit (); + } + + cout << endl << "Report bugs to <" PACKAGE_BUGREPORT << ">." << endl; return EXIT_SUCCESS; Modified: trunk/tests/frontend-cli-options =================================================================== --- trunk/tests/frontend-cli-options 2006-03-12 09:24:03 UTC (rev 14) +++ trunk/tests/frontend-cli-options 2006-03-12 09:24:16 UTC (rev 15) @@ -41,7 +41,8 @@ } test_documented_options scanimage++ \ - list-devices + list-devices \ + device-name sane_env scanimage++ --version > $test_prog.out 2>/dev/null @@ -59,3 +60,13 @@ ${srcdir}/sane-conf/test.conf` actual=`$AWK 'BEGIN {t = 0} {++t} END {print t}' $test_prog.out` test x"$expect" = x"$actual" + + +device=test:0 +sane_env scanimage++ --help --device-name $device > $test_prog.out 2>/dev/null +grep -ie "device options ($device):" $test_prog.out + +expect=`sane_env scanimage --help --device-name $device \ + | grep -ie "test options:"` +actual=`grep -ie "test options:" $test_prog.out` +test x"$expect" = x"$actual" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |