Logo Search packages:      
Sourcecode: ygraph version File versions  Download package

gint dataset_read_from_file ( gchar *  fname,
gint  skip 
)

Open a file and read in a set of data.

The requested file is read into a new DataSet which is appended to the global_data_set_list. The zlib library is used for file I/O, so that gzipped files can be read just as normal files.

Parameters:
fname The name of the file to be read.
skip The number of frames to skip.
Returns:
The index number of the loaded DataSet within the global_data_set_list, or FAIL if the file cannot be read or contains no data.
Note:
The time of the new frame is merged into the global_time_list during the course of this function. Maybe it would be better for global_data_set_list_append() to do this?

Definition at line 466 of file read_data.c.

References check_for_nan(), data_set_free(), data_set_init(), dataset_append_frame(), dataset_set_label(), find_equal(), frame_append_data_point(), frame_free(), frame_init(), frame_set_time(), global_last_frame, global_time_list, label_indicator_set(), message_dialog(), _DataSet::nframes, _Frame::npoints, point_read(), str_to_double(), time_list_build(), and time_list_merge().

Referenced by cmd_line_process(), data_reload_plot(), dataset_read_subtract_files(), dataset_recalc(), and file_select_read().

{
  FILE* fp = NULL;
  gchar* label_indicator_str = NULL;
  gchar* s = NULL;
  gint v = -1;
  gdouble time = 0;
  gdouble x_val = -G_MAXDOUBLE;
  gdouble y_val = -G_MAXDOUBLE;
  DataSet* data_set = NULL;
  Frame* cur_frame = NULL;
  gint label_indicator_len;
  gint time_indicator_len;
  gint data_set_idx;
  gint cur_frame_nbr = 0;
  gint read_mode = READ_COMMENT;
  gint use_pipe = 0;
  gint use_bzip = 0;
  gint use_stdin = 0;

  label_indicator_str = label_indicator_set();
  label_indicator_len = strlen(label_indicator_str);
  time_indicator_len = strlen(TIME_INDICATOR_STRING);

  if (g_strcasecmp(fname, INTERNAL_STDIN_STR)==0)
    use_stdin = 1;
  else
  /* Looking for a pipe */
  if (fname[0] == '<')
    use_pipe = 1;
  else
  {
    /* Do I want to use bzip2 or gzip? */
    g_strreverse(fname);
    if (g_strncasecmp(fname, "2zb.", 4)==0)
      use_bzip=1;
    g_strreverse(fname);
  }
  
  /*
   * Try to open the file, and print an error message if there is a problem.
   */
  if (use_stdin)
  {
    fp = stdin;
  }
  else
    if (use_bzip)
      fp = BZ2_bzopen(fname, "r");
    else
      if (use_pipe)
        fp = popen(fname + 1, "r");
      else
        fp = gzopen(fname, "r");
  
  if (fp == NULL)
    {
      gchar* cant_open_msg = g_strdup_printf("Could not open %s", fname);
      message_dialog(cant_open_msg);
      g_free(cant_open_msg);
      return FAIL;
    }

  data_set = data_set_init(fname);
  cur_frame = frame_init();

  /*
   * Read through each line of the input file.
   */
  s = g_malloc(MAX_LINE_LENGTH);
  while(((use_pipe||use_stdin) ? fgets(s, MAX_LINE_LENGTH, fp) : 
                    use_bzip ? bzgets(fp, s, MAX_LINE_LENGTH):
                               gzgets(fp, s, MAX_LINE_LENGTH)) != NULL)
    {
      g_strstrip(s);

      if ((strchr(COMMENT_DELIMETERS, s[0])) || (s[0] == '\0'))
        {
          if (read_mode == READ_DATA)
            ++cur_frame_nbr;
          read_mode = READ_COMMENT;

          g_strdown(s);

          /*
           * Handle comment lines.
           * Assume frames are delimited by at least one comment line, eg. the
           * time line in cactus files.
           */
          if ((cur_frame->npoints > 0) && ((cur_frame_nbr-1)%skip == 0))
            {
              dataset_append_frame(data_set, cur_frame);
              cur_frame = frame_init();
            }
          /*
           * If the 'comment' line is indicating a time value, store it with
           * the frame.
           */
          if (strchr(COMMENT_DELIMETERS, s[0])
          && (!g_strncasecmp(g_strstrip(s+1), TIME_INDICATOR_STRING,
                         time_indicator_len)))
            {
            v = find_equal(s, strlen(s));
            if (v > -1)
              {
                time=str_to_double((gchar*)g_strstrip(s+v));
                frame_set_time(cur_frame, time);
              }
            }

          /*
           * If the 'comment' line is indicating a label value, store it with
           * the dataset.
           */
          if (strchr(COMMENT_DELIMETERS, s[0]))
            if (!g_strncasecmp(s+1, label_indicator_str, label_indicator_len))
              dataset_set_label(data_set, s+1+label_indicator_len);
        }
      else
        {
          /*
           * Otherwise, read in a data line as an 'x y' pair.
           */
          read_mode = READ_DATA;

          if (!check_for_nan(s) && (cur_frame_nbr%skip == 0))
            {
              point_read(s, &x_val, &y_val);
              frame_append_data_point(cur_frame, x_val, y_val);
            }
        }
    }
  g_free(label_indicator_str);
  g_free(s);

  if (use_pipe)
    pclose(fp);
  else
    if (use_bzip)
      BZ2_bzclose(fp);
    else
      if (!use_stdin)
        gzclose(fp);

  /*
   * Don't forget the last frame ...
   */
  if (cur_frame->npoints > 0)
    dataset_append_frame(data_set, cur_frame);

  /*
   * If no data has been loaded (eg. if the file is an unreadable format),
   * clean up and go home.
   */
  if (data_set->nframes == 0)
    {
      data_set_free(data_set);
      frame_free(cur_frame);

      message_dialog("File contains no data");
      return FAIL;
    }

  /*
   * Add the new data set to the global list.
   */
  data_set_idx = global_data_set_list_append(data_set);

  /*
   * Add the time values contained in the new data set to the global list,
   * and update the number of frames.
   */
  if (global_time_list == NULL)
    time_list_build();
  else
    time_list_merge(data_set);

  global_last_frame = global_time_list->len - 1;

  return data_set_idx;
}


Generated by  Doxygen 1.6.0   Back to index