示例#1
0
static int so_atom_add(session_opt_t *so, const char *id, char *buf, 
    size_t size, void *arg)
{
    atom_t *atom = NULL, *old = NULL;
    size_t new_size, old_found = 0;

    dbg_err_if (so == NULL);
    dbg_err_if (id == NULL);
    dbg_err_if (buf == NULL);

    /* get the old atom associated to this id */
    if(!atoms_get(so->atoms, id, &old))
        old_found = 1;

    /* delete the oldest session if there are already max_count sessions */
    if(so->max_count && atoms_count(so->atoms) - old_found >= so->max_count)
        dbg_err_if(session_delete_oldest(so));

    /* delete the oldest session(s) if we are using already mem_limit bytes */
    if(so->mem_limit)
    {
        warn_err_ifm(size > so->mem_limit, 
            "session size is bigger the memory.limit, save aborted...");
        for(;;)
        {
            /* new_size = size of all atoms + size of the atom we're going to
               add - the size of the atom (if found) we're going to remove */
            new_size = atoms_size(so->atoms) + size - (old ? old->size : 0);
            if(atoms_count(so->atoms) && new_size > so->mem_limit)
                dbg_err_if(session_delete_oldest(so));
            else
                break;
        }
    }

    /* create a new atom */
    dbg_err_if(atom_create(id, buf, size, arg, &atom));

    /* add it to the list */
    dbg_err_if(atoms_add(so->atoms, atom));

    /* remove the old atom associated to this id */
    if(old)
    {
        dbg_err_if(atoms_remove(so->atoms, old));
        atom_free(old);
    }

    return 0;
err:
    if(atom)
        atom_free(atom);
    return ~0;
}
示例#2
0
文件: sys.c 项目: ilyak/vimol
void
sys_add_atom(struct sys *sys, const char *name, vec_t xyz)
{
	struct atoms *atoms = sys_get_atoms(sys, sys->current_frame);

	assert(sys_single_frame(sys));

	atoms_add(atoms, name, xyz);
	graph_vertex_add(sys->graph);
	sel_expand(sys->sel);
	sel_expand(sys->visible);
	sel_add(sys->visible, sel_get_size(sys->visible) - 1);

	sys->is_modified = TRUE;
}
示例#3
0
文件: sys.c 项目: ilyak/vimol
static int
load_from_xyz(struct sys *sys, const char *path, int is_new)
{
	FILE *fp;
	char *buffer;
	struct atoms *atoms;
	struct atom atom;
	int i, n;

	if ((fp = fopen(path, "r")) == NULL) {
		error_set("unable to open %s", path);
		return (FALSE);
	}

	if ((buffer = util_next_line(NULL, fp)) == NULL) {
		error_set("unexpected end of file");
		goto error;
	}

	if (sscanf(buffer, "%d", &n) != 1 || n < 1) {
		error_set("unexpected number of atoms");
		goto error;
	}

	buffer = util_next_line(buffer, fp);

	for (i = 0; i < n; i++) {
		if ((buffer = util_next_line(buffer, fp)) == NULL) {
			error_set("unexpected end of file");
			goto error;
		}

		if (!parse_atom_xyz(buffer, &atom))
			goto error;

		sys_add_atom(sys, atom.name, atom.xyz);
	}

	if (!is_new) {
		free(buffer);
		fclose(fp);
		return (TRUE);
	}

	while ((buffer = util_next_line(buffer, fp)) != NULL) {
		if (util_is_empty(buffer))
			continue;

		if (sscanf(buffer, "%d", &n) != 1 || n != sys_get_atom_count(sys)) {
			error_set("unexpected number of atoms");
			goto error;
		}

		sys_add_frame(sys, atoms_create());
		sys_set_frame(sys, sys_get_frame_count(sys) - 1);

		buffer = util_next_line(buffer, fp);

		for (i = 0; i < n; i++) {
			if ((buffer = util_next_line(buffer, fp)) == NULL) {
				error_set("unexpected end of file");
				goto error;
			}

			if (!parse_atom_xyz(buffer, &atom))
				goto error;

			atoms = sys_get_atoms(sys, sys->current_frame);
			atoms_add(atoms, atom.name, atom.xyz);
		}
	}

	sys_set_frame(sys, 0);
	fclose(fp);
	return (TRUE);
error:
	free(buffer);
	fclose(fp);
	return (FALSE);
}