Example #1
0
void dfs( node_t* current, node_t* parent )
{
    if(current == NULL)
        return;

    flatten_list(current);
    prune_print_list(current, parent);
    for(int i = 0; i < current->n_children; i++) {
        dfs(current->children[i], current);
    }
    if(current->type.index == EXPRESSION)
        calculate_expr(current, parent);

}
Example #2
0
D flatten_list(D list){
    D a=NULL;
    while(list != NULL){
        D b;
        if(list->type == LIST)
            b = flatten_list(list->subl);
        else
            b = make_string(list->str);
        if(a == NULL)
            a = b;
        else
            append_list(a, b);
        list = list->next;
    }
    return a;
}
Example #3
0
int serialise_args(task *tsk, pntr args, array *arr)
{
  pntr *argvalues = NULL;
  int nargs;
  int i;

  if (0 > (nargs = flatten_list(args,&argvalues)))
    return set_error(tsk,"jcall: args is not a valid list");

  for (i = 0; i < nargs; i++)
    if (!serialise_arg(tsk,arr,i,argvalues[i]))
      break;

  free(argvalues);
  return (i == nargs);
}
Example #4
0
/**
 * Move playlists
 */
int sort_playlists(sp_session *session)
{
	sp_playlistcontainer *pc = sp_session_playlistcontainer(session);
	sp_playlist_type playlist_type;
	int i, not_loaded = 0, num_playlists = 0;
	int *reorder;
	sp_playlist *pl;
	node *items, *parent, *previous;
	
	
#ifdef TESTING
	playlist_item *faux_playlist;
#endif
	
	num_playlists = sp_playlistcontainer_num_playlists(pc);
	items = previous = parent = NULL;
	
#ifdef TESTING
	faux_playlist = (playlist_item*) malloc(sizeof(playlist_item) * num_playlists);
#endif

	printf("Reordering %d playlists and playlist folders\n", num_playlists);
	
	for (i = 0; i < num_playlists; ++i) {
		playlist_type = sp_playlistcontainer_playlist_type(pc, i);
		
		switch (playlist_type) {
				
			case SP_PLAYLIST_TYPE_PLAYLIST:
				
				pl = sp_playlistcontainer_playlist(pc, i);
				if (!sp_playlist_is_loaded(pl)) {
					not_loaded++;
				} else {
					previous = create_node(previous, parent, create_playlist_item(i, sp_playlist_name(pl)));
					if (items == NULL) {
						items = previous;
					}
				}
				
#ifdef TESTING
				faux_playlist[i].index = -1;
				faux_playlist[i].name = strdup(sp_playlist_name(pl));
#endif
				
				break;
			case SP_PLAYLIST_TYPE_START_FOLDER:
				
				parent = create_node(previous, parent, create_playlist_item(i, sp_playlistcontainer_playlist_folder_name(pc, i)));
				previous = NULL;
				if (items == NULL) {
					items = parent;
				}
				
#ifdef TESTING
				faux_playlist[i].index = sp_playlistcontainer_playlist_folder_id(pc, i);
				faux_playlist[i].name = strdup(sp_playlistcontainer_playlist_folder_name(pc, i));
#endif
				
				break;
			case SP_PLAYLIST_TYPE_END_FOLDER:
				
				previous = parent;
				previous->item->end_index = i;
				parent = parent->parent;
				
#ifdef TESTING
				faux_playlist[i].index = sp_playlistcontainer_playlist_folder_id(pc, i);
				faux_playlist[i].name = NULL;
#endif
				
				break;
			case SP_PLAYLIST_TYPE_PLACEHOLDER:

#ifdef TESTING
				printf("%d. Placeholder", i);
				faux_playlist[i].index = -1;
				faux_playlist[i].name = NULL;
#endif
				
				break;
		}
	}
	
	if(not_loaded > 0) {
		printf("ERROR: %d playlists could not be loaded\n", not_loaded);
		return 1;
	}
	
	if(items != NULL) {
		items = sort_list(items);

#ifdef TESTING
		print_list(items);
#endif
		
		reorder = (int *) malloc(sizeof(int) * num_playlists);
		flatten_list(items, reorder);
		
		for(i = 0; i < num_playlists; ++i) {
			printf(".");
			if(i != reorder[i]) {
#ifdef TESTING
				printf("Moving item at %d -> %d\n", reorder[i], i);
				move_playlist(faux_playlist, num_playlists, reorder[i], i);
#else			
				sp_playlistcontainer_move_playlist(pc, reorder[i], i);
#endif
				recalculate_indexes(reorder, num_playlists, i);
			}
		}
		printf("\ndone\n");
		
		free(reorder);
		free_list(items);
	}
	
	
#ifdef TESTING
	for(i = 0; i < num_playlists; ++i) {
		if(faux_playlist[i].name != NULL) {
			printf(" -- %s (%d)\n", faux_playlist[i].name, faux_playlist[i].index);
			free((void*)faux_playlist[i].name);
		} else {
			printf(" -- %d\n", faux_playlist[i].index);
		}
	}
	free(faux_playlist);
#endif
	
	return 1;
}