示例#1
0
文件: gnode.c 项目: UIKit0/atv2
/**
 * g_node_copy_deep:
 * @node: a #GNode
 * @copy_func: the function which is called to copy the data inside each node,
 *   or %NULL to use the original data.
 * @data: data to pass to @copy_func
 * 
 * Recursively copies a #GNode and its data.
 * 
 * Return value: a new #GNode containing copies of the data in @node.
 *
 * Since: 2.4
 **/
GNode*
g_node_copy_deep (GNode     *node, 
		  GCopyFunc  copy_func,
		  gpointer   data)
{
  GNode *new_node = NULL;

  if (copy_func == NULL)
	return g_node_copy (node);

  if (node)
    {
      GNode *child, *new_child;
      
      new_node = g_node_new (copy_func (node->data, data));
      
      for (child = g_node_last_child (node); child; child = child->prev) 
	{
	  new_child = g_node_copy_deep (child, copy_func, data);
	  g_node_prepend (new_node, new_child);
	}
    }
  
  return new_node;
}
static gboolean cloud_config_simplify(GNode *node, __unused__ gpointer data) {
	if (node->data) {
		return false;
	}

	GNode *child = g_node_last_child(node);
	while (child) {
		if (child->data) {
			child = g_node_prev_sibling(child);
			continue;
		}
		GNode *remove = child;
		child = g_node_prev_sibling(child);
		g_node_append(node->parent, g_node_copy(remove));
		g_node_unlink(remove);
		g_node_destroy(remove);
	}

	if (g_node_n_children(node) == 0) {
		g_node_unlink(node);
		g_node_destroy(node);
	}

	return false;
}
static void test_getChildInDirectory_FindFromFileAndDir_FileExists() {
    before();
    GNode *tree = single_folder(TRUE, TRUE);

// THIS IS THE STRUCTURE:
//
// test-dir (7 children)
// ├─ .apa.png
// ├─ .depa.gif
// ├─ bepa.png
// ├─ cepa.jpg
// ├─ epa.png
// ├─┬dir_one (3 children)
// │ ├─ .three.png
// │ ├─ two.jpg
// │ └─┬.secrets (1 children)
// │   └─ img.jpg
// └─┬dir_two (7 children)
//   ├─ apa.png
//   ├─ bepa.png
//   ├─ cepa.png
//   ├─┬sub_dir_four (2 children)
//   │ ├──subsub (0 children)
//   │ └──subsub2 (0 children)
//   ├─┬sub_dir_one (3 children)
//   │ ├─ img0.png
//   │ ├─ img1.png
//   │ └─ img2.png
//   ├──sub_dir_three (0 children)
//   └─┬sub_dir_two (3 children)
//     ├─ img0.png
//     ├─ img1.png
//     ├─ img2.png
//     └─ img3.png

    char *path0 = get_absolute_path(testdir_path, "/bepa.png");
    char *path1 = get_absolute_path(testdir_path, "/dir_two");
    char *path2 = get_absolute_path(testdir_path, "/dir_two/bepa.png");

    GNode *child0 = get_child_in_directory(tree, path0);
    GNode *child1 = g_node_last_child(get_root_node(tree));
    assert_child_is_equal("Get child in directory ─ Find bepa.png", child0, path0);
    assert_child_is_equal("Get child in directory ─ Last child is dir_two", child1, path1);

    GNode *child2 = get_child_in_directory(child0, path2);
    GNode *child3 = get_child_in_directory(child1, path2);

    assert_child_is_equal("Get child in directory ─ From file ─ File exists", child2, path2);
    assert_child_is_equal("Get child in directory ─ From subdir ─ File exists", child3, path2);

    free(path0);
    free(path1);
    free(path2);
    free_whole_tree(tree);
    after();
}
static void test_getChildInDirectory_DirectoryIsEmpty() {
    before();
    GNode *tree = single_folder(TRUE, TRUE);

// THIS IS THE STRUCTURE:
//
// test-dir (7 children)
// ├─ .apa.png
// ├─ .depa.gif
// ├─ bepa.png
// ├─ cepa.jpg
// ├─ epa.png
// ├─┬dir_one (3 children)
// │ ├─ .three.png
// │ ├─ two.jpg
// │ └─┬.secrets (1 children)
// │   └─ img.jpg
// └─┬dir_two (7 children)
//   ├─ apa.png
//   ├─ bepa.png
//   ├─ cepa.png
//   ├─┬sub_dir_four (2 children)
//   │ ├──subsub (0 children)
//   │ └──subsub2 (0 children)
//   ├─┬sub_dir_one (3 children)
//   │ ├─ img0.png
//   │ ├─ img1.png
//   │ └─ img2.png
//   ├──sub_dir_three (0 children)
//   └─┬sub_dir_two (3 children)
//     ├─ img0.png
//     ├─ img1.png
//     ├─ img2.png
//     └─ img3.png

    char *path0 = get_absolute_path(testdir_path, "/dir_two/sub_dir_four/subsub");
    char *path1 = get_absolute_path(testdir_path, "/dir_two/sub_dir_four/subsub/apa.jpg");

    GNode *child = get_root_node(tree);
    child = g_node_last_child(child);
    child = g_node_first_child(child);
    child = g_node_next_sibling(child);
    child = g_node_next_sibling(child);
    child = g_node_next_sibling(child);
    child = g_node_first_child(child);
    assert_child_is_equal("Get child in directory ─ Last child is subsub", child, path0);

    child = get_child_in_directory(child, path1);

    assert_tree_is_null("Get child in directory ─ Directory is empty", child);

    free(path0);
    free(path1);
    free_whole_tree(tree);
    after();
}
示例#5
0
文件: gnode.c 项目: UIKit0/atv2
/**
 * g_node_copy:
 * @node: a #GNode
 *
 * Recursively copies a #GNode (but does not deep-copy the data inside the 
 * nodes, see g_node_copy_deep() if you need that).
 *
 * Returns: a new #GNode containing the same data pointers
 */
GNode*
g_node_copy (GNode *node)
{
  GNode *new_node = NULL;
  
  if (node)
    {
      GNode *child;
      
      new_node = g_node_new (node->data);
      
      for (child = g_node_last_child (node); child; child = child->prev)
	g_node_prepend (new_node, g_node_copy (child));
    }
  
  return new_node;
}
示例#6
0
void load_file(GtkFileSelection *selector, gpointer file_selector) {
	FILE *stream;
	gchar *pfad;
	gchar *filename;
	gchar txtBuffer[200];
	gchar bouquetName[MAX_TXT_LEN+1];
	bouquetEntry *bouquet;
	channelEntry *channel;
	GNode *node_root;
	GNode *node_bouquet;
	GNode *node_channel;
	gint sumBouquets = 0;
	gint diseqc, transportID, frequenz, symbolRate, fec, polarity, onid, serviceID, serviceType;
	gchar name[MAX_TXT_LEN+1];

	//******************************
	// load bouquets file & fill GNode.
	//******************************
	filename = (gchar*) gtk_file_selection_get_filename(GTK_FILE_SELECTION(file_selector));
	if (!(stream = fopen(filename, "rb"))){
		GtkWidget* dialog;
		sprintf(txtBuffer,_("Could not read: '%s' \n"),filename);
		dialog = gtk_message_dialog_new (NULL,0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,txtIn(txtBuffer));
		center_window(GTK_WINDOW(dialog));
		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);
		return;
	}
	fgets(txtBuffer,BUFFER_SIZE,stream); // xml Version.
	fgets(txtBuffer,BUFFER_SIZE,stream); // ZAPIT - ID.
	if (!strFind(txtBuffer,"<ZAPIT>" )){
		GtkWidget* dialog= gtk_message_dialog_new (NULL,0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
			txtIn(_("channel format unknown")));
		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);
		fclose(stream);
		return;
	}

	GTK_LIST_STORE(MW_GET("LEFT_LIST_STORE"))->sort_column_id = -2; 	// switch off sorting.
	clear_left_listview();
	fgets(txtBuffer,BUFFER_SIZE,stream);	// Bouquet-Kennung.
	if (!strFind(txtBuffer,"<BOUQUET" )){
		GtkWidget* dialog= gtk_message_dialog_new (NULL,0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
			txtIn(_("This is not a Bouquet File.\n"
							"Please select a bouquet-file like 'bouquets.xml'.")));
		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);
		fclose(stream);
		return;
	}   
	// ***** OK, this seems to be a bouquets file.
	fseek(stream,0,0); 
	fgets(txtBuffer,BUFFER_SIZE,stream); 	// xml Version.
	fgets(txtBuffer,BUFFER_SIZE,stream);	// ZAPIT - ID.

	node_root = MW_GET("LEFT_NODE_ROOT");
	while(1){ // read all Bouquets.
		fgets(txtBuffer,BUFFER_SIZE,stream);	// Bouquet-Daten.
		if (strFind(txtBuffer,"</ZAPIT>" )) break;
		bouquet = malloc(sizeof(bouquetEntry));
		sumBouquets++;
		node_bouquet = g_node_append(node_root, g_node_new(bouquet));
		XMLtoAsc(bouquetName, extractData(txtBuffer,"name"));
		strcpy(bouquet->bouquetName,bouquetName);
		bouquet->hidden = atoi(extractData(txtBuffer,"hidden"));
		bouquet->locked = atoi(extractData(txtBuffer,"locked"));
		node_channel = g_node_last_child (node_bouquet);
		while(1){ // read all channels.
			fgets(txtBuffer,BUFFER_SIZE,stream);
			if (strFind(txtBuffer,"</BOUQUET>" )) break;
			channel = malloc(sizeof(channelEntry));
			node_channel = g_node_append(node_bouquet, g_node_new(channel));
			channel->serviceID= strtol(extractData(txtBuffer,"serviceID"),NULL,16);
			XMLtoAsc(channel->channelName, extractData(txtBuffer,"name"));
			channel->onid= strtol(extractData(txtBuffer,"onid"),NULL, 16);
			channel->frequency = 0;
		}
	}
	fclose(stream);
	// ******************************
	// die services Datei einlesen und die Bouquets in verkette Liste mit diesen
	// Daten ergänzen.
	// ******************************
	pfad=filename+strlen(filename);
	while(*pfad!='\\' && *pfad!='/') pfad--;
	*++pfad='\0';
	strcpy(txtBuffer, filename);
	strcat(txtBuffer,ZAPIT_SERV_NAME);
	if (!(stream = fopen(txtBuffer, "rb"))){
		GtkWidget* dialog;
		strcat(txtBuffer, _(" not found."));
		dialog = gtk_message_dialog_new (NULL,0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,txtIn(txtBuffer));
		center_window(GTK_WINDOW(dialog));
		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);
		clear_left_listview();
		return;
	}
	fgets(txtBuffer,BUFFER_SIZE,stream); 	// xml Version.
	fgets(txtBuffer,BUFFER_SIZE,stream);	// ZAPIT-Kennung.

	while (1){ // alle Satelliten einlesen.
		fgets(txtBuffer,BUFFER_SIZE,stream);	// Sat / Kabel Daten.
		if (!strFind(txtBuffer,"<sat" ) && !strFind(txtBuffer,"<cable" )) break;	
		diseqc = atoi(extractData(txtBuffer,"diseqc"));
		while (1){ // alle Transponder einlesen.
			fgets(txtBuffer,BUFFER_SIZE,stream);	// Transponder
			if (strFind(txtBuffer,"</" )) break;
			transportID= strtol(extractData(txtBuffer,"transponder id"),NULL, 16);
			onid = strtol(extractData(txtBuffer,"onid"),NULL,16);
			frequenz= atoi(extractData(txtBuffer,"frequency"));
			symbolRate= atoi(extractData(txtBuffer,"symbol_rate"));
			fec= atoi(extractData(txtBuffer,"fec_inner"));
			polarity= atoi(extractData(txtBuffer,"polarization"));
			while(1){ // Alle Channels einlesen.
				gint test=0;
				fgets(txtBuffer,BUFFER_SIZE,stream);	// Kanaldaten.
				if (strFind(txtBuffer,"</" )) break;
				serviceID = strtol(extractData(txtBuffer,"service_id"),NULL,16);
				XMLtoAsc(name, extractData(txtBuffer,"name"));
				serviceType = strtol(extractData(txtBuffer,"service_type"),NULL,16);
				// ******************************
				// jeden einzelnen Sender in der Liste mit den neuen Daten ergänzen.
				// ******************************
				node_bouquet = g_node_first_child(node_root);
				while (node_bouquet){
					node_channel = g_node_first_child(node_bouquet);
					while (node_channel){
						channel = node_channel->data;
						if ((serviceID == channel->serviceID) && (onid == channel->onid))
						{ // dieser Sender ist in den Bouquets. Also fehlende Daten ergänzen.
							channel->serviceType=serviceType;
							channel->diseqc=diseqc;
							channel->transportID=transportID;
							channel->frequency=frequenz;
							channel->symbolRate=symbolRate;
							channel->fec=fec;
							channel->polarisation=polarity;
							test++;
						}
						node_channel = node_channel->next;
					}
					node_bouquet=node_bouquet->next;
				}
				// ******************************
				// Wenn der Sender aus den Services nicht in den Bouquets vorhanden war und die Liste
				// das komplette Bouquet enthält-> im Bouquet "*NEW*" eintragen.
				// ******************************
				if (!test && sumBouquets > 1){
					node_bouquet = g_node_last_child(node_root);
					bouquet = node_bouquet->data;
					if (strcmp(bouquet->bouquetName, "*NEW*")){
						bouquet = malloc(sizeof(bouquetEntry));
						node_bouquet = g_node_append(node_root, g_node_new(bouquet));
						strcpy(bouquet->bouquetName,"*NEW*");
						bouquet->hidden = FALSE;
						bouquet->locked = FALSE;
						sumBouquets++;
					}
					channel = malloc(sizeof(channelEntry));
					g_node_append(node_bouquet, g_node_new(channel));
					channel->serviceType=serviceType;
					channel->diseqc=diseqc;
					channel->transportID=transportID;
					channel->frequency=frequenz;
					channel->symbolRate=symbolRate;
					channel->fec=fec;
					channel->polarisation=polarity;
					XMLtoAsc(channel->channelName,name);
					channel->onid= onid;
					channel->serviceID= serviceID;
				}
			}
		}
	}
	fclose(stream);
	//******************************
	// Die Bouquets überprüfen. Wenn kein Eintrag bei (z.B.) Frequez vorhanden ist,
	// war der Sender nicht in der services enthalten -> Daten sind nicht komplett!
	// -> löschen. Ebenso wenn Datendienste nicht eingelsen werden sollten.
	//******************************
	node_bouquet = g_node_first_child(node_root);
	while (node_bouquet){
		node_channel = g_node_first_child (node_bouquet);
		while (node_channel){
			channel = node_channel->data;
			if ( (!channel->frequency) || ((!GTK_TOGGLE_BUTTON(MW_GET("OPT_READ_DATA"))->active)
				&& (getServicePic(channel->serviceType) == DATA)) )
			{ // Sender war in der Bouquets-datei, aber nicht in der Services -> Sender löschen.
				node_channel = remove_node(node_channel);
				continue;
			}
			node_channel = node_channel ->next;
		}
		if (!g_node_first_child (node_bouquet)){ // bouquet now empty ? -> delete it.
			node_bouquet = remove_node(node_bouquet);
			sumBouquets--;
			continue;
		}
		node_bouquet = node_bouquet->next;
	}
	gtk_widget_grab_focus(GTK_WIDGET(MW_GET("OPT_READ_SHORT"))); // unfocus search entry.
	fill_left_listview();
}
示例#7
0
static gboolean store_into_g_ptr_array (GNode* node, gpointer g_ptr_array) {
	TreeElement* temp = (TreeElement*) node -> data;
	STOCKINFO* temp_stock = (STOCKINFO*) temp -> userdata; /* for debugging */
	if ((temp -> state_info & BASIS_ACTIVATED) == IS_ACTIVATED) {
		/* node is the root */
		if (g_node_depth (node) == 1) {
			/* initialize */
			temp -> parent = NULL;
			temp -> lastchild = g_node_last_child (node); /* lastchild */

			memset (temp -> base_format, 0x0, sizeof (temp -> base_format));
			memset (temp -> format, 0x0, sizeof (temp -> format));

			/* base_format */
			/* nothing to do */
			/* format */
			char sign;
			if (!G_NODE_IS_LEAF (node)) {
				if ((temp -> state_info & BASIS_OPENED) == IS_OPENED) sign = '-';

				else sign = '+';

				strncpy (temp -> format, &sign ,1);
			}

		}
		/* node is not root */
		else {
			/* initialize */
			memset (temp -> base_format, 0x0, sizeof (temp -> base_format));
			memset (temp -> format, 0x0, sizeof (temp -> format));

			temp -> lastchild = g_node_last_child (node); /* lastchild */

			/* extend the parent's base_format */
			GNode* parent = temp -> parent;
			TreeElement* temp_parent = (TreeElement*) parent -> data;
			int length = strlen (temp_parent -> base_format);
			strncpy (temp -> base_format, temp_parent -> base_format, length);

			/* base_format */
			char base_format_link [3];
			char* temp_base_format;
			TreeElement* temp_grand_parent = NULL;
			if (temp_parent -> parent) {
				GNode* grand_parent = temp_parent -> parent;
				temp_grand_parent = grand_parent -> data;
				if (temp_grand_parent -> lastchild == temp -> parent)
					temp_base_format = "\t\0";
				else temp_base_format = "|\t";
			}
			else {
				temp_base_format = "\t\0";
			}

			strncpy (base_format_link, temp_base_format, 3);			
			length = strlen (temp -> base_format);
			strncpy (temp -> base_format + length, base_format_link, 3);

			/* format */
			length = strlen (temp -> base_format);
			strncpy (temp -> format, temp -> base_format, length);

			/* ARM */
			char* arm;
			if (node == temp_parent -> lastchild) {
				arm = "*-";
			}
			else {
				arm = "|-";
			}
			length = strlen (temp -> format);
			strncpy (temp -> format + length, arm, 3);

			/* SIGN */
			char sign;
			if (!G_NODE_IS_LEAF (node)) {
				if ((temp -> state_info & BASIS_OPENED) == IS_OPENED) sign = '-';

				else sign = '+';
				length = strlen (temp -> format);
				strncpy (temp -> format + length, &sign ,1);
			}
			else {
				temp -> state_info += IS_LEAF;
			}
		}	

		g_ptr_array_add (g_ptr_array, temp);
	}
	return false;
}