list_chooser.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : list_chooser                                                      *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Shows a list of options from a provided file and gets the user to pick   *
00009 *  one of the options.                                                        *
00010 *                                                                             *
00011 *******************************************************************************
00012 * Copyright (c) 1995-$now By Author.  This program is free software; you can  *
00013 * redistribute it and/or modify it under the terms of the GNU General Public  *
00014 * License as published by the Free Software Foundation; either version 2 of   *
00015 * the License or (at your option) any later version.  This is online at:      *
00016 *     http://www.fsf.org/copyleft/gpl.html                                    *
00017 * Please send any updates to: fred@gruntose.com                               *
00018 \*****************************************************************************/
00019 
00020 #include <basis/byte_array.h>
00021 #include <basis/istring.h>
00022 #include <opsystem/application_shell.h>
00023 #include <opsystem/byte_filer.h>
00024 #include <loggers/console_logger.h>
00025 #include <opsystem/application_shell.h>
00026 #include <opsystem/ini_config.h>
00027 #include <data_struct/static_memory_gremlin.h>
00028 #include <textual/byte_format.h>
00029 #include <textual/parser_bits.h>
00030 
00031 #ifdef __WIN32__
00032   #include <mfc_ext/list_dialog.h>
00033   #include <mfc_ext/tiny_app.cpp>
00034   #include <mfc_ext/tiny_shell.h>
00035 #endif
00036 
00037 #ifdef __WIN32__
00038   #define base_implementation tiny_shell
00039 #else
00040   #define base_implementation application_shell
00041 #endif
00042 
00043 class list_chooser : public base_implementation
00044 {
00045 public:
00046   list_chooser();
00047 
00048   IMPLEMENT_CLASS_NAME("list_chooser");
00049 
00050   int execute();
00051     // performs main body of test.
00052 };
00053 
00054 list_chooser::list_chooser()
00055 #ifdef __WIN32__
00056 : base_implementation("List Chooser", "list_chooser")
00057 #else
00058 : base_implementation("list_chooser")
00059 #endif
00060 {}
00061 
00062 int list_chooser::execute()
00063 {
00064 #ifdef __WIN32__
00065 
00066   ShowWindow(SW_HIDE);  // hide the tiny shell window.
00067 
00068   // set up our output file right now.
00069   istring ini_name = portable::env_string("TEMP") + "/" + "list_chooser.ini";
00070   ini_configurator ini(ini_name, configurator::RETURN_ONLY);
00071   ini.put("answer", "okay", "0");
00072   ini.put("answer", "choice", "");
00073 
00074   if (__argc < 4) {
00075     log("This program needs two parameters.  The first is the caption that will be used");
00076     log("in the list box.  The second is the number of lines to skip in the input file.");
00077     log("The third parameter is the filename from which to load the options that will");
00078     log("be shown in the list.");
00079     PostMessage(WM_CLOSE);
00080 
00081     return 34;
00082   }
00083 
00084   istring caption = __argv[1];
00085   int skip_count = istring(__argv[2]).convert(0);
00086   istring filename = __argv[3];
00087 
00088   byte_filer option_file(filename, "rb");
00089   if (!option_file.good()) {
00090     log(istring("Could not open the file named: ") + filename);
00091     PostMessage(WM_CLOSE);
00092 
00093     return 35;
00094   }
00095 
00096   list_dialog list(this, caption);
00097 
00098   int skippy = 0;
00099   while (true) {
00100     istring next_option;
00101     int chars_read = option_file.getline(next_option, 255);
00102     if (!chars_read) break;
00103     skippy++;
00104     if (skippy <= skip_count) continue;  // not ready yet.
00105     // clean EOL chars off.
00106     next_option.shrink();
00107     while (next_option[0] == ' ') {
00108       next_option.zap(0, 0);
00109     }
00110     while (parser_bits::is_eol(next_option[next_option.end()])
00111         || (next_option[next_option.end()] == ' ') ) {
00112       next_option.zap(next_option.end(), next_option.end());
00113     }
00114 //log(text_dump(byte_array(next_option.length() + 1, (byte *)next_option.s())));
00115     list.add_string(next_option.s(), true);
00116   }
00117   option_file.close();
00118 
00119   list.SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
00120 
00121   if (list.DoModal() != IDOK) {
00122     PostMessage(WM_CLOSE);
00123     return 36;
00124   }
00125 
00126   istring curr;
00127   list.get_current(curr);
00128 
00129   while (parser_bits::is_eol(curr[curr.end()])) {
00130     curr.zap(curr.end(), curr.end());
00131   }
00132 
00133   ini.put("answer", "choice", curr);
00134   ini.put("answer", "okay", "1");
00135 
00136   PostMessage(WM_CLOSE);
00137 
00138 #endif //win32
00139 
00140   return 0;
00141 }
00142 
00143 HOOPLE_MAIN(list_chooser, )
00144 

Generated on Fri Nov 28 04:28:51 2008 for HOOPLE Libraries by  doxygen 1.5.1