Exemplo n.º 1
0
    bool ReverseSublist::test() {
        vector<int> vec = {0, 11, 5, 3, 7, 2};
        vector<int> sv = {0, 11, 7, 3, 5, 2};
        shared_ptr<ListNode<int>> list = createList(vec);
        shared_ptr<ListNode<int>> shouldBe = createList(sv);
        shared_ptr<ListNode<int>> res = reverseSublist(list, 2, 4);

        if ( !is_equal(res, shouldBe) ) {
            cout << "Should be: " << list_to_string(shouldBe) << endl;
            cout << "But result: " << list_to_string(res) << endl;
            return false;
        }

        return true;
    }
Exemplo n.º 2
0
    bool EvenOddMerge::test() {
        vector<int> vec1 = {0, 1, 2, 3, 4, 5, 6};
        vector<int> vec2 = {0, 2, 4, 6, 1, 3, 5};
        shared_ptr<ListNode<int>> list1 = createList(vec1);
        shared_ptr<ListNode<int>> list2 = createList(vec2);

        auto head = evenOddMerge(list1);

        if ( !is_equal(head, list2) ) {
            cout << "Should be: " << list_to_string(list2) << endl;
            cout << "But result: " << list_to_string(head) << endl;

            return false;
        }

        return true;
    }
Exemplo n.º 3
0
    std::string tv_to_string(caValue* value)
    {
        ca_assert(is_list(value));
        if (is_compound_type(value->value_type))
            return compound_type_to_string(value);

        return list_to_string((ListData*) get_pointer(value));
    }
Exemplo n.º 4
0
char	*resolve_part(t_linked_list *line, t_linked_list *line_strings, int start, int end)
{
	FILE			*fp;
	t_linked_list	*tmp_command;
	int				i;
	char			out[100];
	
	(void)start;
	(void)end;
	tmp_command = new_list();
	add_to_list(tmp_command, ft_strdup("echo \""));
	i = 0;
	while (*((char*)line->elts[i]) == INDENT)
		i++;
	while (i < line->len)
	{
		if (*((char*)line->elts[i]) == FUNCTION_NAME)
		{
			if (function_is_builtin((char*)line_strings->elts[i]))
				add_to_list(tmp_command, get_c_function_name((char*)line_strings->elts[i]));
			else
			{
				add_to_list(tmp_command, ft_strdup("__"));
				add_to_list(tmp_command, (char*)line_strings->elts[i]);
			}
		}
		else
			add_to_list(tmp_command, (char*)line_strings->elts[i]);
		add_to_list(tmp_command, ft_strdup(" "));
		i++;
	}
	add_to_list(tmp_command, ft_strdup(";\" | ./grammar/a.out"));
	printf("command : %s\n", list_to_string(tmp_command));
	fp = popen(list_to_string(tmp_command), "r");
	fgets(out, 100, fp);
	printf("RESULT : %s\n", out);
	return (ft_strdup(out));
}
Exemplo n.º 5
0
/*
** Set the group "ACLs:" line.
*/
int group_acls(const char *argv[])
	{
	const char *group = argv[0];
	char *acls;
	int retval;

	if(!group)
		{
		fputs(_("You must specify a group and a (possibly empty) list\n"
				"of PPR access control lists.\n"), errors);
		return EXIT_SYNTAX;
		}

	acls = list_to_string(&argv[1]);
	retval = conf_set_name(QUEUE_TYPE_GROUP, group, "ACLs", acls ? "%s" : NULL, acls);
	if(acls) gu_free(acls);

	return retval;
	} /* end of group_acls() */
Exemplo n.º 6
0
/*
** Set the group "PassThru:" line.
*/
int group_passthru(const char *argv[])
	{
	const char *group = argv[0];
	char *passthru;
	int retval;

	if(!group)
		{
		fputs(_("You must specify a group and a (possibly empty) list\n"
				"of file types.  These file types should be the same as\n"
				"those used with the \"ppr -T\" option.\n"), errors);
		return EXIT_SYNTAX;
		}

	passthru = list_to_string(&argv[1]);
	retval = conf_set_name(QUEUE_TYPE_GROUP, group, "PassThru", passthru ? "%s" : NULL, passthru);
	if(passthru) gu_free(passthru);

	return retval;
	} /* end of group_passthru() */
Exemplo n.º 7
0
char		*get_next_line(const int fd)
{
  static t_char	*begin;
  static t_char	*end;
  char		*line;
  int		size;

  if (begin == NULL || (size = get_line_length(begin)) <= 0)
    fill_list(fd, &begin, &end);
  size = get_line_length(begin);
  if (size == 0 && begin == NULL)
    {
      if (begin != NULL)
	{
	  free(begin->to_free);
	  free(begin);
	}
      return (NULL);
    }
  if (size < 0)
    size = -size;
  line = list_to_string(&begin, &end, size);
  return (line);
}
Exemplo n.º 8
0
static void
write_check_combos (void)
{
    int i, n;
    index_list_t *q;
    FILE *f;
    f = fopen("check_combos.c", "w");
    fprintf(f, "/* THIS FILE IS AUTOMATICALLY GENERATED BY table */\n\n");
    fprintf(f, "#include <stddef.h>\n");
    fprintf(f, "#include \"options.h\"\n");
    fprintf(f, "#include \"option_seen.h\"\n");
    fprintf(f, "#include \"option_names.h\"\n");
    fprintf(f, "#include \"opt_actions.h\"\n");
    fprintf(f, "#include \"errors.h\"\n");
    fprintf(f, "\n");
    fprintf(f, "/* replace individual options with combo */\n");
    fprintf(f, "static void\n");
    fprintf(f, "replace_with_combo (int combo_index)\n");
    fprintf(f, "{\n");
    fprintf(f, "\tint flag;\n");
    fprintf(f, "\tint count = 1;\n");
    fprintf(f, "\tFOREACH_OPTION_IN_COMBO(flag,combo_index) {\n");
    fprintf(f, "\t\tif (count == 1) {\n");
    fprintf(f, "\t\t\treplace_option_seen(flag, combo_index);\n");
    fprintf(f, "\t\t} else {\n");
    fprintf(f, "\t\t\tset_option_unseen(flag);\n");
    fprintf(f, "\t\t}\n");
    fprintf(f, "\t\tcount++;\n");
    fprintf(f, "\t}\n");
    fprintf(f, "}\n\n");
    fprintf(f, "static void\n");
    fprintf(f, "report_combo_errors (void)\n");
    fprintf(f, "{\n");
    for (i = 0; i < num_options; i++) {
        if (options[i].syntax == combo && !EMPTY(options[i].action)) {
            if (find_by_flag(options[i].flag, i))
                continue;
            fprintf(f, "\tif (option_was_seen(%s)) {\n",
                    options[i].flag);
            if (strcmp(options[i].action, "WARNING") == 0) {
                fprintf(f, "\t\twarning(\"%s combination not allowed, replaced with %s\");\n",
                        options[i].name, list_to_string(options[i].implies));
            } else {
                fprintf(f, "\t\tparse_error(\"%s\", \"illegal combination\");\n",
                        options[i].name);
            }
            fprintf(f, "\t}\n");
        }
    }
    fprintf(f, "}\n\n");
    fprintf(f, "extern boolean\n");
    fprintf(f, "is_replacement_combo (int combo_index)\n");
    fprintf(f, "{\n");
    fprintf(f, "\tswitch (combo_index) {\n");
    for (i = n = 0; i < num_options; i++) {
        if (options[i].syntax == combo && !EMPTY(options[i].action)
                && strcmp(options[i].action, "WARNING") == 0 )
        {
            if (find_by_flag(options[i].flag, i))
                continue;
            fprintf(f, "\tcase %s:\n", options[i].flag);
            n++;
        }
    }
    if (n)
        fprintf(f, "\t\treturn TRUE;\n");
    fprintf(f, "\tdefault:\n");
    fprintf(f, "\t\treturn FALSE;\n");
    fprintf(f, "\t}\n");
    fprintf(f, "}\n\n");
    fprintf(f, "extern void\n");
    fprintf(f, "check_for_combos (void)\n");
    fprintf(f, "{\n");
    for (i = 0; i < num_options; i++) {
        if (options[i].syntax == combo) {
            if (find_by_flag(options[i].flag, i))
                continue;
            q = options[i].combo_list;
            if (q == NULL) internal_error("empty combo_list?");
            fprintf(f, "\tif (");
            if (q->negated) fprintf(f, "!");
            fprintf(f, "option_was_seen(%s)",
                    options[q->info_index].flag);
            q = q->next;
            while (q != NULL) {
                fprintf(f, " && ");
                if (q->negated) fprintf(f, "!");
                fprintf(f, "option_was_seen(%s)",
                        options[q->info_index].flag);
                q = q->next;
            }
            fprintf(f, ") {\n");
            /* replace in seen array */
            fprintf(f, "\t\treplace_with_combo(%s);\n",
                    options[i].flag);
            /* untoggle individual options */
            for (q = options[i].combo_list; q != NULL; q = q->next) {
                if (options[q->info_index].toggle) {
                    fprintf(f, "\t\tun%s\n",
                            options[q->info_index].action);
                }
            }
            fprintf(f, "\t}\n");
        }
    }
    fprintf(f, "\treport_combo_errors();\n");
    fprintf(f, "}\n\n");
    fclose(f);
}
Exemplo n.º 9
0
Arquivo: db.c Projeto: hannenz/busy
gint db_host_add(Host *host, MYSQL *mysql) {
    gchar *query, *schedule, *excludes, *ips, *srcdirs;
    gint ret, id = -1;
    MYSQL_RES *result;
    MYSQL_ROW row;

    schedule = list_to_string(host->schedule);
    excludes = list_to_string(host->excludes);
    srcdirs = list_to_string(host->srcdirs);
    ips = list_to_string(host->ips);

    // Check if we have a host with this name already
    query = g_strdup_printf("SELECT * FROM `hosts` WHERE `name`=\"%s\";", host_get_name(host));
    ret = mysql_real_query(mysql, query, strlen(query));
    g_free(query);

    if (ret != 0) {
        syslog (LOG_ERR, "MySQL Query failed: %s (%d: %s)", query, ret, mysql_error(mysql));
        return (-1);
    }

    result = mysql_store_result(mysql);

    // If we have more than one, delete all and create a new host
    gint n;
    n = mysql_num_rows(result);
    if (n > 1) {
        query = g_strdup_printf("DELETE FROM `hosts` WHERE `name`=\"%s\"", host_get_name(host));
        mysql_real_query(mysql, query, strlen(query));
        g_free(query);
        n = 0;
    }

    // Cases: No such host and replace existing host
    switch (n) {
    case 1:
        // Replace existing host;
        row = mysql_fetch_row(result);
        id = atoi(row[0]);
        query = g_strdup_printf("UPDATE `hosts` SET `name`='%s', `hostname`='%s', `user`='%s', `max_incr`='%u', `max_age`='%f', `max_age_full`='%f', `max_age_incr`='%f', `backupdir`='%s', `archivedir`='%s', `schedule`='%s', `excludes`='%s', `ips`='%s', `srcdirs`='%s' WHERE id='%u'",
                                host_get_name(host),
                                host_get_hostname(host),
                                host_get_user(host),
                                host_get_max_incr(host),
                                host_get_max_age(host),
                                host_get_max_age_full(host),
                                host_get_max_age_incr(host),
                                host_get_backupdir(host),
                                host_get_archivedir(host),
                                schedule,
                                excludes,
                                ips,
                                srcdirs,
                                id
                               );
        break;

    case 0:
        query = g_strdup_printf("INSERT INTO `hosts` (`name`, `hostname`, `user`, `max_incr`, `max_age_full`, `max_age`, `max_age_incr`, `backupdir`, `archivedir`, `schedule`, `excludes`, `ips`, `srcdirs`) VALUES ('%s', '%s', '%s', '%d', '%.5f', '%.5f', '%.5f', '%s', '%s', '%s', '%s', '%s'. '%s')",
                                host_get_name(host),
                                host_get_hostname(host),
                                host_get_user(host),
                                host_get_max_incr(host),
                                host_get_max_age(host),
                                host_get_max_age_full(host),
                                host_get_max_age_incr(host),
                                host_get_backupdir(host),
                                host_get_archivedir(host),
                                schedule,
                                excludes,
                                ips,
                                srcdirs
                               );
        id = mysql_insert_id(mysql);
        break;

    default:
        break;

    }
    mysql_free_result(result);

    if ((ret = mysql_real_query(mysql, query, strlen(query))) != 0) {
        syslog (LOG_ERR, "MySQL Query failed: %s (%d: %s)", query, ret, mysql_error(mysql));
    }
    g_free(query);
    g_free(excludes);
    g_free(ips);
    g_free(schedule);
    g_free(srcdirs);

    return (id);
}
Exemplo n.º 10
0
static GtkTreeModel *
create_lang_model (void)
{
    GtkTreeStore *store;
    MooLangMgr *mgr;
    GSList *langs, *sections, *l;
    GtkTreeIter iter;
    const char *config;
    char *ext, *mime;

    mgr = moo_lang_mgr_default ();
    langs = g_slist_sort (moo_lang_mgr_get_available_langs (mgr), (GCompareFunc) lang_cmp);
    sections = moo_lang_mgr_get_sections (mgr);

    store = gtk_tree_store_new (6, G_TYPE_STRING, G_TYPE_STRING, MOO_TYPE_LANG,
                                G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

    gtk_tree_store_append (store, &iter, NULL);
    config = _moo_lang_mgr_get_config (mgr, MOO_LANG_NONE);
    ext = list_to_string (_moo_lang_mgr_get_globs (mgr, NULL), TRUE);
    mime = list_to_string (_moo_lang_mgr_get_mime_types (mgr, NULL), TRUE);
    gtk_tree_store_set (store, &iter,
                        COLUMN_ID, MOO_LANG_NONE,
                        /* Label in the Language combo box on Languages preferences page,
                           remove the part before and including | */
                        COLUMN_NAME, Q_("Language|None"),
                        COLUMN_CONFIG, config,
                        COLUMN_MIMETYPES, mime,
                        COLUMN_EXTENSIONS, ext,
                        -1);
    g_free (ext);
    g_free (mime);

    while (sections)
    {
        char *section = sections->data;

        gtk_tree_store_append (store, &iter, NULL);
        gtk_tree_store_set (store, &iter, COLUMN_NAME, section, -1);

        for (l = langs; l != NULL; l = l->next)
        {
            MooLang *lang = l->data;

            if (!_moo_lang_get_hidden (lang) &&
                !strcmp (_moo_lang_get_section (lang), section))
            {
                GtkTreeIter child;

                ext = list_to_string (_moo_lang_mgr_get_globs (mgr, _moo_lang_id (lang)), TRUE);
                mime = list_to_string (_moo_lang_mgr_get_mime_types (mgr, _moo_lang_id (lang)), TRUE);

                config = _moo_lang_mgr_get_config (mgr, _moo_lang_id (lang));
                gtk_tree_store_append (store, &child, &iter);
                gtk_tree_store_set (store, &child,
                                    COLUMN_ID, _moo_lang_id (lang),
                                    COLUMN_NAME, _moo_lang_display_name (lang),
                                    COLUMN_LANG, lang,
                                    COLUMN_CONFIG, config,
                                    COLUMN_MIMETYPES, mime,
                                    COLUMN_EXTENSIONS, ext,
                                    -1);

                g_free (ext);
                g_free (mime);
            }
        }

        g_free (section);
        sections = g_slist_delete_link (sections, sections);
    }

    g_slist_foreach (langs, (GFunc) g_object_unref, NULL);
    g_slist_free (langs);
    return GTK_TREE_MODEL (store);
}
Exemplo n.º 11
0
void ListData::dump()
{
    std::string s = list_to_string(this);
    printf("%s\n", s.c_str());
}