gimme-alpha 0.1

textfile_common.h File Reference

#include <glib.h>
#include <gio/gio.h>
#include "gimme_alpha_types.h"
Include dependency graph for textfile_common.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void prettify_line (char *line, char comment)
GList * get_key_value_pairs (char *s, const char *delim)
GList * read_titled_whitespaced_data (GInputStream *istream, GError **err, GList **titles, char comment, enum data_types t)

Function Documentation

GList* get_key_value_pairs ( char *  s,
const char *  delim 
)

Definition at line 172 of file textfile_common.c.

References pop_n_push_keys(), and split_list_into_sublist().

Referenced by read_nextnano_inputfile_stringy_core().

                                                       {
  GList *l = NULL;
  GList *slist = NULL;
  char *p;
  char *saveptr;
  //fprintf(stderr, "get_key_value_pairs: got string (%s) and delim=(%s)\n", s, delim);
  slist = g_list_append(slist, strtok_r(s, delim, &saveptr));
  while((p=strtok_r(NULL, delim, &saveptr)) != NULL) {
    slist = g_list_append(slist, p);
  }
  g_list_foreach(slist, split_list_into_sublist, &l);
  GList *key = NULL;
  /*
  fprintf(stderr, "\tafter splitting into sublists:\n");
  g_list_foreach(l, print_lists, NULL);
  */
  GList *i;
  for(i = g_list_first(l); i != NULL; i = g_list_next(i)) {
    pop_n_push_keys(&(i->data), &key);
  }
  g_list_last(l)->data = g_list_concat(g_list_first(g_list_last(l)->data), key);
  /*
  fprintf(stderr, "\tafter appending the popped key:\n");
  g_list_foreach(g_list_first(l), print_lists, NULL);
  */
  g_list_free(slist);
  return l;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void prettify_line ( char *  line,
char  comment 
)

Definition at line 36 of file textfile_common.c.

Referenced by read_nextnano_inputfile_stringy_core(), read_nextnano_keyfile2(), and read_titled_whitespaced_data().

                                                     {
  register char *look;
  register char *write;
  register int in_whitespace;
  register int leading_whitespace;
  for(look=write=line, in_whitespace=0, leading_whitespace=1; *look != '\0'; look++) {
#ifdef DEBUG_2
    fprintf(stderr, " look=%d(%c) write=%d(%c) in_whitespace=%d leading_whitespace=%d: ", (look-line), *look, (write-line), *write, in_whitespace, leading_whitespace);
#endif
    if(*look == comment) {
#ifdef DEBUG_2
      fprintf(stderr, "comment\n");
#endif
      *write = '\0';
      break;
    }
    switch(*look) {
    case ' ':
    case '\n':
    case '\r':
    case '\t':
#ifdef DEBUG_2
      fprintf(stderr, "whitespace\n");
#endif
      if((!leading_whitespace) && (!in_whitespace)) *write++ = '\t';
      in_whitespace=1;
      break;
    default:
#ifdef DEBUG_2
      fprintf(stderr, "default\n");
#endif
      *write++ = *look;
      in_whitespace=leading_whitespace=0;
    }
  }
  *write='\0';
  for(look=write=line; *look != '\0'; look++) {
    switch(*look) {
    case ' ':
    case '\n':
    case '\t':
      if(*(look+1) == '\0') {
  *look = '\0';
      }
    }
  }
}

Here is the caller graph for this function:

GList* read_titled_whitespaced_data ( GInputStream *  istream,
GError **  err,
GList **  titles,
char  comment,
enum data_types  t 
)

Definition at line 226 of file textfile_common.c.

References free_if_not_null(), prettify_line(), str, string_to_data(), and unlikely.

Referenced by read_effectivemass_complex_wavefunction2(), read_effectivemass_complex_wavefunctionfile(), read_nextnano_8x8kp_get_eigfunc(), read_nextnano_8x8kp_get_eigvals(), read_nextnano_bandstructure_file(), and read_nextnano_density_el().

                                                                                                                          {
  GDataInputStream *in = g_data_input_stream_new(istream);
  GList *l = NULL, *slist=NULL;
  char *str, *p, *saveptr;
  GList *ct, *cs;
  int got_title=0;
  GHashTable *h=NULL;
  gsize len;
  //void *ptr;

  if(*titles != NULL) {
    got_title=1;
    /*We have to invert the list because it needs to be inverted for
      the later logic. and also because it'll be re-inverted at the end.*/
    *titles = g_list_reverse(*titles);
  }

  /*Note: although ordering will be reversed (for speed) from what's
   * in the file, since both title and row lists are reversed, they
   * work out to be the same.  Ordering in the hash is irrelevant.
   * However, beacuse the user probably wants the titles to be in the
   * same order as in the file, we'll reverse the order of that before
   * returning.
   */
  int i = -1;;
  while((str = g_data_input_stream_read_line(in, &len, NULL, err)) != NULL) {
    //fprintf(stderr, "got line: %s\n", str);
    prettify_line(str, comment);
    if((strlen(str)) == 0) continue;
    slist = g_list_prepend(NULL, strtok_r(str, "\t", &saveptr));
    while((p=strtok_r(NULL, "\t", &saveptr)) != NULL) {
      slist = g_list_prepend(slist, p);
    }
    if(unlikely(!got_title)) {
      //fprintf(stderr, "Got titles\n");
      *titles = slist;
      got_title=1;
    }else{
      i++;
      //fprintf(stderr, "Got row\n");
      h = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_if_not_null);
      //fprintf(stderr, "%d\t", i);
      for(ct=g_list_first(*titles), cs=g_list_first(slist); 
    (ct != NULL) && (cs != NULL); 
    ct = g_list_next(ct), cs=g_list_next(cs)) {
  /*
  ptr = string_to_data(cs->data, t);
  fprintf(stderr, "\tdata=%s\tp=%p\ttitle=%s\n", (char*)cs->data, ptr, (char*)ct->data);
  g_hash_table_replace(h, ct->data, ptr);
  char s[128];
  sprintf(s, "%%s -> %s\t", format_string_for_type_print(t));
  fprintf(stderr, s, cs->data,
  *(double*)string_to_data(cs->data, t));
  */
  g_hash_table_replace(h, ct->data, string_to_data(cs->data, t));
      }
      //fprintf(stderr, "\n");
      l = g_list_prepend(l, h);
      h = NULL;
      //Free the temporary row storage list.
      g_list_free(slist);
      if(unlikely((ct != NULL) || (cs != NULL))) {
  if(ct != NULL)
    fprintf(stderr, "WARNING: there are more titles than columns in this row (%s)!\n", str);
  else
    fprintf(stderr, "WARNING: there are more columns than titles in this row (%s)!\n", str);
      }
    }
  }

  /*Reverse the ordering of rows (they were prepended for performance)*/
  l = g_list_reverse(l);
  /*And also for titles.*/
  *titles = g_list_reverse(*titles);
  return l;
}

Here is the call graph for this function:

Here is the caller graph for this function:

 All Classes Files Functions Variables Enumerations Enumerator Defines