示例#1
0
文件: toxic.c 项目: gracchus163/toxic
static void load_data(Tox *m, char *path)
{
    if (arg_opts.ignore_data_file)
        return;

    FILE *fd;

    if ((fd = fopen(path, "rb")) != NULL) {
        fseek(fd, 0, SEEK_END);
        int len = ftell(fd);
        fseek(fd, 0, SEEK_SET);

        char *buf = malloc(len);

        if (buf == NULL) {
            fclose(fd);
            exit_toxic_err("failed in load_data", FATALERR_MEMORY);
        }

        if (fread(buf, len, 1, fd) != 1) {
            free(buf);
            fclose(fd);
            exit_toxic_err("failed in load_data", FATALERR_FREAD);
        }

        tox_load(m, (uint8_t *) buf, len);
        load_friendlist(m);

        free(buf);
        fclose(fd);
    } else {
        if (store_data(m, path) != 0)
            exit_toxic_err("failed in load_data", FATALERR_STORE_DATA);
    }
}
示例#2
0
static Tox *load_toxic(char *data_path)
{
    struct Tox_Options tox_opts;
    init_tox_options(&tox_opts);

    TOX_ERR_NEW new_err;
    Tox *m = load_tox(data_path, &tox_opts, &new_err);

    if (new_err == TOX_ERR_NEW_PORT_ALLOC && tox_opts.ipv6_enabled) {
        queue_init_message("Falling back to ipv4");
        tox_opts.ipv6_enabled = false;
        m = load_tox(data_path, &tox_opts, &new_err);
    }

    if (!m)
        exit_toxic_err("tox_new returned fatal error", new_err);

    if (new_err != TOX_ERR_NEW_OK)
        queue_init_message("tox_new returned non-fatal error %d", new_err);

    init_tox_callbacks(m);
    load_friendlist(m);
    load_blocklist(BLOCK_FILE);

    if (tox_self_get_name_size(m) == 0)
        tox_self_set_name(m, (uint8_t *) "Toxic User", strlen("Toxic User"), NULL);

    return m;
}
示例#3
0
文件: main.c 项目: stqism/toxic
static void load_data(Tox *m, char *path)
{
    if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
        return;

    FILE *fd;
    size_t len;
    uint8_t *buf;

    if ((fd = fopen(path, "rb")) != NULL) {
        fseek(fd, 0, SEEK_END);
        len = ftell(fd);
        fseek(fd, 0, SEEK_SET);

        buf = malloc(len);

        if (buf == NULL) {
            fclose(fd);
            endwin();
            fprintf(stderr, "malloc() failed. Aborting...\n");
            exit(EXIT_FAILURE);
        }

        if (fread(buf, len, 1, fd) != 1) {
            free(buf);
            fclose(fd);
            endwin();
            fprintf(stderr, "fread() failed. Aborting...\n");
            exit(EXIT_FAILURE);
        }

        tox_load(m, buf, len);
        load_friendlist(m);

        free(buf);
        fclose(fd);
    } else {
        int st;

        if ((st = store_data(m, path)) != 0) {
            endwin();
            fprintf(stderr, "Store messenger failed with return code: %d\n", st);
            exit(EXIT_FAILURE);
        }
    }
}