示例#1
0
/* open/merge the dnd file */
void
dnd_open_file_cmd(gchar *cf_names_freeme)
{
    int       err;
    gchar     *cf_name;
    int       in_file_count;
    int       files_work;
    char      **in_filenames;
    char      *tmpname;

    if (cf_names_freeme == NULL) return;

    /* DND_TARGET_URL:
     * The cf_name_freeme is a single string, containing one or more URI's,
     * terminated by CR/NL chars. The length of the whole field can be found
     * in the selection_data->length field. If it contains one file, simply open it,
     * If it contains more than one file, ask to merge these files. */

    /* count the number of input files */
    cf_name = cf_names_freeme;
    for(in_file_count = 0; (cf_name = strstr(cf_name, "\r\n")) != NULL; ) {
        cf_name += 2;
        in_file_count++;
    }
    if (in_file_count == 0) {
      g_free(cf_names_freeme);
      return;
    }

    in_filenames = (char **)g_malloc(sizeof(char*) * in_file_count);

    /* store the starts of the file entries in a gchar array */
    cf_name = cf_names_freeme;
    in_filenames[0] = cf_name;
    for(files_work = 1; (cf_name = strstr(cf_name, "\r\n")) != NULL && files_work < in_file_count; ) {
        cf_name += 2;
        in_filenames[files_work] = cf_name;
        files_work++;
    }

    /* replace trailing CR NL simply with zeroes (in place), so we get valid terminated strings */
    cf_name = cf_names_freeme;
    g_strdelimit(cf_name, "\r\n", '\0');

    /* convert all filenames from URI to local filename (in place) */
    for(files_work = 0; files_work < in_file_count; files_work++) {
        in_filenames[files_work] = dnd_uri2filename(in_filenames[files_work]);
    }

    if (in_file_count == 1) {
        /* open and read the capture file (this will close an existing file) */
        if (cf_open(&cfile, in_filenames[0], FALSE, &err) == CF_OK) {
            /* XXX - add this to the menu if the read fails? */
            cf_read(&cfile, FALSE);
            add_menu_recent_capture_file(in_filenames[0]);
        } else {
            /* the capture file couldn't be read (doesn't exist, file format unknown, ...) */
        }
    } else {
        /* merge the files in chronological order */
        tmpname = NULL;
        if (cf_merge_files(&tmpname, in_file_count, in_filenames,
                           WTAP_FILE_TYPE_SUBTYPE_PCAP, FALSE) == CF_OK) {
            /* Merge succeeded; close the currently-open file and try
               to open the merged capture file. */
            cf_close(&cfile);
            if (cf_open(&cfile, tmpname, TRUE /* temporary file */, &err) == CF_OK) {
                g_free(tmpname);
                cf_read(&cfile, FALSE);
            } else {
                /* The merged file couldn't be read. */
                g_free(tmpname);
            }
        } else {
            /* merge failed */
            g_free(tmpname);
        }
    }

    g_free(in_filenames);
    g_free(cf_names_freeme);
}
示例#2
0
/* open/merge the dnd file */
void
dnd_open_file_cmd(gchar *cf_names_freeme)
{
    int       err;
    gchar     *cf_name;
    int       in_files;
    GString   *dialog_text;
    int       files_work;
    char      **in_filenames;

    if (cf_names_freeme == NULL) return;

    /* DND_TARGET_URL:
     * The cf_name_freeme is a single string, containing one or more URI's,
     * terminated by CR/NL chars. The length of the whole field can be found
     * in the selection_data->length field. If it contains one file, simply open it,
     * If it contains more than one file, ask to merge these files. */

    /* count the number of input files */
    cf_name = cf_names_freeme;
    for(in_files = 0; (cf_name = strstr(cf_name, "\r\n")) != NULL; ) {
        cf_name += 2;
        in_files++;
    }
    if (in_files == 0) {
      g_free(cf_names_freeme);
      return;
    }

    in_filenames = g_malloc(sizeof(char*) * in_files);

    /* store the starts of the file entries in a gchar array */
    cf_name = cf_names_freeme;
    in_filenames[0] = cf_name;
    for(files_work = 1; (cf_name = strstr(cf_name, "\r\n")) != NULL && files_work < in_files; ) {
        cf_name += 2;
        in_filenames[files_work] = cf_name;
        files_work++;
    }

    /* replace trailing CR NL simply with zeroes (in place), so we get valid terminated strings */
    cf_name = cf_names_freeme;
    g_strdelimit(cf_name, "\r\n", '\0');

    /* convert all filenames from URI to local filename (in place) */
    for(files_work = 0; files_work < in_files; files_work++) {
        in_filenames[files_work] = dnd_uri2filename(in_filenames[files_work]);
    }

    if (in_files == 1) {
        /* open and read the capture file (this will close an existing file) */
        if (cf_open(&cfile, in_filenames[0], FALSE, &err) == CF_OK) {
            /* XXX - add this to the menu if the read fails? */
            cf_read(&cfile, FALSE);
            add_menu_recent_capture_file(in_filenames[0]);
        } else {
            /* the capture file couldn't be read (doesn't exist, file format unknown, ...) */
        }
    } else {
        /* build and show the info dialog */
        dialog_text = g_string_sized_new(200);
        g_string_printf(dialog_text, "%sMerging the following files:%s\n\n",
            simple_dialog_primary_start(), simple_dialog_primary_end());
        for(files_work = 0; files_work < in_files; files_work++) {
            g_string_append(dialog_text, in_filenames[files_work]);
            g_string_append(dialog_text, "\n");
        }
        g_string_append(dialog_text, "\nThe packets in these files will be merged chronologically into a new temporary file.");
        simple_dialog(ESD_TYPE_CONFIRMATION, ESD_BTN_OK, "%s",
                      dialog_text->str);
        g_string_free(dialog_text, TRUE);

        /* actually merge the files now */
        dnd_merge_files(in_files, in_filenames);
    }

    g_free(in_filenames);
    g_free(cf_names_freeme);
}