Esempio n. 1
0
/**
 * gts_bb_tree_draw:
 * @tree: a bounding box tree.
 * @depth: a specified depth.
 * @fptr: a file pointer.
 *
 * Write in @fptr an OOGL (Geomview) description of @tree for the
 * depth specified by @depth.
 */
void gts_bb_tree_draw (GNode * tree, guint depth, FILE * fptr)
{
  guint d;

  g_return_if_fail (tree != NULL);
  g_return_if_fail (fptr != NULL);

  d = g_node_depth (tree);

  if (d == 1)
    fprintf (fptr, "{ LIST");

  if (d == depth)
    gts_bbox_draw (tree->data, fptr);
  else if (d < depth) {
    GNode * i = tree->children;
    while (i) {
      gts_bb_tree_draw (i, depth, fptr);
      i = i->next;
    }
  }

  if (d == 1)
    fprintf (fptr, "}\n");
}
Esempio n. 2
0
/* Debug */
gboolean
spew_func(GNode* node, gpointer data)
{
  LTableEntry* lte = node->data;  
  GList* tmp;
  gchar spaces[256];
  memset(spaces, ' ', 256);
  spaces[g_node_depth(node)] = '\0';

  
  g_print (" %sSpewing node `%s' (%p): ", spaces, lte->name, node);

  tmp = lte->listeners;
  while (tmp != NULL)
    {
      Listener* l = tmp->data;
      
      g_print ("  %slistener %u is here\n", spaces, (guint)l->cnxn);

      tmp = g_list_next(tmp);
    }

  if (lte->listeners == NULL)
    g_print ("\n");
  
  return FALSE;
}
Esempio n. 3
0
void
i7_node_layout(I7Node *self, GooCanvasItemModel *skein, GooCanvas *canvas, gdouble x)
{
	I7_NODE_USE_PRIVATE;

	gdouble hspacing, vspacing;
	g_object_get(skein,
		"horizontal-spacing", &hspacing,
		"vertical-spacing", &vspacing,
		NULL);

	if(g_node_n_children(self->gnode) == 1)
		i7_node_layout(self->gnode->children->data, skein, canvas, x);
	else {
		/* Find the total width of all descendant nodes */
		gdouble total = i7_node_get_tree_width(self, skein, canvas);
		/* Lay out each child node */
		GNode *child;
		gdouble child_x = 0.0;

		for(child = self->gnode->children; child; child = child->next) {
			gdouble treewidth = i7_node_get_tree_width(child->data, skein, canvas);
			i7_node_layout(child->data, skein, canvas, x - total * 0.5 + child_x + treewidth * 0.5);
			child_x += treewidth + hspacing;
		}
	}

	/* Move the node's group to its proper place */
	gdouble y = (gdouble)(g_node_depth(self->gnode) - 1.0) * vspacing;
	g_object_set(self, "x", x, "y", y, NULL);
	
	/* Cache the x coordinate */
	priv->x = x;
}
Esempio n. 4
0
static void check_and_store (GNode* node, gpointer data) {
	TreeElement* temp = (TreeElement*) node -> data;
	STOCKINFO* stock_data = (STOCKINFO*) temp -> userdata;

	/* 얕은 복사 */
	regex_t_and_node* temp_data = (regex_t_and_node*) malloc (sizeof (regex_t_and_node)); 
	*temp_data = *(regex_t_and_node*) data;

	int status = regexec (&(temp_data -> state), stock_data -> symbol, 0, NULL, 0);
	if (status == 0 || g_node_depth (node) == 2) {
		/* 얕은 복사 */
		/*
		TreeElement* copy_data = (TreeElement*) malloc (sizeof (TreeElement));
		*copy_data = *temp;
		*/
		GNode* copy_node = new_tree_node (stock_data, IS_OPENED | IS_ACTIVATED, temp_data -> array);
		g_node_insert (temp_data -> array, -1, copy_node);
		
		temp_data -> array = copy_node;
		if (!G_NODE_IS_LEAF (node)) {
			g_node_children_foreach (node, G_TRAVERSE_ALL, check_and_store, (gpointer) temp_data);
		} /* Recursive call */
	}
	
	free (temp_data);
	return;
}
Esempio n. 5
0
gboolean state_compute_variance_cb (GNode *nd, gpointer data)
{
    double *variance = (double*)data;
    dijk_node_t *n = (dijk_node_t*)nd->data;

    *variance += n->pdf1 * powf (1.0 * g_node_depth (nd), 2);
    return FALSE;
}
Esempio n. 6
0
static GNode *
xmms_magic_add_node (GNode *tree, const gchar *s, GNode *prev_node)
{
	xmms_magic_entry_t *entry;
	gpointer *data = tree->data;
	guint indent = 0, prev_indent;

	g_assert (s);

	XMMS_DBG ("adding magic spec to tree '%s'", (gchar *) data[0]);

	/* indent level is number of leading '>' characters */
	while (*s == '>') {
		indent++;
		s++;
	}

	entry = parse_entry (s);
	if (!entry) {
		XMMS_DBG ("cannot parse magic entry");
		return NULL;
	}

	if (!indent) {
		return g_node_append_data (tree, entry);
	}

	if (!prev_node) {
		XMMS_DBG ("invalid indent level");
		xmms_magic_entry_free (entry);
		return NULL;
	}

	prev_indent = g_node_depth (prev_node) - 2;

	if (indent > prev_indent) {
		/* larger jumps are invalid */
		if (indent != prev_indent + 1) {
			XMMS_DBG ("invalid indent level");
			xmms_magic_entry_free (entry);
			return NULL;
		}

		return g_node_append_data (prev_node, entry);
	} else {
		while (indent < prev_indent) {
			prev_indent--;
			prev_node = prev_node->parent;
		}

		return g_node_insert_after (prev_node->parent, prev_node,
		                            g_node_new (entry));
	}
}
Esempio n. 7
0
gboolean state_transition_convolve_cb (GNode *node, gpointer data)
{
    int depth = g_node_depth (node);

    dijk_node_t *nd = (dijk_node_t*)node->data;

    double *val = (double*)data;

    *val += nd->pdf1 * exp (-powf(depth,2)/(2.0*g_state_sigma*g_state_sigma));

    return FALSE;
}
Esempio n. 8
0
File: ui.c Progetto: Harvie/Programs
void print_parents_lines(GList *line)
{
	GNode *file_ptr;
	int depth;

	for (file_ptr = NODE(line)->parent, depth = g_node_depth(file_ptr) - 1;
			!G_NODE_IS_ROOT(file_ptr); file_ptr = file_ptr->parent, depth--) {
		if (file_ptr != g_node_last_sibling(file_ptr))
			mvwaddch(tree_window, g_list_position(first_line, line),
					2 * depth - 2, ACS_VLINE);
		else
			mvwaddch(tree_window, g_list_position(first_line, line),
					2 * depth - 2, ' ');
		mvwaddch(tree_window, g_list_position(first_line, line), 2 * depth - 1, ' ');
	}
}
Esempio n. 9
0
static int xml_write_tree_recursive(GNode *node, FILE *fp)
{
	gint i, depth;
	XMLTag *tag;
	GList *cur;

	cm_return_val_if_fail(node != NULL, -1);
	cm_return_val_if_fail(fp != NULL, -1);

	depth = g_node_depth(node) - 1;
	for (i = 0; i < depth; i++)
		TRY(claws_fputs("    ", fp) != EOF);

	tag = ((XMLNode *) node->data)->tag;

	TRY(fprintf(fp, "<%s", tag->tag) > 0);

	for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
		XMLAttr *attr = (XMLAttr *) cur->data;

		TRY(fprintf(fp, " %s=\"", attr->name) > 0);
		TRY(xml_file_put_escape_str(fp, attr->value) == 0);
		TRY(claws_fputs("\"", fp) != EOF);
		
	}

	if (node->children) {
		GNode *child;
		TRY(claws_fputs(">\n", fp) != EOF);

		child = node->children;
		while (child) {
			GNode *cur;

			cur = child;
			child = cur->next;
			TRY(xml_write_tree_recursive(cur, fp) == 0);
		}

		for (i = 0; i < depth; i++)
			TRY(claws_fputs("    ", fp) != EOF);
		TRY(fprintf(fp, "</%s>\n", tag->tag) > 0);
	} else
		TRY(claws_fputs(" />\n", fp) != EOF);
	
	return 0;
}
Esempio n. 10
0
File: ui.c Progetto: Harvie/Programs
void print_line(GList *line)
{
	int line_number = g_list_position(first_line, line);
	GNode *file = NODE(line);
	int depth = g_node_depth(file) - 1;
	char *link_str;

	wmove(tree_window, line_number, 0);
	wclrtoeol(tree_window);

	if (line == selected_line)
		wattron(tree_window, A_REVERSE);

	if (G_NODE_IS_ROOT(file)) {
		wattron(tree_window, A_BOLD);
		waddnstr(tree_window, FILE(file)->name, getmaxx(tree_window) - 1);
		if (!g_str_has_suffix(FILE(file)->name, "/"))
			waddch(tree_window, '/');
		wattroff(tree_window, A_BOLD);
	} else {
		if (file != g_node_last_sibling(file))
			mvwaddch(tree_window, line_number, 2 * depth - 2, ACS_LTEE);
		else
			mvwaddch(tree_window, line_number, 2 * depth - 2, ACS_LLCORNER);

		waddch(tree_window, ACS_HLINE);
		if (FILE(file)->link == TRUE) {
			link_str = g_strdup_printf("%s -> %s", FILE(file)->name,
					FILE(file)->link_path);
			waddnstr(tree_window, link_str, getmaxx(tree_window) - 2 * depth - 1);
			free(link_str);
		} else
			waddnstr(tree_window, FILE(file)->name,
					getmaxx(tree_window) - 2 * depth - 1);

		if (FILE(file)->type == directory_type && ((FILE(file)->link == TRUE &&
						!g_str_has_suffix(FILE(file)->link_path, "/")) ||
					(FILE(file)->link == FALSE &&
					 !g_str_has_suffix(FILE(file)->name, "/"))))
			waddch(tree_window, '/');
		print_parents_lines(line);
	}
	wattroff(tree_window, A_REVERSE);
}
Esempio n. 11
0
/**
 * as_node_to_xml:
 * @node: a #AsNode.
 * @flags: the AsNodeToXmlFlags, e.g. %AS_NODE_INSERT_FLAG_PRE_ESCAPED.
 *
 * Converts a node and it's children to XML.
 *
 * Returns: (transfer full): a #GString
 *
 * Since: 0.1.0
 **/
GString *
as_node_to_xml (const AsNode *node, AsNodeToXmlFlags flags)
{
	GString *xml;
	const AsNode *l;
	guint depth_offset;

	g_return_val_if_fail (node != NULL, NULL);

	depth_offset = g_node_depth ((GNode *) node) + 1;
	xml = g_string_new ("");
	if ((flags & AS_NODE_TO_XML_FLAG_ADD_HEADER) > 0)
		g_string_append (xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
	if ((flags & AS_NODE_TO_XML_FLAG_INCLUDE_SIBLINGS) > 0) {
		for (l = node; l != NULL; l = l->next)
			as_node_to_xml_string (xml, depth_offset, l, flags);
	} else {
		as_node_to_xml_string (xml, depth_offset, node, flags);
	}
	return xml;
}
Esempio n. 12
0
GNode *cloud_config_find(GNode* node, gchar** path) {
	GNode* child;

	if (!path) {
		if (g_node_depth(node) == 0) {
			return NULL;
		}
		/* reached the end of our search, so return this node as value */
		return g_node_first_child(node);
	}

	child = g_node_first_child(node);
	while (child) {
		if (g_strcmp0(child->data, path[0]) == 0) {
			/* recurse */
			return cloud_config_find(child, (gchar**)path[1]);
		}
		child = g_node_next_sibling(child);
	}

	return NULL;
}
Esempio n. 13
0
/* Adds a new entry to the directory tree */
void
dirtree_entry_new( GNode *dnode )
{
	GtkCTreeNode *parent_ctnode = NULL;
	const char *name;
	boolean expanded;

	g_assert( NODE_IS_DIR(dnode) );

	parent_ctnode = DIR_NODE_DESC(dnode->parent)->ctnode;
	if (strlen( NODE_DESC(dnode)->name ) > 0)
		name = NODE_DESC(dnode)->name;
	else
		name = _("/. (root)");
	expanded = g_node_depth( dnode ) <= 2;

	DIR_NODE_DESC(dnode)->ctnode = gui_ctree_node_add( dir_ctree_w, parent_ctnode, dir_colexp_mini_icons, name, expanded, dnode );

	if (parent_ctnode == NULL) {
		/* First entry was just added. Keep directory tree frozen
		 * most of the time while scanning, otherwise it tends to
		 * flicker annoyingly */
		gtk_clist_freeze( GTK_CLIST(dir_ctree_w) );
	}
	else if (GTK_CTREE_ROW(parent_ctnode)->expanded) {
		/* Pre-update (allow ctree to register new row) */
		gtk_clist_thaw( GTK_CLIST(dir_ctree_w) );
		gui_update( );
		gtk_clist_freeze( GTK_CLIST(dir_ctree_w) );
		/* Select last row */
		gtk_ctree_select( GTK_CTREE(dir_ctree_w), DIR_NODE_DESC(dnode)->ctnode );
		/* Scroll directory tree down to last row */
		gui_clist_moveto_row( dir_ctree_w, -1, 0.0 );
		/* Post-update (allow ctree to perform select/scroll) */
		gtk_clist_thaw( GTK_CLIST(dir_ctree_w) );
		gui_update( );
		gtk_clist_freeze( GTK_CLIST(dir_ctree_w) );
	}
}
Esempio n. 14
0
gboolean
dump_func(GNode *node, gpointer data)
{
  FILE *out;
  if (g_dump_trans) {
    out = out_stream;
  }
  else {
    out = stdout;
  }

  if (node != NULL) {
    guint depth = g_node_depth(node);
    if (depth == 2) {
      fprintf(out,"NEW TRANS\n");
    }

    struct VyattaNode *gp = (struct VyattaNode *) node->data;
    struct Data *gdata = &(gp->_data);
    struct Config *gcfg = &(gp->_config);
    if (gdata->_name != NULL) {
      unsigned int i;
      NODE_OPERATION op = gdata->_operation;
      if (IS_ACTIVE(op)) {
        fprintf(out, "*");
      } else if (IS_DELETE(op)) {
        fprintf(out, "-");
      } else if (IS_CREATE(op)) {
        fprintf(out, "+");
      } else if (IS_SET(op)) {
        fprintf(out, ">");
      } else {
        fprintf(out, " ");
      }

      for (i = 0; i < depth; ++i) {
        fprintf(out,"  ");
      }

      if (gcfg->_def.def_type2 != ERROR_TYPE) {
        fprintf(out,"%s (t: %d-%d, ", gdata->_name, gcfg->_def.def_type,
                gcfg->_def.def_type2);
      } else {
        fprintf(out,"%s (t: %d, ", gdata->_name, gcfg->_def.def_type);
      }
      if (gcfg->_priority_extended) {
        fprintf(out, "p: %s)", gcfg->_priority_extended);
      } else {
        fprintf(out, "p: %d)", gcfg->_priority);
      }

      if (gdata->_value == TRUE) {
        fprintf(out," [VALUE]");
      }
      if (gcfg->_multi == TRUE) {
        fprintf(out," [MULTI(%d)]",gcfg->_limit);
      }
      if (gcfg->_def.actions[syntax_act].vtw_list_head
          && !gcfg->_def.actions[syntax_act].vtw_list_head->vtw_node_aux) {
        fprintf(out," [SYNTAX]");
      }
      if (gcfg->_def.actions[create_act].vtw_list_head) {
        fprintf(out," [CREATE]");
      }
      if (gcfg->_def.actions[activate_act].vtw_list_head) {
        fprintf(out," [ACTIVATE]");
      }
      if (gcfg->_def.actions[update_act].vtw_list_head) {
        fprintf(out," [UPDATE]");
      }
      if (gcfg->_def.actions[delete_act].vtw_list_head) {
        fprintf(out," [DELETE]");
      }
      if (gcfg->_def.actions[syntax_act].vtw_list_head
          && gcfg->_def.actions[syntax_act].vtw_list_head->vtw_node_aux) {
        fprintf(out," [COMMIT]");
      }
      if (gcfg->_def.actions[begin_act].vtw_list_head) {
        fprintf(out," [BEGIN]");
      }
      if (gcfg->_def.actions[end_act].vtw_list_head) {
        fprintf(out," [END]");
      }
      fprintf(out,"\n");
    }
  }
  return FALSE;
}
Esempio n. 15
0
gboolean
sort_func(GNode *node, gpointer data, boolean priority_mode)
{
  struct VyattaNode *gp = (struct VyattaNode *) node->data;
  struct Data *d = &(gp->_data);
  GNode *root_node = (GNode*)data;
  d_dplog("commit2::sort_func(): %s, node count: %d",
          (d->_name ?  d->_name : "[n/a]"), g_node_n_children(root_node));

  //change action state of node according to enclosing behavior
  /* XXX this is ugly. originally the condition for the if is the following:
   *       (c1 && c2 || (c3 || c4) && c5)
   *
   *     this causes compiler warning for mixing && and || without (). the
   *     previous warning removal attempt changed the condition to the
   *     following:
   *       ((c1 && c2) || (c3 || (c4 && c5)))
   *
   *     which was incorrect (c3 and c4 should be at the same "level") and
   *     therefore was reverted.
   *
   *     now changing the condition to the following to avoid compiler
   *     warning:
   *       ((c1 && c2) || ((c3 || c4) && c5))
   *
   *     note that since the current goal is simply cleanup, no attempt is
   *     made to understand the logic here, and the change is purely based
   *     on operator precendence to maintain the original logic.
   *
   * XXX now removing deactivate-handling code, which involves c2.
   *
   *     note that c2 is (d->_disable_op != K_NO_DISABLE_OP), which means
   *     the node is "deactivated" (in working or active config or both).
   *     this in turn means that the (c1 && c2) part of the logic can only
   *     be true if the node is deactivated.
   *
   *     however, since activate/deactivate has not actually been exposed,
   *     this means that in actual usage the (c1 && c2) part is never true.
   *     therefore, we can simply remove the whole part, and the logic
   *     becomes:
   *       ((c3 || c4) && c5)
   */
  NODE_OPERATION op = d->_operation;
  if (((/* c3 */ IS_SET_OR_CREATE(op)) || (/* c4 */ IS_DELETE(op)))
      && (/* c5 */ IS_NOOP(((struct VyattaNode*)
                             (node->parent->data))->_data._operation))) {
    //first check if there is enclosing behavior
    boolean enclosing = FALSE;
    GNode *n = node;
    while (TRUE) {
      n = n->parent;
      vtw_def def = ((struct VyattaNode*)(n->data))->_config._def;
      if (def.actions[end_act].vtw_list_head
          || def.actions[begin_act].vtw_list_head) {
        enclosing = TRUE;
        break;
      }
      if (G_NODE_IS_ROOT(n) == TRUE) {
        break;
      }
    }

    //walk back up and flip operations until enclosing behavior
    if (enclosing == TRUE) {
      GNode *n = node;
      while (TRUE) {
        n = n->parent;
        vtw_def def = ((struct VyattaNode*)(n->data))->_config._def;
        if (((struct VyattaNode*)(n->data))->_data._operation == K_NO_OP) {
          /* XXX this is ugly. _operation is intended to be a bitmap, in which
           *     case it doesn't make sense to make it an enum type (should
           *     just be, e.g., int). this causes g++ to (rightly) complain.
           *     work around it for now to avoid impacting other code since
           *     the current goal is simply cleanup.
           */
          int op = ((struct VyattaNode*)(n->data))->_data._operation;
          op |= K_ACTIVE_OP;
          ((struct VyattaNode*)(n->data))->_data._operation
            = (NODE_OPERATION) op;
        }
        if (def.actions[end_act].vtw_list_head
            || def.actions[begin_act].vtw_list_head) {
          break;
        }
        if (G_NODE_IS_ROOT(n) == TRUE) {
          break;
        }
      }
    }
  }

  if (priority_mode) {
    int gprio = gp->_config._priority;
    if (gprio < LOWEST_PRIORITY) {
      // only if priority is specified.
      //unlink from original tree
      g_node_unlink(node);

      GNode *new_node = g_node_copy(node);
      GNode *sibling = root_node->children;
      //now iterate through siblings of root_node and compare priority
      
      while (sibling
             && gprio > ((struct VyattaNode*)(sibling->data))
                        ->_config._priority) {
        sibling = sibling->next;
        if (!sibling
            || gprio < ((struct VyattaNode*)(sibling->data))
                       ->_config._priority) {
          // XXX isn't this redundant??? just cleaning up so not changing it
          break;
        }
      }

      d_dplog("commit2::sort_func(): inserting %s into transaction, "
              "priority: %d BEFORE %d", d->_name, gprio,
              (sibling
               ? ((struct VyattaNode*)(sibling->data))->_config._priority
                 : LOWEST_PRIORITY));
      g_node_insert_before(root_node,sibling,new_node);
    }
  }
  else {
    if (g_node_depth(node) == 2) {
      d_dplog("commit2::sort_func(): insert %s into transaction", d->_name);
      GNode *new_node = g_node_copy(node);
      g_node_insert(root_node,-1,new_node); //make a flat structure for now
    }
  }
  return FALSE;
}
Esempio n. 16
0
static void
g_node_test (void)
{
  GNode *root;
  GNode *node;
  GNode *node_B;
  GNode *node_D;
  GNode *node_F;
  GNode *node_G;
  GNode *node_J;
  guint i;
  gchar *tstring;

  failed = FALSE;

  root = g_node_new (C2P ('A'));
  TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);

  node_B = g_node_new (C2P ('B'));
  g_node_append (root, node_B);
  TEST (NULL, root->children == node_B);

  g_node_append_data (node_B, C2P ('E'));
  g_node_prepend_data (node_B, C2P ('C'));
  node_D = g_node_new (C2P ('D'));
  g_node_insert (node_B, 1, node_D); 

  node_F = g_node_new (C2P ('F'));
  g_node_append (root, node_F);
  TEST (NULL, root->children->next == node_F);

  node_G = g_node_new (C2P ('G'));
  g_node_append (node_F, node_G);
  node_J = g_node_new (C2P ('J'));
  g_node_prepend (node_G, node_J);
  g_node_insert (node_G, 42, g_node_new (C2P ('K')));
  g_node_insert_data (node_G, 0, C2P ('H'));
  g_node_insert (node_G, 1, g_node_new (C2P ('I')));

  TEST (NULL, g_node_depth (root) == 1);
  TEST (NULL, g_node_max_height (root) == 4);
  TEST (NULL, g_node_depth (node_G->children->next) == 4);
  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
  TEST (NULL, g_node_max_height (node_F) == 3);
  TEST (NULL, g_node_n_children (node_G) == 4);
  TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
  TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
  TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);

  for (i = 0; i < g_node_n_children (node_B); i++)
    {
      node = g_node_nth_child (node_B, i);
      TEST (NULL, P2C (node->data) == ('C' + i));
    }
  
  for (i = 0; i < g_node_n_children (node_G); i++)
    TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);

  /* we have built:                    A
   *                                 /   \
   *                               B       F
   *                             / | \       \
   *                           C   D   E       G
   *                                         / /\ \
   *                                       H  I  J  K
   *
   * for in-order traversal, 'G' is considered to be the "left"
   * child of 'F', which will cause 'F' to be the last node visited.
   */

  tstring = NULL;
  g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
  TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
  g_free (tstring); tstring = NULL;
  g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
  TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
  g_free (tstring); tstring = NULL;
  g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
  TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
  g_free (tstring); tstring = NULL;
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
  TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
  g_free (tstring); tstring = NULL;
  
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
  TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
  g_free (tstring); tstring = NULL;
  g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
  TEST (tstring, strcmp (tstring, "ABFG") == 0);
  g_free (tstring); tstring = NULL;

  g_node_reverse_children (node_B);
  g_node_reverse_children (node_G);

  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
  TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
  g_free (tstring); tstring = NULL;
  
  g_node_append (node_D, g_node_new (C2P ('L')));
  g_node_append (node_D, g_node_new (C2P ('M')));

  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
  TEST (tstring, strcmp (tstring, "ABFEDCGLMKJIH") == 0);
  g_free (tstring); tstring = NULL;

  g_node_destroy (root);

  /* allocation tests */

  root = g_node_new (NULL);
  node = root;

  for (i = 0; i < 2048; i++)
    {
      g_node_append (node, g_node_new (NULL));
      if ((i%5) == 4)
	node = node->children->next;
    }
  TEST (NULL, g_node_max_height (root) > 100);
  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);

  g_node_destroy (root);
  
  if (failed)
    exit(1);
}
Esempio n. 17
0
static void
as_node_to_xml_string (GString *xml,
		       guint depth_offset,
		       const AsNode *n,
		       AsNodeToXmlFlags flags)
{
	AsNodeData *data = n->data;
	AsNode *c;
	const gchar *tag_str;
	const gchar *comment;
	guint depth = g_node_depth ((GNode *) n);
	gchar *attrs;

	/* comment */
	comment = as_node_get_comment (n);
	if (comment != NULL) {
		guint i;
		g_auto(GStrv) split = NULL;

		/* do not put additional spacing for the root node */
		if (depth_offset < g_node_depth ((GNode *) n) &&
		    (flags & AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE) > 0)
			g_string_append (xml, "\n");
		if ((flags & AS_NODE_TO_XML_FLAG_FORMAT_INDENT) > 0)
			as_node_add_padding (xml, depth - depth_offset);

		/* add each comment section */
		split = g_strsplit (comment, "<&>", -1);
		for (i = 0; split[i] != NULL; i++) {
			g_string_append_printf (xml, "<!--%s-->", split[i]);
			if ((flags & AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE) > 0)
				g_string_append (xml, "\n");
		}
	}

	/* root node */
	if (data == NULL || as_node_get_tag (n) == AS_TAG_LAST) {
		if ((flags & AS_NODE_TO_XML_FLAG_SORT_CHILDREN) > 0)
			as_node_sort_children (n->children);
		for (c = n->children; c != NULL; c = c->next)
			as_node_to_xml_string (xml, depth_offset, c, flags);

	/* leaf node */
	} else if (n->children == NULL) {
		if ((flags & AS_NODE_TO_XML_FLAG_FORMAT_INDENT) > 0)
			as_node_add_padding (xml, depth - depth_offset);
		attrs = as_node_get_attr_string (data);
		tag_str = as_tag_data_get_name (data);
		if (data->cdata == NULL || data->cdata[0] == '\0') {
			g_string_append_printf (xml, "<%s%s/>",
						tag_str, attrs);
		} else {
			as_node_cdata_to_escaped (data);
			g_string_append_printf (xml, "<%s%s>%s</%s>",
						tag_str,
						attrs,
						data->cdata,
						tag_str);
		}
		if ((flags & AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE) > 0)
			g_string_append (xml, "\n");
		g_free (attrs);

	/* node with children */
	} else {
		if ((flags & AS_NODE_TO_XML_FLAG_FORMAT_INDENT) > 0)
			as_node_add_padding (xml, depth - depth_offset);
		attrs = as_node_get_attr_string (data);
		tag_str = as_tag_data_get_name (data);
		g_string_append_printf (xml, "<%s%s>", tag_str, attrs);
		if ((flags & AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE) > 0)
			g_string_append (xml, "\n");
		g_free (attrs);
		if ((flags & AS_NODE_TO_XML_FLAG_SORT_CHILDREN) > 0)
			as_node_sort_children (n->children);
		for (c = n->children; c != NULL; c = c->next)
			as_node_to_xml_string (xml, depth_offset, c, flags);

		if ((flags & AS_NODE_TO_XML_FLAG_FORMAT_INDENT) > 0)
			as_node_add_padding (xml, depth - depth_offset);
		g_string_append_printf (xml, "</%s>", tag_str);
		if ((flags & AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE) > 0)
			g_string_append (xml, "\n");
	}
}
Esempio n. 18
0
gboolean dump(GNode *node, gpointer data) {
	    int i = g_node_depth(node);
	        while (--i) printf("-");
		    printf("%s\n", (char*) node->data);
		    return(FALSE);
}
Esempio n. 19
0
static void ExecuteStatement(GNode* node, gpointer data)
{
	Statement* stmt = (Statement*) node->data;
	Verbose("* Visiting GNode at level %d", g_node_depth(node));

	if (parseOnly && (stmt->type < STMT_REPEAT)) return;

	switch (stmt->type) {
		case STMT_ASSIGN: {
			ExpressionStatus status[2];
			ParameterList* paramList = stmt->parameters;
			gchar* varident = param_string_get(paramList, 0, &status[0]);
			Expression* expression = param_index_get(paramList, 1);

			// evaluator error check
			if (status[0] != STATUS_EVAL_OK) {
				Error("Malicious assign parameters! (status = %s)", expr_status_to_string(status[0]));
			}

			switch (expression->type) {
				case EXPR_RICH_INT:
				case EXPR_CONSTANT_INT: {
					Verbose("~ Executing STMT_ASSIGN: variable = %s, type = INT", varident);
					glong result = param_int_get(paramList, 1, &status[1]);

					// evaluator error check
					if (status[1] != STATUS_EVAL_OK) {
						Error("Malicious assign parameters! (status = %s)", expr_status_to_string(status[1]));
					}

					var_set_value(varident, VAR_INT, &result);
					break;
				}

				case EXPR_RICH_STRING:
				case EXPR_CONSTANT_STRING: {
					Verbose("~ Executing STMT_ASSIGN: variable = %s, type = STRING", varident);
					gchar* result_raw = param_string_get(paramList, 1, &status[1]);

					// evaluator error check
					if (status[1] != STATUS_EVAL_OK) {
						Error("Malicious assign parameters! (status = %s)", expr_status_to_string(status[1]));
					}

					gchar* result = var_replace_substrings(result_raw);

					var_set_value(varident, VAR_STRING, result);
					g_free(result_raw);
					g_free(result);
					break;
				}

				default: Error("Expression type %d not supported in assign statement!\n", expression->type);
			}

			g_free(varident);
			break;
		}

		case STMT_REPEAT: {
			ExpressionStatus status[2];
			ParameterList* paramList = stmt->parameters;
			gchar* varident = param_string_get(paramList, 0, &status[0]);
			glong i, loopCount = param_int_get(paramList, 1, &status[1]);

			Verbose("~ Executing STMT_REPEAT: variable = %s, loopCount = %ld", varident, loopCount);

			g_assert(loopCount >= 0);

			// evaluator error check
			if (!expr_status_assert(status, 2)) {
				backtrace(stmt);
				Error("Malicious repeat parameters! (%s:%d)", __FILE__, __LINE__);
			}

			for (i=0; i<loopCount; i++) {
				var_set_value(varident, VAR_INT, &i);
				g_node_children_foreach(node, G_TRAVERSE_ALL, &ExecuteStatement, NULL);
			}

			var_destroy(varident);
			g_free(varident);
			break;
		}

		case STMT_TIME: {
			Verbose("~ Executing STMT_TIME: label = %s", stmt->label);

			static gint timeId = 0;
			gdouble time;
			GTimer* timer = g_timer_new();

			g_node_children_foreach(node, G_TRAVERSE_ALL, &ExecuteStatement, NULL);

			g_timer_stop(timer);
			time = g_timer_elapsed(timer, NULL);
			g_timer_destroy(timer);

			gchar* label = var_replace_substrings(stmt->label);
			timeList = g_slist_prepend(timeList, timeevent_new(timeId++, label, time));
			g_free(label);
			break;
		}

		case STMT_CTIME: {
			Verbose("~ Executing STMT_CTIME: label = %s", stmt->label);

			static gint coreTimeId = 0;
			gchar* label = var_replace_substrings(stmt->label);
			CoreTimeEvent* coreTimeEvent = coretime_event_new(coreTimeId++, label, coretime_new(0, 0));
			coreTimeStack = g_list_prepend(coreTimeStack, coreTimeEvent);

			g_node_children_foreach(node, G_TRAVERSE_ALL, &ExecuteStatement, NULL);

			coreTimeList = g_slist_prepend(coreTimeList, coreTimeEvent);
			coreTimeStack = g_list_remove_link(coreTimeStack, g_list_first(coreTimeStack));
			g_free(label);
			break;
		}

#ifdef HAVE_MPI
		case STMT_GROUP: {
			Verbose("~ Executing STMT_GROUP: group = %s", stmt->label);

			GroupBlock* groupBlock = g_hash_table_lookup(groupMap, stmt->label);

			if(groupBlock && groupBlock->member) {
				groupStack = g_list_prepend(groupStack, groupBlock);
				g_node_children_foreach(node, G_TRAVERSE_ALL, &ExecuteStatement, NULL);
				groupStack = g_list_remove_link(groupStack, g_list_first(groupStack));
			}
			else if(!groupBlock) {
				backtrace(stmt);
				Error("Group \"%s\" doesn't exist!", stmt->label);
			}
			break;
		}

		case STMT_MASTER: {
			GroupBlock* groupBlock;
			MPI_Comm comm = MPI_COMM_WORLD;
			if(groupStack) {
				groupBlock = (GroupBlock*) g_list_first(groupStack)->data;
				comm = groupBlock->mpicomm;
			}

			gint groupRank;
			MPI_ASSERT(MPI_Comm_rank(comm, &groupRank), "Master Statement", TRUE)

			if(groupStack) Verbose("~ Executing STMT_MASTER: rank = %d, type = implicit group", groupRank);
			else           Verbose("~ Executing STMT_MASTER: rank = %d, type = world", groupRank);

			if(groupRank == MASTER) {
				g_node_children_foreach(node, G_TRAVERSE_ALL, &ExecuteStatement, NULL);
			}
			else Verbose("Im not the master here... groupRank = %d", groupRank);
			break;
		}

		case STMT_BARRIER: {
			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			gchar* groupName = param_string_get_optional(paramList, 0, &status[0], NULL);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			GroupBlock* groupBlock = groupblock_get(groupName);

			if(groupName)  Verbose("~ Executing STMT_BARRIER: type = explicit group, name = %s", groupName);
			else           Verbose("~ Executing STMT_BARRIER: type = implicit active group");

			MPI_ASSERT(MPI_Barrier(groupBlock->mpicomm), "Barrier Statement", TRUE)

			g_free(groupName);
			break;
		}
#endif

		case STMT_SLEEP: {
			if (agileMode) return;
			Verbose("~ Executing STMT_SLEEP");

			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			glong time = param_int_get(paramList, 0, &status[0]);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			g_usleep(time);
			//sleep(time);
			break;
		}

		case STMT_BLOCK: {
			Verbose("~ Executing STMT_BLOCK");
			g_node_children_foreach(node, G_TRAVERSE_ALL, &ExecuteStatement, NULL);
			break;
		}

		case STMT_PRINT: {
			Verbose("~ Executing STMT_PRINT");

			ParameterList* paramList = stmt->parameters;
			GString* buffer = g_string_new("");
			ExpressionStatus status;
			gint i;

			for (i=0; i<paramList->len; i++) {
				gchar* string = param_string_get(paramList, i, &status);
				if (status == STATUS_EVAL_OK) {
					g_string_append(buffer, string);
					g_free(string);
					if (i < (paramList->len-1))
						g_string_append(buffer, " ");
				}
				else {
					backtrace(stmt);
					Error("Error during print parameter evaluation (index=%d, status=%s).\n",
							i, expr_status_to_string(status));
				}
			}

			gchar* processed = var_replace_substrings(buffer->str);
			g_printf("[%d] %s\n", rank, processed);
			g_free(processed);
			g_string_free(buffer, TRUE);
			break;
		}

		case STMT_FCREAT: {
			ExpressionStatus status[2];
			ParameterList* paramList = stmt->parameters;
			gchar* fhname = param_string_get(paramList, 0, &status[0]);
			gchar* fname_raw = param_string_get(paramList, 1, &status[1]);

			Verbose("~ Executing STMT_FCREAT: fhname = %s, fname = %s", fhname, fname_raw);

			// evaluator error check
			if (!expr_status_assert(status, 2)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			File* file;
			IOStatus ioStatus = iio_fcreat(fname, &file);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success) {
				Verbose("  > file = %p", file);
				var_set_value(fhname, VAR_FILE, &file);
				statementsSucceed[STMT_FCREAT]++;
			}
			else
				statementsFail[STMT_FCREAT]++;

			g_free(fhname);
			g_free(fname_raw);
			g_free(fname);
			break;
		}

		case STMT_FOPEN: {
			ExpressionStatus status[3];
			ParameterList* paramList = stmt->parameters;
			gchar* fhname = param_string_get(paramList, 0, &status[0]);
			gchar* fname_raw = param_string_get(paramList, 1, &status[1]);
			gint   flags = param_int_get(paramList, 2, &status[2]);

			Verbose("~ Executing STMT_FOPEN: fhname = %s, fname = %s, flags = %d", fhname, fname_raw, flags);

			// evaluator error check
			if (!expr_status_assert(status, 3)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			File* file;
			IOStatus ioStatus = iio_fopen(fname, flags, &file);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success) {
				Verbose("  > file = %p", file);
				var_set_value(fhname, VAR_FILE, &file);
				statementsSucceed[STMT_FOPEN]++;
			}
			else
				statementsFail[STMT_FOPEN]++;

			g_free(fhname);
			g_free(fname_raw);
			g_free(fname);
			break;
		}

		case STMT_FCLOSE: {
			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			File* file = param_file_get(paramList, 0, &status[0]);

			Verbose("~ Executing STMT_FCLOSE: file = %p", file);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			g_assert(file);

			IOStatus ioStatus = iio_fclose(file);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success) {
				gchar* fhname = (gchar*) param_value_get(paramList, 0);
				var_destroy(fhname);
				statementsSucceed[STMT_FCLOSE]++;
			}
			else
				statementsFail[STMT_FCLOSE]++;
			break;
		}

		case STMT_FREAD: {
			ExpressionStatus status[3];
			ParameterList* paramList = stmt->parameters;
			File* file = param_file_get(paramList, 0, &status[0]);
			glong dataSize = param_int_get_optional(paramList, 1, &status[1], READALL);
			glong offset = param_int_get_optional(paramList, 2, &status[2], OFFSET_CUR);

			Verbose("~ Executing STMT_FREAD: file = %p, dataSize = %ld, offset = %ld", file, dataSize, offset);

			// evaluator error check
			if (!expr_status_assert(status, 3)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			IOStatus ioStatus = iio_fread(file, dataSize, offset);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_FREAD]++;
			else
				statementsFail[STMT_FREAD]++;
			break;
		}

		case STMT_FWRITE: {
			ExpressionStatus status[3];
			ParameterList* paramList = stmt->parameters;
			File* file = param_file_get(paramList, 0, &status[0]);
			glong dataSize = param_int_get(paramList, 1, &status[1]);
			glong offset = param_int_get_optional(paramList, 2, &status[2], -1);

			Verbose("~ Executing STMT_FWRITE: file = %p, dataSize = %ld, offset = %ld", file, dataSize, offset);

			// evaluator error check
			if (!expr_status_assert(status, 3)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			IOStatus ioStatus = iio_fwrite(file, dataSize, offset);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_FWRITE]++;
			else
				statementsFail[STMT_FWRITE]++;
			break;
		}

		case STMT_FSEEK: {
			ExpressionStatus status[3];
			ParameterList* paramList = stmt->parameters;
			File* file = param_file_get(paramList, 0, &status[0]);
			glong offset = param_int_get(paramList, 1, &status[1]);
			gint whence = param_int_get_optional(paramList, 2, &status[2], SEEK_SET);

			Verbose("~ Executing STMT_FSEEK: file = %p, offset = %ld, whence = %d", file, offset, whence);

			// evaluator error check
			if (!expr_status_assert(status, 3)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			IOStatus ioStatus = iio_fseek(file, offset, whence);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_FSEEK]++;
			else
				statementsFail[STMT_FSEEK]++;
			break;
		}

		case STMT_FSYNC: {
			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			File* file = param_file_get(paramList, 0, &status[0]);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			IOStatus ioStatus = iio_fsync(file);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_FSYNC]++;
			else {
				statementsFail[STMT_FSYNC]++;
			}
			break;
		}

		case STMT_WRITE: {
			ExpressionStatus status[3];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);
			glong  dataSize = param_int_get(paramList, 1, &status[1]);
			glong  offset = param_int_get_optional(paramList, 2, &status[2], 0);

			Verbose("~ Executing STMT_WRITE: file = %s, dataSize = %ld, offset = %ld", fname_raw, dataSize, offset);

			// evaluator error check
			if (!expr_status_assert(status, 3)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			IOStatus ioStatus = iio_write(fname, dataSize, offset);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_WRITE]++;
			else
				statementsFail[STMT_WRITE]++;

			g_free(fname_raw);
			g_free(fname);
			break;
		}

		case STMT_APPEND: {
			ExpressionStatus status[2];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);
			glong  dataSize = param_int_get(paramList, 1, &status[1]);

			Verbose("~ Executing STMT_APPEND: file = %s, dataSize = %ld", fname_raw, dataSize);

			// evaluator error check
			if (!expr_status_assert(status, 2)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			IOStatus ioStatus = iio_append(fname, dataSize);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_APPEND]++;
			else
				statementsFail[STMT_APPEND]++;

			g_free(fname_raw);
			g_free(fname);
			break;
		}

		case STMT_READ: {
			ExpressionStatus status[3];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);
			glong  dataSize = param_int_get_optional(paramList, 1, &status[1], READALL);
			glong  offset = param_int_get_optional(paramList, 2, &status[2], 0);

			Verbose("~ Executing STMT_READ: file = %s, dataSize = %ld, offset = %ld",
					fname_raw, dataSize, offset);

			// evaluator error check
			if (!expr_status_assert(status, 2)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			IOStatus ioStatus = iio_read(fname, dataSize, offset);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_READ]++;
			else
				statementsFail[STMT_READ]++;

			g_free(fname_raw);
			g_free(fname);
			break;
		}

		case STMT_LOOKUP: {
			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);

			Verbose("~ Executing STMT_LOOKUP: file = %s", fname_raw);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			IOStatus ioStatus = iio_lookup(fname);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_LOOKUP]++;
			else
				statementsFail[STMT_LOOKUP]++;

			g_free(fname_raw);
			g_free(fname);
			break;
		}

		case STMT_DELETE: {
			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);

			Verbose("~ Executing STMT_DELETE: file = %s", fname_raw);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			IOStatus ioStatus = iio_delete(fname);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_DELETE]++;
			else
				statementsFail[STMT_DELETE]++;

			g_free(fname_raw);
			g_free(fname);
			break;
		}

		case STMT_MKDIR: {
			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);

			Verbose("~ Executing STMT_MKDIR: file = %s", fname_raw);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			IOStatus ioStatus = iio_mkdir(fname);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_MKDIR]++;
			else
				statementsFail[STMT_MKDIR]++;

			g_free(fname_raw);
			g_free(fname);
			break;
		}

		case STMT_RMDIR: {
			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);

			Verbose("~ Executing STMT_RMDIR: file = %s", fname_raw);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			IOStatus ioStatus = iio_rmdir(fname);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_RMDIR]++;
			else
				statementsFail[STMT_RMDIR]++;

			g_free(fname_raw);
			g_free(fname);
			break;
		}

		case STMT_CREATE: {
			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);

			Verbose("~ Executing STMT_CREATE: file = %s", fname_raw);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			IOStatus ioStatus = iio_create(fname);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_CREATE]++;
			else
				statementsFail[STMT_CREATE]++;

			g_free(fname_raw);
			g_free(fname);
			break;
		}

		case STMT_STAT: {
			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);

			Verbose("~ Executing STMT_STAT: file = %s", fname_raw);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			IOStatus ioStatus = iio_stat(fname);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_STAT]++;
			else
				statementsFail[STMT_STAT]++;

			g_free(fname_raw);
			g_free(fname);
			break;
		}

		case STMT_RENAME: {
			ExpressionStatus status[2];
			ParameterList* paramList = stmt->parameters;
			gchar* oldname_raw = param_string_get(paramList, 0, &status[0]);
			gchar* newname_raw = param_string_get(paramList, 1, &status[1]);

			Verbose("~ Executing STMT_RENAME: oldname = %s, newname = %s", oldname_raw, oldname_raw);

			// evaluator error check
			if (!expr_status_assert(status, 2)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* oldname = var_replace_substrings(oldname_raw);
			gchar* newname = var_replace_substrings(newname_raw);

			IOStatus ioStatus = iio_rename(oldname, newname);
			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_RENAME]++;
			else
				statementsFail[STMT_RENAME]++;

			g_free(oldname_raw);
			g_free(newname_raw);
			g_free(oldname);
			g_free(newname);
			break;
		}

#ifdef HAVE_MPI
		case STMT_PFOPEN: {
			ExpressionStatus status[3];
			ParameterList* paramList = stmt->parameters;
			gchar* fhname = param_string_get(paramList, 0, &status[0]);
			gchar* fname_raw = param_string_get(paramList, 1, &status[1]);
			gchar* mode = param_string_get(paramList, 2, &status[2]);

			Verbose("~ Executing STMT_FOPEN: fhname = %s, fname = %s mode = %s", fhname, fname_raw, mode);

			// evaluator error check
			if (!expr_status_assert(status, 3)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);
			MPI_Comm comm = MPI_COMM_WORLD;
			if(groupStack)
				comm = ((GroupBlock*) g_list_first(groupStack)->data)->mpicomm;

			File* file;
			if (iio_pfopen(fname, mode, comm, &file)) {
				Verbose("  > file = %p", file);
				var_set_value(fhname, VAR_FILE, &file);
				statementsSucceed[STMT_PFOPEN]++;
			}
			else
				statementsFail[STMT_PFOPEN]++;

			g_free(fhname);
			g_free(fname_raw);
			g_free(fname);
			g_free(mode);
			break;
		}

		case STMT_PFCLOSE: {
			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			File* file = param_file_get(paramList, 0, &status[0]);

			Verbose("~ Executing STMT_FCLOSE: file = %p", file);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			if (file && iio_pfclose(file)) {
				gchar* fhname = (gchar*) param_value_get(paramList, 0);
				var_destroy(fhname);
				statementsSucceed[STMT_PFCLOSE]++;
			}
			else
				statementsFail[STMT_PFCLOSE]++;
			break;
		}

		case STMT_PFWRITE: {
			ExpressionStatus status[2];
			ParameterList* paramList = stmt->parameters;
			File*  file = param_file_get(paramList, 0, &status[0]);
			gchar* pname = param_string_get(paramList, 1, &status[1]);

			Verbose("~ Executing STMT_PFWRITE: file = %p, pattern = %s", file, pname);

			// evaluator error check
			if (!expr_status_assert(status, 2)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			Pattern* pattern = g_hash_table_lookup(patternMap, pname);
			if (!pattern) {
				g_printf("Invalid pattern parameter in statement pfwrite.\n");
				Error("Malicious statement parameters!");
				break;
			}

			MPI_Comm comm = MPI_COMM_WORLD;
			if(groupStack)
				comm = ((GroupBlock*) g_list_first(groupStack)->data)->mpicomm;

			IOStatus ioStatus;
			switch(pattern->level) {
				/* Level 0: non-collective, contiguous */
				case 0:
					Verbose("  > level = 0");
					ioStatus = iio_pfwrite_level0(file, pattern);
					break;

				/* Level 1: collective, contiguous */
				case 1:
					Verbose("  > level = 1");
					ioStatus = iio_pfwrite_level1(file, pattern);
					break;

				/* Level 2: non-collective, non-contiguous */
				case 2:
					Verbose("  > level = 2");
					ioStatus = iio_pfwrite_level2(file, pattern);
					break;

				/* Level 3: collective, non-contiguous */
				case 3:
					Verbose("  > level = 3");
					ioStatus = iio_pfwrite_level3(file, pattern);
					break;

				default: Error("Invalid level (%d) for statement pfwrite!", pattern->level);
			}

			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_PFWRITE]++;
			else
				statementsFail[STMT_PFWRITE]++;

			g_free(pname);
			break;
		}

		case STMT_PFREAD: {
			ExpressionStatus status[2];
			ParameterList* paramList = stmt->parameters;
			File*  file = param_file_get(paramList, 0, &status[0]);
			gchar* pname = param_string_get(paramList, 1, &status[1]);

			Verbose("~ Executing STMT_PFREAD: file = %p, pattern = %s", file, pname);

			// evaluator error check
			if (!expr_status_assert(status, 2)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			Pattern* pattern = g_hash_table_lookup(patternMap, pname);
			if (!pattern) {
				g_printf("Invalid pattern parameter in statement pread.\n");
				Error("Malicious statement parameters!");
				break;
			}

			MPI_Comm comm = MPI_COMM_WORLD;
			if(groupStack)
				comm = ((GroupBlock*) g_list_first(groupStack)->data)->mpicomm;

			IOStatus ioStatus;
			switch(pattern->level) {
				/* Level 0: non-collective, contiguous */
				case 0:
					Verbose("  > level = 0");
					ioStatus = iio_pfread_level0(file, pattern);
					break;

				/* Level 1: collective, contiguous */
				case 1:
					Verbose("  > level = 1");
					ioStatus = iio_pfread_level1(file, pattern);
					break;

				/* Level 2: non-collective, non-contiguous */
				case 2:
					Verbose("  > level = 2");
					ioStatus = iio_pfread_level2(file, pattern);
					break;

				/* Level 3: collective, non-contiguous */
				case 3:
					Verbose("  > level = 3");
					ioStatus = iio_pfread_level3(file, pattern);
					break;

				default: Error("Invalid level (%d) for statement pfread!", pattern->level);
			}

			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_PFREAD]++;
			else
				statementsFail[STMT_PFREAD]++;

			g_free(pname);
			break;
		}

		case STMT_PWRITE: {
			ExpressionStatus status[2];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);
			gchar* pname = param_string_get(paramList, 1, &status[1]);

			Verbose("~ Executing STMT_PWRITE: file = %s, pattern = %s", fname_raw, pname);

			// evaluator error check
			if (!expr_status_assert(status, 2)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			Pattern* pattern = g_hash_table_lookup(patternMap, pname);
			if (!pattern) {
				g_printf("Invalid pattern parameter in statement pwrite.\n");
				Error("Malicious statement parameters!");
				break;
			}

			MPI_Comm comm = MPI_COMM_WORLD;
			if(groupStack)
				comm = ((GroupBlock*) g_list_first(groupStack)->data)->mpicomm;

			IOStatus ioStatus;
			switch(pattern->level) {
				/* Level 0: non-collective, contiguous */
				case 0:
					Verbose("  > level = 0");
					ioStatus = iio_pwrite_level0(fname, pattern, comm);
					break;

				/* Level 1: collective, contiguous */
				case 1:
					Verbose("  > level = 1");
					ioStatus = iio_pwrite_level1(fname, pattern, comm);
					break;

				/* Level 2: non-collective, non-contiguous */
				case 2:
					Verbose("  > level = 2");
					ioStatus = iio_pwrite_level2(fname, pattern, comm);
					break;

				/* Level 3: collective, non-contiguous */
				case 3:
					Verbose("  > level = 3");
					ioStatus = iio_pwrite_level3(fname, pattern, comm);
					break;

				default: Error("Invalid level (%d) for statement pwrite!", pattern->level);
			}

			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_PWRITE]++;
			else
				statementsFail[STMT_PWRITE]++;

			g_free(fname_raw);
			g_free(fname);
			g_free(pname);
			break;
		}

		case STMT_PREAD: {
			ExpressionStatus status[2];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);
			gchar* pname = param_string_get(paramList, 1, &status[1]);

			Verbose("~ Executing STMT_PREAD: file = %s, pattern = %s", fname_raw, pname);

			// evaluator error check
			if (!expr_status_assert(status, 2)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			Pattern* pattern = g_hash_table_lookup(patternMap, pname);
			if (!pattern) {
				g_printf("Invalid pattern parameter in statement pread.\n");
				g_free(fname);
				Error("Malicious statement parameters!");
				break;
			}

			MPI_Comm comm = MPI_COMM_WORLD;
			if(groupStack)
				comm = ((GroupBlock*) g_list_first(groupStack)->data)->mpicomm;

			IOStatus ioStatus;
			switch(pattern->level) {
				/* Level 0: non-collective, contiguous */
				case 0:
					Verbose("  > level = 0");
					ioStatus = iio_pread_level0(fname, pattern, comm);
					break;

				/* Level 1: collective, contiguous */
				case 1:
					Verbose("  > level = 1");
					ioStatus = iio_pread_level1(fname, pattern, comm);
					break;

				/* Level 2: non-collective, non-contiguous */
				case 2:
					Verbose("  > level = 2");
					ioStatus = iio_pread_level2(fname, pattern, comm);
					break;

				/* Level 3: collective, non-contiguous */
				case 3:
					Verbose("  > level = 3");
					ioStatus = iio_pread_level3(fname, pattern, comm);
					break;

				default: Error("Invalid level (%d) for statement pread!", pattern->level);
			}

			dump_coretime(coreTimeStack, ioStatus.coreTime);

			if (ioStatus.success)
				statementsSucceed[STMT_PREAD]++;
			else
				statementsFail[STMT_PREAD]++;

			g_free(fname_raw);
			g_free(fname);
			g_free(pname);
			break;
		}

		case STMT_PDELETE: {
			ExpressionStatus status[1];
			ParameterList* paramList = stmt->parameters;
			gchar* fname_raw = param_string_get(paramList, 0, &status[0]);

			Verbose("~ Executing STMT_PDELETE: file = %s", fname_raw);

			// evaluator error check
			if (!expr_status_assert(status, 1)) {
				backtrace(stmt);
				Error("Malicious statement parameters!");
			}

			gchar* fname = var_replace_substrings(fname_raw);

			if (iio_pdelete(fname))
				statementsSucceed[STMT_PDELETE]++;
			else
				statementsFail[STMT_PDELETE]++;

			g_free(fname_raw);
			g_free(fname);
			break;
		}
#endif

		/* ![ModuleHook] statement_exec */

		default: Error("Invalid statement! (id=%d)\n", stmt->type);
	}
}
Esempio n. 20
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;
}