Example #1
0
int main(int argc, char ** argv) {
    int line = 1;

    /*  Open file specified on command line  */

    FILE * fp = get_file_from_cmdline(argc, argv);
    if ( fp == NULL ) {
        return EXIT_FAILURE;
    }

    /*  Initialize and populate binary search tree  */

    bs_tree tree = bs_tree_init(cds_compare_string, NULL);
    populate_tree(fp, tree);

    /*  Print sorted input via inorder left traverse of tree  */

    bs_tree_inorder_left_traverse(tree, print_node, &line);

    /*  Free tree and close file  */

    bs_tree_free(tree);

    if ( fclose(fp) != 0 ) {
        fprintf(stderr, "bstree_sort: couldn't close file (%s)\n",
                    strerror(errno));
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
Example #2
0
    void load(const xmlDocPtr doc, xml_node &node)
    {
        if (!doc)
        {
            std::string msg("XML document not well formed");
            xmlError * error = xmlCtxtGetLastError( ctx_ );
            if (error)
            {
                msg += ":\n";
                msg += error->message;
                throw config_error(msg, error->line, error->file);
            }
            else
            {
                throw config_error(msg);
            }
        }

        int iXIncludeReturn = xmlXIncludeProcessFlags(doc, options_);

        if (iXIncludeReturn < 0)
        {
            xmlFreeDoc(doc);
            throw config_error("XML XInclude error.  One or more files failed to load.");
        }

        xmlNode * root = xmlDocGetRootElement(doc);
        if (!root) {
            xmlFreeDoc(doc);
            throw config_error("XML document is empty.");
        }

        populate_tree(root, node);
        xmlFreeDoc(doc);
    }
Example #3
0
static void test_levelorder(struct cag_test_series *tests)
{
	char c[1000];
	complex_tree t;
	const char *exp = "3-B(0) \n"
		"1-B(3) 7-R(3) \n"
		"0-B(1) 2-B(1) \t5-B(7) 9-B(7) \n"
		"4-B(5) 6-B(5) \t8-B(9) 11-R(9) \n"
		"10-B(11) 13-B(11) \n"
		"12-R(13) 14-R(13) ";

	new_complex_tree(&t);
	strcpy(c, "\0");
	populate_tree(&t, 0, 15, 1);
	CAG_TEST(*tests, check_integrity_complex_tree(&t, t.root),
		 "cag_tree: integrity after populate tree");
	CAG_TEST(*tests, distance_complex_tree(begin_complex_tree(&t),
					       end_complex_tree(&t)) == 15,
		 "cag_tree: distance after populate tree");
	levelorder_complex_tree(&t, c, sprintf_complex_tree2);


	CAG_TEST(*tests, strcmp(c, exp) == 0,
		 "cag_tree: inorder");
	free_complex_tree(&t);
}
Example #4
0
    void load(std::basic_istream<char> &stream, xml_node &node)
    {
        stream.unsetf(std::ios::skipws);
        std::vector<char> v(std::istreambuf_iterator<char>(stream.rdbuf()),
                            std::istreambuf_iterator<char>());
        if (!stream.good())
        {
            throw config_error("Could not load map file", 0, filename_);
        }
        v.push_back(0); // zero-terminate
        try
        {
            // Parse using appropriate flags
            const int f_tws = rapidxml::parse_normalize_whitespace
                              | rapidxml::parse_trim_whitespace;
            rapidxml::xml_document<> doc;
            doc.parse<f_tws>(&v.front());

            for (rapidxml::xml_node<char> *child = doc.first_node();
                    child; child = child->next_sibling())
            {
                populate_tree(child, node);
            }
        }
        catch (rapidxml::parse_error &e)
        {
            long line = static_cast<long>(
                            std::count(&v.front(), e.where<char>(), '\n') + 1);
            throw config_error(e.what(), line, filename_);
        }
    }
Example #5
0
    void populate_tree(rapidxml::xml_node<char> *cur_node, xml_node &node)
    {
        switch (cur_node->type())
        {
        case rapidxml::node_element:
        {
            xml_node &new_node = node.add_child((char *)cur_node->name(), 0, false);
            // Copy attributes
            for (rapidxml::xml_attribute<char> *attr = cur_node->first_attribute();
                 attr; attr = attr->next_attribute())
            {
                new_node.add_attribute(attr->name(), attr->value());
            }

            // Copy children
            for (rapidxml::xml_node<char> *child = cur_node->first_node();
                 child; child = child->next_sibling())
            {
                populate_tree(child, new_node);
            }
        }
        break;

        // Data nodes
        case rapidxml::node_data:
        case rapidxml::node_cdata:
        {
            node.add_child(cur_node->value(), 0, true);
        }
        break;
        default:
            break;
        }
    }
Example #6
0
static void test_find(struct cag_test_series *tests)
{
	complex_tree l;
	it_complex_tree it;
	struct complex c;

	c.imag = -9;

	new_complex_tree(&l);
	populate_tree(&l, 0, 10, 1);
	it = find_all_complex_tree(&l,c, find_element);
	CAG_TEST(*tests, it && it->value.real == 9.0,
		 "cag_tree: find element");
	c.imag = -10;
	it = find_all_complex_tree(&l, c, find_element);
	CAG_TEST(*tests, it == end_complex_tree(&l),
		 "cag_tree: find element");
	c.imag = -9.0;
	it = findp_all_complex_tree(&l, &c, find_element);
	CAG_TEST(*tests, it && it->value.real == 9.0,
		 "cag_tree: find element");
	c.imag = -10;
	it = findp_all_complex_tree(&l, &c, find_element);
	CAG_TEST(*tests, it == end_complex_tree(&l),
		 "cag_tree: find element");
	c.imag = -9.0;
	free_complex_tree(&l);
}
Example #7
0
static void test_copy(struct cag_test_series *tests)
{
	complex_tree a, b, c;
	it_complex_tree it1, it2;
	new_complex_tree(&a);
	new_complex_tree(&b);
	new_complex_tree(&c);
	populate_tree(&a, 0, 5, 1);
	CAG_CHECK(copy_all_complex_tree(&a, &b), "Assignment failure");
	for (it1 = beg_complex_tree(&a), it2 = begin_complex_tree(&b);
	     it1 != end_complex_tree(&a);
	     it1 = next_complex_tree(it1), it2 = next_complex_tree(it2))
		CAG_TEST(*tests, it1->value.real == it2->value.real &&
			 it1->value.imag == it2->value.imag,
			 "cag_tree: assigned values are equal");
	CAG_TEST(*tests, distance_all_complex_tree(&a) ==
		 distance_all_complex_tree(&b),
		"cag_array: assigned array same size");
	CAG_CHECK(rcopy_all_complex_tree(&a, &c), "Assignment failure");
	for (it1 = beg_complex_tree(&a), it2 = begin_complex_tree(&c);
	     it1 != end_complex_tree(&a);
	     it1 = next_complex_tree(it1), it2 = next_complex_tree(it2))
		CAG_TEST(*tests, it1->value.real == it2->value.real &&
			 it1->value.imag == it2->value.imag,
			 "cag_tree: assigned values are equal");
	CAG_TEST(*tests, distance_all_complex_tree(&a) ==
		 distance_all_complex_tree(&c),
		"cag_array: reverse assigned array same size");
error:
	free_complex_tree(&a);
	free_complex_tree(&b);
	free_complex_tree(&c);
}
Example #8
0
    void populate_tree(xmlNode *cur_node, xml_node &node)
    {
        for (; cur_node; cur_node = cur_node->next )
        {
            switch (cur_node->type)
            {
            case XML_ELEMENT_NODE:
            {

                xml_node &new_node = node.add_child((const char *)cur_node->name, cur_node->line, false);
                append_attributes(cur_node->properties, new_node);
                populate_tree(cur_node->children, new_node);
            }
            break;
            case XML_TEXT_NODE:
            {
                std::string trimmed((const char*)cur_node->content);
                mapnik::util::trim(trimmed);
                if (trimmed.empty()) break; //Don't add empty text nodes
                node.add_child(trimmed, cur_node->line, true);
            }
            break;
            case XML_COMMENT_NODE:
                break;
            default:
                break;

            }
        }
    }
Example #9
0
static void test_search(struct cag_test_series *tests)
{
	complex_tree t;
	it_complex_tree it;
	struct complex c, d, e, f;

	c.real = 3.0;
	c.imag = -3.0;
	d.real = -1;
	d.imag = 1.0;
	e.real = 2.5;
	e.imag = 2.5;
	f.real = 8.0;
	f.imag = -8.0;
	new_complex_tree(&t);
	populate_tree(&t, 0, 5, 1);
	populate_tree(&t, 0, 5, 1);

	it = get_complex_tree(&t, c);
	CAG_TEST(*tests, it && it->value.real == 3.0
		 && it->value.imag == -3.0,
		 "cag_tree: Value found");
	it = get_complex_tree(&t, d);
	CAG_TEST(*tests, !it, "cag_tree: Value not found < all");
	it = get_complex_tree(&t, e);
	CAG_TEST(*tests, !it, "cag_tree: Value not found in between");
	it = get_complex_tree(&t, f);
	CAG_TEST(*tests, !it, "cag_tree: Value not found > all");

	it = getp_complex_tree(&t, &c);
	CAG_TEST(*tests, it && it->value.real == 3.0
		 && it->value.imag == -3.0,
		 "cag_tree: Value found");
	it = getp_complex_tree(&t, &d);
	CAG_TEST(*tests, !it, "cag_tree: Value not found < all");
	it = getp_complex_tree(&t, &e);
	CAG_TEST(*tests, !it, "cag_tree: Value not found in between");
	it = getp_complex_tree(&t, &f);
	CAG_TEST(*tests, !it, "cag_tree: Value not found > all");
	free_complex_tree(&t);
}
int main()
 {
	struct tnode *root;
	char node_urls[10][MAX_URL_LENGTH];
	int url_frequency[10];
 	root = NULL;
	root = add_to_tree(root, "google.com");
	root = add_to_tree(root, "google.com");
	assert( root->count == 2);
	root = add_to_tree(root, "amazon.com");
 	assert(treeprint(0,root,node_urls, url_frequency) == 2);
	
       	populate_tree("small_url", &root);
	test_print(root, 7, Expected_URLs_small, Expected_Url_Freq_small);

       	populate_tree("long_url", &root);
	assert( lookup(root, "vonage.com") == 2323);
	test_print(root, 10, Expected_URLs, Expected_Url_Freq);
	
	return 0;
 }
Example #11
0
static void
caja_column_chooser_constructed (GObject *object)
{
    CajaColumnChooser *chooser;

    chooser = CAJA_COLUMN_CHOOSER (object);

    populate_tree (chooser);

    g_signal_connect (chooser->details->store, "row_deleted",
                      G_CALLBACK (row_deleted_callback), chooser);
}
static void
nautilus_column_chooser_constructed (GObject *object)
{
	NautilusColumnChooser *chooser;

	chooser = NAUTILUS_COLUMN_CHOOSER (object);

	populate_tree (chooser);

	g_signal_connect (chooser->details->store, "row-deleted", 
			  G_CALLBACK (row_deleted_callback), chooser);
}
Example #13
0
struct Group* populate_tree(pugi::xml_node parent, int id=1)
{
	// TODO validate data

	struct Group* g = new Group;
    g->total_subgroups = 0;
    g->id = id;

	try
    {
		int children_number;
        int current_id = g->id+1;

        g->name = parent.child_value("name");
        g->attrs = static_cast<unsigned char>(std::stoi(parent.child_value("attributes")));
        g->max_users = std::stol(parent.child_value("max_users"));
        g->online_users = std::stol(parent.child_value("online_users"));

        if (g->attrs >= (1 << 7)) // 2^7 = 128, if the number is greater than 128 then it's a Root
        {
        	g->father = nullptr;
        }

        pugi::xml_node sub_g = parent.child("subgroups");
        pugi::xml_attribute sub_attr = sub_g.first_attribute();
        //assert(sub_attr.name() == "num");

        children_number = std::stoi(sub_attr.value());
        g->children.reserve(children_number);

        if (children_number > 0)
        {
            pugi::xml_node node_list = parent.child("subgroups");
            for (pugi::xml_node temp = node_list.first_child(); temp; temp = temp.next_sibling())
            {
                // assert (temp.name() == "Group")
                struct Group* temp_group = populate_tree(temp, current_id);
                temp_group->father = g;
                g->total_subgroups += temp_group->total_subgroups + 1;
                current_id += temp_group->total_subgroups+1;
                g->children.push_back(temp_group);
            }
    	}
    	return g; // delete it!
    }
    catch (...)
    {
    	std::cerr << "\nError while parsing.";
    	return nullptr;
    }
}
Example #14
0
static void test_new(struct cag_test_series *tests)
{
	complex_tree ct1, ct2, ct3;
	complexp_tree cpt;
	string_tree st;
	int i;

	CAG_TEST(*tests, new_complex_tree(&ct1),
		 "cag_tree: new complex tree");
	CAG_TEST(*tests, beg_complex_tree(&ct1) == end_complex_tree(&ct1),
		 "cag_tree: begin == end after new for complex tree");
	free_complex_tree(&ct1);

	CAG_TEST(*tests, new_complexp_tree(&cpt),
		 "cag_tree: new complex pointer tree");
	free_complexp_tree(&cpt);

	CAG_TEST(*tests, new_string_tree(&st),
		 "cag_tree: new string tree");
	free_string_tree(&st);

	CAG_CHECK( (i = new_many_complex_tree(&ct1, &ct2, &ct3, NULL)) == 3,
		   "failure many new");
	CAG_TEST(*tests, i == 3,
		 "cag_tree: multiple new");
	populate_tree(&ct1, 0, 5, 1);
	CAG_TEST(*tests, distance_all_complex_tree(&ct1) == 5,
		 "cag_test: tree populated after batch new 1");
	populate_tree(&ct2, 0, 5, 1);
	CAG_TEST(*tests, distance_all_complex_tree(&ct2) == 5,
		 "cag_test: tree populated after batch new 2");
	populate_tree(&ct3, 0, 5, 1);
	CAG_TEST(*tests, distance_all_complex_tree(&ct3) == 5,
		 "cag_test: tree populated after batch new 3");
error:
	free_many_complex_tree(i, &ct1, &ct2, &ct3, NULL);
}
Example #15
0
static void test_front_back(struct cag_test_series *tests)
{
	complex_tree t;
	struct complex c;

	new_complex_tree(&t);
	populate_tree(&t, 1, 5, 1);
	c = *front_complex_tree(&t);
	CAG_TEST(*tests, c.real == 1.0 && c.imag == -1.0,
		 "cag_tree: Retrieve front value");
	c = *back_complex_tree(&t);
	CAG_TEST(*tests, c.real == 4.0 && c.imag == -4.0,
		 "cag_tree: Retrieve back value");
	free_complex_tree(&t);
}
Example #16
0
static void test_postorder(struct cag_test_series *tests)
{
	char c[100];
	complex_tree t;

	new_complex_tree(&t);
	strcpy(c, "\0");
	populate_tree(&t, 0, 15, 1);
	CAG_TEST(*tests, check_integrity_complex_tree(&t, t.root),
		 "cag_tree: integrity after populate tree");
	CAG_TEST(*tests, distance_complex_tree(begin_complex_tree(&t),
					       end_complex_tree(&t)) == 15,
		 "cag_tree: distance after populate tree");
	postorder_complex_tree(t.root, c, sprintf_complex_tree);
	CAG_TEST(*tests, strcmp(c, "0 2 1 4 6 5 8 10 12 14 13 11 9 7 3 ") == 0,
		 "cag_tree: inorder");
	free_complex_tree(&t);
}
Example #17
0
File: main.c Project: Rubusch/c
void select_insert(unsigned int* number_of_nodes)
{
  unsigned int select = 0;
  do{
    printf("\n\navailable insert options are:\n");
    printf("0\tcancel\n");
    printf("1\tpopulate\n");
    printf("2\tinsert an element\n");
    readdigit(&select, "select an operation:");

    if(1 == select){
      if(0 == *number_of_nodes){
	puts("the tree is empty, first create a tree root\nfailed.");
      }else if(1 <= *number_of_nodes){
	unsigned int number=0;
	do{
	  readnumber(&number, NUM_OF_NODES_DIGITS, "enter a number of nodes");
	}while((number < 0) || (number > MAX_NODES));
	populate_tree(number_of_nodes, number);
	printf("the tree was populated and now contains %d elements\ndone.\n", *number_of_nodes);
      }else{
	printf("%d elements\nfailed.\n", *number_of_nodes);
      }
      break;

    }else if(2 == select){
      if(0 == *number_of_nodes){
	puts("the tree is empty, first create a tree root");
      }else if(MAX_NODES == *number_of_nodes){
	printf("the tree is stuffed, it already contains %d nodes, (max. %d nodes possible)\n", *number_of_nodes, MAX_NODES);
      }else{
	puts("enter a data value for the new node");
	if(0 > add_element(number_of_nodes)){
	  puts("failed.");
	}else{
	  printf("%d elements\ndone.\n", *number_of_nodes);
	}    
      }
      break;
    }
  }while(0 != select);
}
Example #18
0
static void test_at(struct cag_test_series *tests)
{
	complex_tree t;
	it_complex_tree it;

	new_complex_tree(&t);
	populate_tree(&t, 0, 10, 1);
	it = at_complex_tree(beg_complex_tree(&t), 0);
	CAG_TEST(*tests, it == beg_complex_tree(&t),
		"cag_tree: at 0 index");
	it = at_complex_tree(it, 2);
	CAG_TEST(*tests, it->value.real == 2.0,
		 "cag_tree: at 2 index");
	it = at_complex_tree(it, 7);
	CAG_TEST(*tests, it->value.real == 9.0,
		 "cag_tree: at 7 index");
	it = at_complex_tree(it, 1);
	CAG_TEST(*tests, it == end_complex_tree(&t),
		 "cag_tree: at end");
	free_complex_tree(&t);
}
Example #19
0
    void populate_tree(rapidxml::xml_node<char> *cur_node, xml_node &node)
    {
        switch (cur_node->type())
        {
        case rapidxml::node_element:
        {
            xml_node &new_node = node.add_child((char *)cur_node->name(), 0, false);
            // Copy attributes
            for (rapidxml::xml_attribute<char> *attr = cur_node->first_attribute();
                    attr; attr = attr->next_attribute())
            {
                new_node.add_attribute(attr->name(), attr->value());
            }

            // Copy children
            for (rapidxml::xml_node<char> *child = cur_node->first_node();
                    child; child = child->next_sibling())
            {
                populate_tree(child, new_node);
            }
        }
        break;

        // Data nodes
        case rapidxml::node_data:
        case rapidxml::node_cdata:
        {
            std::string trimmed(cur_node->value());
            mapnik::util::trim(trimmed);
            if (trimmed.empty()) break; //Don't add empty text nodes
            node.add_child(trimmed, 0, true);
        }
        break;
        default:
            break;
        }
    }
Example #20
0
    void load_array(T && array, xml_node & node)
    {
        try
        {
            // Parse using appropriate flags
            // https://github.com/mapnik/mapnik/issues/1856
            // const int f_tws = rapidxml::parse_normalize_whitespace;
            const int f_tws = rapidxml::parse_trim_whitespace | rapidxml::parse_validate_closing_tags;
            rapidxml::xml_document<> doc;
            doc.parse<f_tws>(&array.front());

            for (rapidxml::xml_node<char> *child = doc.first_node();
                 child; child = child->next_sibling())
            {
                populate_tree(child, node);
            }
        }
        catch (rapidxml::parse_error const& e)
        {
            long line = static_cast<long>(
                std::count(&array.front(), e.where<char>(), '\n') + 1);
            throw config_error(e.what(), line, filename_);
        }
    }
Example #21
0
static bool test_fixed(struct torture_context *tctx,
		       struct smb2_tree *tree)
{
	TALLOC_CTX *mem_ctx = talloc_new(tctx);
	struct smb2_create create;
	struct smb2_handle h, h2;
	struct smb2_find f;
	union smb_search_data *d;
	struct file_elem files[NFILES] = {};
	NTSTATUS status;
	bool ret = true;
	unsigned int count;
	int i;

	status = populate_tree(tctx, mem_ctx, tree, files, NFILES, &h);

	ZERO_STRUCT(create);
	create.in.desired_access = SEC_RIGHTS_DIR_ALL;
	create.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
	create.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
	create.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
				 NTCREATEX_SHARE_ACCESS_WRITE |
				 NTCREATEX_SHARE_ACCESS_DELETE;
	create.in.create_disposition = NTCREATEX_DISP_OPEN;
	create.in.fname = DNAME;

	status = smb2_create(tree, mem_ctx, &create);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
	h2 = create.out.file.handle;

	ZERO_STRUCT(f);
	f.in.file.handle	= h;
	f.in.pattern		= "*";
	f.in.continue_flags	= SMB2_CONTINUE_FLAG_SINGLE;
	f.in.max_response_size	= 0x100;
	f.in.level              = SMB2_FIND_BOTH_DIRECTORY_INFO;

	/* Start enumeration on h, then delete all from h2 */
	status = smb2_find_level(tree, tree, &f, &count, &d);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");

	f.in.file.handle	= h2;

	do {
		status = smb2_find_level(tree, tree, &f, &count, &d);
		if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
			break;
		torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");

		for (i = 0; i < count; i++) {
			const char *found = d[i].both_directory_info.name.s;
			char *path = talloc_asprintf(mem_ctx, "%s\\%s",
			    DNAME, found);

			if (!strcmp(found, ".") || !strcmp(found, ".."))
				continue;

			status = smb2_util_unlink(tree, path);
			torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
							"");

			talloc_free(path);
		}

		f.in.continue_flags = 0;
		f.in.max_response_size	= 4096;
	} while (count != 0);

	/* Now finish h enumeration. */
	f.in.file.handle = h;

	do {
		status = smb2_find_level(tree, tree, &f, &count, &d);
		if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
			break;
		torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");

		for (i = 0; i < count; i++) {
			const char *found = d[i].both_directory_info.name.s;

			if (!strcmp(found, ".") || !strcmp(found, ".."))
				continue;

			torture_result(tctx, TORTURE_FAIL,
			    "(%s): didn't expect %s\n",
			    __location__, found);
			ret = false;
			goto done;
		}

		f.in.continue_flags = 0;
		f.in.max_response_size	= 4096;
	} while (count != 0);

 done:
	smb2_util_close(tree, h);
	smb2_util_close(tree, h2);
	smb2_deltree(tree, DNAME);
	talloc_free(mem_ctx);

	return ret;
}
Example #22
0
int main()
{
    int max_rows, max_cols;
    int fake = 7;

	WINDOW* statusbar = nullptr;
	WINDOW* treeview = nullptr;
	WINDOW* commandline = nullptr;

	pugi::xml_document doc;
	pugi::xml_parse_result result;
	struct Group* root;
    struct Group* current_selection;

	std::string command = "";

	char server_addr[IP_ADDRLEN] = "127.0.0.1";
	char buffer[8192];
	unsigned short port = 26750;
	struct sockaddr_in server;
	struct in_addr addr;
	socklen_t server_size = sizeof server;
	int sock;
	int packlen;

	initscr();
    getmaxyx(stdscr, max_rows, max_cols);

	if (has_colors() == FALSE)
	{
		endwin();
		std::cerr << "\nncurses: colors not supported.\n";
		return 1;
	}

	start_color();
	init_pair(1, COLOR_WHITE, COLOR_BLACK);
	init_pair(2, COLOR_RED, COLOR_BLACK);

	attron(COLOR_PAIR(1));
	//attron(A_BOLD);

	cbreak(); // we don't need to push [enter]
	noecho(); // pressed keys are not displayed
	keypad(stdscr, TRUE);
    keypad(commandline, TRUE);

	move(7,1);
	printw("Sending request to %s:%i...", server_addr, port);

	std::string packet_1 = "1oeila";
	const unsigned packet_1_len = packet_1.length();

	if (inet_pton(AF_INET, server_addr, &addr) <= 0) // returns 1 on success
    {
        printw("Invalid IP address (inet_pton).");
        refresh();
        getch();
        endwin();
        return 1;
    }

	memset(&server, 0, sizeof server);
	server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr = addr;

	sock = socket(AF_INET, SOCK_DGRAM, 0);

	packlen = sendto(sock, packet_1.c_str(), packet_1_len, 0, (struct sockaddr*)&server, sizeof(server));
	if (packlen <= 0)
	{
		printw("error");
		refresh();
		getch();
		endwin();
		return 1;
	}
	printw("done");
	move(8,1);
	printw("Waiting for response...");
	refresh();

	packlen = recvfrom(sock, buffer, sizeof buffer, 0, (struct sockaddr*)&server, &server_size);
	if (packlen <= 0)
	{
		printw("error");
		refresh();
		getch();
		endwin();
		return 1;
	}
	printw("done");

	move(9,1);
    printw("Loading tree...");
	result = doc.load_buffer(buffer, sizeof buffer);

	switch (result.status)
    {
        case pugi::status_ok:
        {
        	printw("done");
        	move(10,1);
        	printw("Rebuilding tree...");
        	root = populate_tree(doc.first_child().first_child());  // -> TreeStructure -> Root 
        	if (root == nullptr)
        	{
        		refresh();
        		getch();
        		endwin();
        		return 1;
        	}
        	else
            {
        		printw("done");
                getch();
            }
            break;
        }
        case pugi::status_io_error:
        case pugi::status_file_not_found:
        {
            printw("IO error.");
            refresh();
            getchar();
            endwin();
            return 1;
        }
        case pugi::status_out_of_memory:
        {
            printw("out of memory.");
            refresh();
            getchar();
            endwin();
            return 1;
        }
        case pugi::status_internal_error:
        {
            printw("internal error while loading.");
            refresh();
            getchar();
            endwin();
            return 1;
        }
        default:
        {
            printw("error while parsing.");
            refresh();
            getchar();
            endwin();
            return 1;
        }
    }

    drawWindows(&statusbar, &treeview, &commandline);
    drawTree(&treeview, root, 2, 3, 1);
    mvwprintw(statusbar, 1, max_cols-18, "Total groups: %i", root->total_subgroups+1);
    wrefresh(statusbar);

    wmove(commandline, 1, 3);

    current_selection = root;

    while (1)
    {
    	int keypress = getch();

        if (keypress == KEY_LEFT) // return to father node
        {
            if (current_selection->father != nullptr) // if not root
            {
                current_selection = current_selection->father;
                drawTree(&treeview, root, 2, 3, current_selection->id);
            }
        }
        else if (keypress == KEY_RIGHT) // enter the first child
        {
            if (current_selection->total_subgroups) // if has children
            {
                current_selection = current_selection->children[0];
                drawTree(&treeview, root, 2, 3, current_selection->id);
            }
        }
        else if (keypress == KEY_UP) // navigate through siblings
        {
            if (current_selection->id > 1) // if not root
            {
                int sibling_index = getIndexById(current_selection->father, current_selection->id) - 1;

                if (sibling_index >= 0) // check bounds
                {
                    current_selection = current_selection->father->children[sibling_index];
                    drawTree(&treeview, root, 2, 3, current_selection->id);
                }
            }
        }
        else if (keypress == KEY_DOWN) // navigate through siblings
        {
            if (current_selection->id > 1) // if not root
            {
                int sibling_index = getIndexById(current_selection->father, current_selection->id) + 1;

                if (sibling_index < current_selection->father->children.size()) // check bounds
                {
                    current_selection = current_selection->father->children[sibling_index];
                    drawTree(&treeview, root, 2, 3, current_selection->id);
                }
            }
        }
    	else if ((keypress >= 'A' && keypress <= 'Z') || (keypress >= 'a' && keypress <= 'z') || (keypress >= '0' && keypress <= '9') || keypress == ' ') // command
    	{
    		command += (char)keypress;
    		waddch(commandline, keypress);
    		wrefresh(commandline);
    	}
    	else if (keypress == 127 || keypress == 8) // backspace
    	{
    		int slen = command.length(), c_y, c_x;
    		if (slen > 0)
    		{
    			command.erase(command.end()-1);
    			getyx(commandline, c_y, c_x);
    			mvwaddch(commandline, c_y, c_x-1, ' ');
                wmove(commandline, c_y, c_x-1);
                wrefresh(commandline);
    		}
        }
    	else if (keypress == 10) // enter
    	{
            if (command == "")
            {
                endwin();
                delete root;
                return 1;
            }

            std::string buf;
            std::vector<std::string> tokens;
            std::stringstream ss(command);

            // split the command into individual words (delim == ' ')

            while (ss >> buf)
            {
                tokens.push_back(buf);
            }

            /*if (tokens[0] == "datetime")
            {
                time_t current_time;
                struct tm* time_info;
                char timeString[9];

                time(&current_time);
                time_info = localtime(&current_time);
                strftime(timeString, sizeof(timeString), "%H:%M:%S", time_info);

                mvwprintw(treeview, fake++, 50, ">> server replies: %s", timeString);
                wrefresh(treeview);
            }
            else
            {
                endwin();
                delete root;
                return 1;
            }*/
    	}
        else if (keypress == KEY_LEFT)
        {
            endwin();
            delete root;
            return 1;
        }
    }
Example #23
0
static bool test_find(struct torture_context *tctx,
		      struct smb2_tree *tree)
{
	TALLOC_CTX *mem_ctx = talloc_new(tctx);
	struct smb2_handle h;
	struct smb2_find f;
	union smb_search_data *d;
	struct file_elem files[NFILES] = {};
	NTSTATUS status;
	bool ret = true;
	unsigned int count;
	int i, j, file_count = 0;

	status = populate_tree(tctx, mem_ctx, tree, files, NFILES, &h);

	ZERO_STRUCT(f);
	f.in.file.handle	= h;
	f.in.pattern		= "*";
	f.in.continue_flags	= SMB2_CONTINUE_FLAG_SINGLE;
	f.in.max_response_size	= 0x100;
	f.in.level              = SMB2_FIND_BOTH_DIRECTORY_INFO;

	do {
		status = smb2_find_level(tree, tree, &f, &count, &d);
		if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
			break;
		torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");

		for (i = 0; i < count; i++) {
			bool expected;
			const char *found = d[i].both_directory_info.name.s;

			if (!strcmp(found, ".") || !strcmp(found, ".."))
				continue;

			expected = false;
			for (j = 0; j < NFILES; j++) {
				if (!strcmp(files[j].name, found)) {
					files[j].found = true;
					expected = true;
					break;
				}
			}

			if (expected)
				continue;

			torture_result(tctx, TORTURE_FAIL,
			    "(%s): didn't expect %s\n",
			    __location__, found);
			ret = false;
			goto done;
		}

		file_count = file_count + i;
		f.in.continue_flags = 0;
		f.in.max_response_size	= 4096;
	} while (count != 0);

	torture_assert_int_equal_goto(tctx, file_count, NFILES + 2, ret, done,
				      "");

	for (i = 0; i < NFILES; i++) {
		if (files[j].found)
			continue;

		torture_result(tctx, TORTURE_FAIL,
		    "(%s): expected to find %s, but didn't\n",
		    __location__, files[j].name);
		ret = false;
		goto done;
	}

 done:
	smb2_deltree(tree, DNAME);
	talloc_free(mem_ctx);

	return ret;
}