Ejemplo n.º 1
0
// This is the helper for QObject.pyqtConfigure().
PyObject *qpycore_pyqtconfigure(PyObject *self, PyObject *args, PyObject *kwds)
{
    // Check there are no positional arguments.
    if (PyTuple_Size(args) > 0)
    {
        PyErr_SetString(PyExc_TypeError,
                        "QObject.pyqtConfigure() has no positional arguments");
        return 0;
    }

    // Get the QObject self.
    QObject *qobj = reinterpret_cast<QObject *>(
                        sipGetCppPtr((sipSimpleWrapper *)self, sipType_QObject));

    if (!qobj)
        return 0;

    PyObject *name_obj, *value_obj;
    SIP_SSIZE_T pos = 0;

    while (PyDict_Next(kwds, &pos, &name_obj, &value_obj))
    {
        ArgStatus as = handle_argument(self, qobj, name_obj, value_obj);

        if (as == AsError)
            return 0;

        if (as == AsUnknown)
        {
#if PY_MAJOR_VERSION >= 3
            PyErr_Format(PyExc_AttributeError,
                         "'%S' is not the name of a Qt property or signal",
                         name_obj);
#else
            PyObject *name_s = PyObject_Str(name_obj);

            if (name_s != NULL)
            {
                PyErr_Format(PyExc_AttributeError,
                             "'%s' is not the name of a Qt property or signal",
                             PyString_AsString(name_s));

                Py_DECREF(name_s);
            }
#endif

            return 0;
        }
    }

    Py_INCREF(Py_None);
    return Py_None;
}
Ejemplo n.º 2
0
/**
 * \brief Parse commandline parameters in 'cmdline'.
 *
 * Parses 'cmdline' for kernel commandline parameters and sets configuration
 * variables accordingly.
 *
 * \param cmdline       Kernel commandline string.
 */
void parse_commandline(const char *cmdline, struct cmdarg *cmdargs)
{
    // Parse argument string into whitespace-separated 'var=val' tokens
    for(const char *p = strchr(cmdline, '='); p != NULL;
        p = strchr(p + 1, '=')) {
        const char *var = look_back(p, ' ') + 1, *val = p + 1;

        if(handle_argument(var, val, cmdargs) != 0) {
            assert(!"parse_commandline: Parse error in commandline");
        }
    }
}
Ejemplo n.º 3
0
// This is the helper for the QObject %FinalisationCode.
int qpycore_qobject_finalisation(PyObject *self, QObject *qobj, PyObject *kwds,
                                 PyObject **updated_kwds)
{
    // Handle the trivial case.
    if (!kwds)
        return 0;

    // The dict we will be removing handled arguments from, 0 if it needs to be
    // created.
    PyObject *unused = (updated_kwds ? 0 : kwds);

    PyObject *name_obj, *value_obj;
    SIP_SSIZE_T pos = 0;

    while (PyDict_Next(kwds, &pos, &name_obj, &value_obj))
    {
        ArgStatus as = handle_argument(self, qobj, name_obj, value_obj);

        if (as == AsError)
            return -1;

        if (as == AsHandled)
        {
            if (!unused)
            {
                unused = PyDict_Copy(kwds);

                if (!unused)
                    return -1;

                *updated_kwds = unused;
            }

            if (PyDict_DelItem(unused, name_obj) < 0)
            {
                if (updated_kwds)
                    Py_DECREF(unused);

                return -1;
            }
        }
    }

    return 0;
}
Ejemplo n.º 4
0
int argparse_parse(const ArgparseCommandLine *cmd, void *out, int argc, char **argv)
{
	std::unordered_map<std::string, const ArgparseOption *> switch_short_map;
	std::unordered_map<std::string, const ArgparseOption *> switch_long_map;
	bool has_short_help = false;
	int error = 0;

	for (size_t i = 0; i < cmd->num_switches; ++i) {
		const ArgparseOption *opt = cmd->switches + i;

		if (opt->short_name)
			switch_short_map[opt->short_name] = opt;
		if (opt->long_name && strcmp(opt->long_name, "help"))
			switch_long_map[opt->long_name] = opt;
	}
	has_short_help = (switch_short_map.find("h") == switch_short_map.end());

	try {
		int arg_index = 1;
		size_t positional_count = 0;
		bool positional_flag = false;
		std::string str;

		while (arg_index < argc) {
			const ArgparseOption *opt = nullptr;
			int adjust;
			int ret;

			str = argv[arg_index];

			if (!positional_flag) {
				if ((has_short_help && str == "-h") || str == "--help") {
					error = ARGPARSE_HELP;
					break;
				}
				if (!opt && str.find("--no-") == 0)
					opt = map_find_default(switch_long_map, str.substr(5));
				if (!opt && str.find("--") == 0)
					opt = map_find_default(switch_long_map, str.substr(2));
				if (!opt && str.find("-") == 0)
					opt = map_find_default(switch_short_map, str.substr(1));

				if (!opt)
					positional_flag = true;
			}

			if (positional_flag) {
				if (positional_count >= cmd->num_positional) {
					std::cerr << "too many positional arguments\n";
					error = ARGPARSE_ERROR;
					break;
				}

				opt = cmd->positional + positional_count++;
			}

			adjust = positional_flag ? 0 : 1;

			if ((ret = handle_argument(opt, out, argc - arg_index - adjust, argv + arg_index + adjust)) < 0) {
				std::cerr << "error parsing argument: " << (opt->short_name ? opt->short_name : opt->long_name) << '\n';
				error = ARGPARSE_ERROR;
				break;
			}
			if (positional_flag && ret == 0)
				throw std::logic_error{ "positional argument must advance argument list" };

			arg_index += ret + adjust;
		}

		if (arg_index >= argc && positional_count != cmd->num_positional) {
			const ArgparseOption *opt = cmd->positional + positional_count;

			std::cerr << "missing positional argument: ";
			std::cerr << (opt->short_name ? opt->short_name : opt->long_name) << '\n';
			error = ARGPARSE_ERROR;
		}
	} catch (const std::logic_error &e) {
		std::cerr << e.what() << '\n';
		error = ARGPARSE_FATAL;
	}

	if (error == ARGPARSE_HELP || error == ARGPARSE_ERROR)
		print_usage(cmd);

	return error;
}
Ejemplo n.º 5
0
int cpp_main(int argc, char * argv[])
{
   // start by processing the command line args:
   if(argc < 2)
      return show_usage();
   int result = 0;
   for(int c = 1; c < argc; ++c)
   {
      result += handle_argument(argv[c]);
   }
   if(result)
      return result;

   if(test_matches)
   {
      // start with a simple test, this is basically a measure of the minimal overhead
      // involved in calling a regex matcher:
      test_match("abc", "abc");
      // these are from the regex docs:
      test_match("^([0-9]+)(\\-| |$)(.*)$", "100- this is a line of ftp response which contains a message string");
      test_match("([[:digit:]]{4}[- ]){3}[[:digit:]]{3,4}", "1234-5678-1234-456");
      // these are from http://www.regxlib.com/
      test_match("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", "*****@*****.**");
      test_match("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", "*****@*****.**");
      test_match("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", "*****@*****.**");
      test_match("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$", "EH10 2QQ");
      test_match("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$", "G1 1AA");
      test_match("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$", "SW1 1ZZ");
      test_match("^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$", "4/1/2001");
      test_match("^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$", "12/12/2001");
      test_match("^[-+]?[[:digit:]]*\\.?[[:digit:]]*$", "123");
      test_match("^[-+]?[[:digit:]]*\\.?[[:digit:]]*$", "+3.14159");
      test_match("^[-+]?[[:digit:]]*\\.?[[:digit:]]*$", "-3.14159");
   }
   output_html_results(true, "%short_matches%");

   std::string file_contents;

   if(test_code)
   {
      load_file(file_contents, "../../../boost/crc.hpp");

      const char* highlight_expression = // preprocessor directives: index 1
                              "(^[ \t]*#(?:[^\\\\\\n]|\\\\[^\\n_[:punct:][:alnum:]]*[\\n[:punct:][:word:]])*)|"
                              // comment: index 2
                              "(//[^\\n]*|/\\*.*?\\*/)|"
                              // literals: index 3
                              "\\<([+-]?(?:(?:0x[[:xdigit:]]+)|(?:(?:[[:digit:]]*\\.)?[[:digit:]]+(?:[eE][+-]?[[:digit:]]+)?))u?(?:(?:int(?:8|16|32|64))|L)?)\\>|"
                              // string literals: index 4
                              "('(?:[^\\\\']|\\\\.)*'|\"(?:[^\\\\\"]|\\\\.)*\")|"
                              // keywords: index 5
                              "\\<(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import"
                              "|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall"
                              "|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool"
                              "|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete"
                              "|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto"
                              "|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected"
                              "|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast"
                              "|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned"
                              "|using|virtual|void|volatile|wchar_t|while)\\>"
                              ;

      const char* class_expression = "^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?" 
                   "(class|struct)[[:space:]]*(\\<\\w+\\>([ \t]*\\([^)]*\\))?" 
                   "[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?" 
                   "(\\{|:[^;\\{()]*\\{)";

      const char* include_expression = "^[ \t]*#[ \t]*include[ \t]+(\"[^\"]+\"|<[^>]+>)";
      const char* boost_include_expression = "^[ \t]*#[ \t]*include[ \t]+(\"boost/[^\"]+\"|<boost/[^>]+>)";


      test_find_all(class_expression, file_contents);
      test_find_all(highlight_expression, file_contents);
      test_find_all(include_expression, file_contents);
      test_find_all(boost_include_expression, file_contents);
   }
   output_html_results(false, "%code_search%");

   if(test_html)
   {
      load_file(file_contents, "../../../libs/libraries.htm");
      test_find_all("beman|john|dave", file_contents, true);
      test_find_all("<p>.*?</p>", file_contents, true);
      test_find_all("<a[^>]+href=(\"[^\"]*\"|[^[:space:]]+)[^>]*>", file_contents, true);
      test_find_all("<h[12345678][^>]*>.*?</h[12345678]>", file_contents, true);
      test_find_all("<img[^>]+src=(\"[^\"]*\"|[^[:space:]]+)[^>]*>", file_contents, true);
      test_find_all("<font[^>]+face=(\"[^\"]*\"|[^[:space:]]+)[^>]*>.*?</font>", file_contents, true);
   }
   output_html_results(false, "%html_search%");

   if(test_short_twain)
   {
      load_file(file_contents, "short_twain.txt");

      test_find_all("Twain", file_contents);
      test_find_all("Huck[[:alpha:]]+", file_contents);
      test_find_all("[[:alpha:]]+ing", file_contents);
      test_find_all("^[^\n]*?Twain", file_contents);
      test_find_all("Tom|Sawyer|Huckleberry|Finn", file_contents);
      test_find_all("(Tom|Sawyer|Huckleberry|Finn).{0,30}river|river.{0,30}(Tom|Sawyer|Huckleberry|Finn)", file_contents);
   }
   output_html_results(false, "%short_twain_search%");

   if(test_long_twain)
   {
      load_file(file_contents, "mtent13.txt");

      test_find_all("Twain", file_contents);
      test_find_all("Huck[[:alpha:]]+", file_contents);
      test_find_all("[[:alpha:]]+ing", file_contents);
      test_find_all("^[^\n]*?Twain", file_contents);
      test_find_all("Tom|Sawyer|Huckleberry|Finn", file_contents);
      time_posix = false;
      test_find_all("(Tom|Sawyer|Huckleberry|Finn).{0,30}river|river.{0,30}(Tom|Sawyer|Huckleberry|Finn)", file_contents);
      time_posix = true;
   }   
   output_html_results(false, "%long_twain_search%");

   output_final_html();
   return 0;
}