query_text.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : query_text                                                        *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Asks for a piece of information from the user with an associated         *
00009 *  message that describes what is expected.                                   *
00010 *                                                                             *
00011 *******************************************************************************
00012 * Copyright (c) 2006-$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/convert_utf.h>
00021 #include <basis/function.h>
00022 #include <basis/istring.h>
00023 #ifdef _AFXDLL
00024   #include <mfc_ext/info_edit.h>
00025   #include <mfc_ext/tiny_app.cpp>
00026   #include <mfc_ext/tiny_shell.h>
00027 #else
00028   #include <opsystem/application_shell.h>
00029 #endif
00030 #include <opsystem/ini_config.h>
00031 #include <data_struct/static_memory_gremlin.h>
00032 
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 
00036 #define BASE_LOG(to_print) program_wide_logger().log(to_print)
00037 #define LOG(to_print) STAMPED_EMERGENCY_LOG(program_wide_logger(), to_print)
00038 
00039 #ifdef _AFXDLL
00040 class query_text : public tiny_shell 
00041 #else
00042 class query_text : public application_shell 
00043 #endif
00044 {
00045 public:
00046 #ifdef _AFXDLL
00047   query_text() : tiny_shell("not to be seen", "query_text") {}
00048 #else
00049   query_text() : application_shell("query_text") {}
00050 #endif
00051   virtual ~query_text() {}
00052   IMPLEMENT_CLASS_NAME("query_text");
00053   virtual int execute();
00054 };
00055 
00057 
00058 const char *help_info = \
00059 "This program requires a title and a description as the first and second\r\n\
00060 parameters.  The third parameter is the initial value for the edit field\r\n\
00061 that the user fills in.  The fourth parameter must be an INI file where\r\n\
00062 the result of the edit will be written.  The INI file format is as follows:\r\n\
00063 \t[query]\r\n\
00064 \tanswer=USER-ENTERED-INFORMATION";
00065 
00066 int query_text::execute()
00067 {
00068   FUNCDEF("execute");
00069 
00070 
00071 
00072 #ifdef _AFXDLL
00073   ShowWindow(SW_HIDE);
00074 #endif
00075   if (__argc < 4) {
00076 #ifdef _AFXDLL
00077     MessageBox(to_unicode_temp(help_info),
00078         to_unicode_temp("Insufficient Parameters"), MB_OK);
00079     PostMessage(WM_CLOSE);
00080 #else
00081     BASE_LOG(help_info);
00082 #endif
00083     return 1;
00084   }
00085   istring title = __argv[1];
00086   istring description = __argv[2];
00087   istring initial_edit = __argv[3];
00088   istring outfile = __argv[4];
00089 
00090   // process description since we get handed some unresolved escape codes.
00091   for (int i = 0; i < description.length(); i++) {
00092     if ( (description[i] == '\\') && (i <= description.length() - 4) ) {
00093       // we got the escape code start at least and enough text for it...
00094       if ( (description[i + 1] == 'r') && (description[i + 2] == '\\')
00095           && (description[i + 3] == 'n') ) {
00096         // yep, this is our escape code.
00097         description.zap(i, i + 3);  // whack the code.
00098         description.insert(i, "\r\n");  // replace with the real code.
00099       }
00100     }
00101   }
00102 
00103   istring to_return;
00104   while (!to_return) {
00105 #ifdef _AFXDLL
00106     info_edit querier(0, initial_edit.s(), 100, description.s(), title.s());
00107     if (querier.DoModal() == IDOK) {
00108       // grab the string they entered and return it.
00109       to_return = querier.text();
00110     } else break;  // get out of loop due to unexpected return.
00111 #else
00112     BASE_LOG(title);
00113     BASE_LOG("-------");
00114     BASE_LOG(description);
00115     BASE_LOG(istring("default value: ") + initial_edit);
00116     BASE_LOG("Please enter a value for the query...");
00117     char buff[2049];
00118     if (fscanf(stdin, "%2047s", buff) >= 1)
00119       to_return = buff;
00120 
00121 BASE_LOG(istring("got answer of: \"") + to_return + "\"");
00122 
00123 #endif
00124   }
00125 
00126   if (to_return.t()) {
00127     ini_configurator ini(outfile, configurator::RETURN_ONLY);
00128     ini.store("query", "answer", to_return);
00129 #ifdef _AFXDLL
00130     PostMessage(WM_CLOSE);
00131 #endif
00132     return 0;  // non-error return.
00133   } else {
00134     // this is a failure outcome.  the user probably cancelled.
00135 #ifdef _AFXDLL
00136     PostMessage(WM_CLOSE);
00137 #endif
00138     return 1;  // error return.
00139   }
00140 }
00141 
00143 
00144 HOOPLE_MAIN(query_text, )
00145 

Generated on Fri Nov 21 04:29:05 2008 for HOOPLE Libraries by  doxygen 1.5.1