gimme-alpha 0.1

read_nextnano_inputfile.h File Reference

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

Go to the source code of this file.

Classes

struct  nextnano_keyword
struct  nextnano_keyword_section
struct  nextnano_value

Defines

#define DOMAIN_READ_NEXTNANO_INPUTFILE   1024
#define ERR_NO_START_SECTION   -1
#define ERR_BAD_KEYWORD_LINE   -2

Functions

GHashTable * read_nextnano_inputfile (GInputStream *istream, GError **err)
GHashTable * read_nextnano_inputfile2 (GFile *basedir, GHashTable *keys, int inputfile, GError **err, char **filename)
 reads in a nextnano inputfile, as it should be.
GHashTable * read_nextnano_inputfile_stringy (GInputStream *istream, GError **err)
GHashTable * read_nextnano_inputfile_stringy2 (GFile *basedir, GHashTable *keys, int inputfile, GError **err)
GHashTable * read_nextnano_dbfile (GInputStream *istream, GError **err)
GHashTable * read_nextnano_dbfile2 (GFile *basedir, GError **err)
GHashTable * read_nextnano_keyfile2 (GFile *basedir, GError **err)
 reads in a keywords.in file from an istream
void print_stringy_inputfile (GHashTable *h)
void print_finalized_inputfile (GHashTable *h)
GList * read_nextnano_extract_list_db (GHashTable *input, char *section, int secnum, int chunknum, char *key)
 Returns the list for a specific part of the input file.
GList * read_nextnano_extract_list (GHashTable *input, char *section, int chunknum, char *key, int *err)
GList * read_nextnano_extract_list_typed (GHashTable *input, char *section, int chunknum, char *key, int *err, enum data_types type)
char * read_nextnano_extract_item (GHashTable *input, char *section, int chunknum, char *key, int *err)
void * read_nextnano_extract_item_typed (GHashTable *input, char *section, int chunknum, char *key, int *err, enum data_types type)

Define Documentation

#define DOMAIN_READ_NEXTNANO_INPUTFILE   1024

Definition at line 35 of file read_nextnano_inputfile.h.

Referenced by read_nextnano_keyfile2().

#define ERR_BAD_KEYWORD_LINE   -2

Definition at line 39 of file read_nextnano_inputfile.h.

Referenced by read_nextnano_keyfile2().

#define ERR_NO_START_SECTION   -1

error codes

Definition at line 38 of file read_nextnano_inputfile.h.

Referenced by read_nextnano_keyfile2().


Function Documentation

void print_finalized_inputfile ( GHashTable *  h)

Definition at line 305 of file read_nextnano_inputfile.c.

References print_section().

                                              {
  int stringy = 0;
  g_hash_table_foreach(h, print_section, &stringy);
}

Here is the call graph for this function:

void print_stringy_inputfile ( GHashTable *  h)

Definition at line 300 of file read_nextnano_inputfile.c.

References print_section().

                                            {
  int stringy = 1;
  g_hash_table_foreach(h, print_section, &stringy);
}

Here is the call graph for this function:

GHashTable* read_nextnano_dbfile ( GInputStream *  istream,
GError **  err 
)

Definition at line 310 of file read_nextnano_inputfile.c.

References read_nextnano_inputfile_stringy_core().

Referenced by main(), and read_nextnano_dbfile2().

                                                                      {
  GHashTable *ht = read_nextnano_inputfile_stringy_core(istream, err);
  if(ht == NULL) return ht;
  /*Convert the strings to numbers here.*/
  return ht;
}

Here is the call graph for this function:

Here is the caller graph for this function:

GHashTable* read_nextnano_dbfile2 ( GFile *  basedir,
GError **  err 
)

Definition at line 641 of file read_nextnano_inputfile.c.

References read_nextnano_dbfile().

Referenced by main().

                                                                {
  GFile* dbfile = g_file_get_child(basedir, "database.in");
  GFileInputStream *dbstream = g_file_read(dbfile, NULL, err);
  if(*err != NULL) return NULL;
  GHashTable *ht = read_nextnano_dbfile(G_INPUT_STREAM(dbstream), err);
  GError *e = NULL;
  g_input_stream_close(G_INPUT_STREAM(dbstream), NULL, &e);
  /*If we fail to close the stream, we almost certainly have bigger
   *issues to worry about! Let the user know of *those* errors, not
   *the close failure*/
  if(e != NULL) g_error_free(e);
  g_object_unref(dbfile);
  return ht;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* read_nextnano_extract_item ( GHashTable *  input,
char *  section,
int  chunknum,
char *  key,
int *  err 
)

Definition at line 431 of file read_nextnano_inputfile.c.

References read_nextnano_extract_list().

Referenced by read_effectivemass_complex_wavefunction2(), read_nextnano_8x8kp_get_eigfunc(), read_nextnano_8x8kp_get_eigvals(), read_nextnano_density_el(), and read_nextnano_extract_item_typed().

                                                                                                      {
  GList *values = read_nextnano_extract_list(input, section, chunknum, key, err);
  if(values == NULL) {
    return NULL;
  }
  if(g_list_length(g_list_first(values)) != 1) {
    *err = 20;
    return NULL;
  }
  *err = 0;
  return g_list_first(values)->data;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* read_nextnano_extract_item_typed ( GHashTable *  input,
char *  section,
int  chunknum,
char *  key,
int *  err,
enum data_types  type 
)

Definition at line 449 of file read_nextnano_inputfile.c.

References read_nextnano_extract_item(), string_to_data(), and unlikely.

Referenced by get_nanostructure().

                                                                                                                                  {
  char *c;
  if(unlikely((c=read_nextnano_extract_item(input, section, chunknum, key, err)) == NULL)) {
    return NULL;
  }
  void *v = string_to_data(c, type);
  if(unlikely(v == NULL)) {
    *err = 10;
  }
  return v;
}

Here is the call graph for this function:

Here is the caller graph for this function:

GList* read_nextnano_extract_list ( GHashTable *  input,
char *  section,
int  chunknum,
char *  key,
int *  err 
)

Definition at line 379 of file read_nextnano_inputfile.c.

Referenced by read_nextnano_extract_item(), and read_nextnano_extract_list_typed().

                                                                                                       {
  GList *chunk = g_hash_table_lookup(input, section);
  if(chunk == NULL) {
    //fprintf(stderr, "chunk was NULL\n");
    *err = 1;
    return NULL;
  }
  GHashTable *kv_set = g_list_nth_data(g_list_first(chunk), chunknum);
  if(kv_set == NULL) {
    //fprintf(stderr, "kv_set was NULL\n");
    *err = 2;
    return NULL;
  }
  //if(g_hash_table_lookup(kv_set, key) == NULL) {fprintf(stderr, "Unable to find key %s in chunk.\n", key);}
  /*g_hash_table_lookup returns NULL if not found, which is a valid
   * 0-length list. */
  *err = 0;
  return g_list_first(g_hash_table_lookup(kv_set, key));
}

Here is the caller graph for this function:

GList* read_nextnano_extract_list_db ( GHashTable *  input,
char *  section,
int  secnum,
int  chunknum,
char *  key 
)

Returns the list for a specific part of the input file.

Parameters:
sectionname of the section you want
secnumThere may be multiple sections that match the name. This specifies which one you want (in order of occurrance)
chunknumThere may be multiple chunks of key/value lists within each section. This specifies which which one you want (in order of occurrance) (NOTE: first chunk is chunk 0)
keySpecifies the key within the section.
Returns:
The list associated with the requested key. NULL if the key wasn't found.

Definition at line 366 of file read_nextnano_inputfile.c.

                                                                                                            {
  return NULL;
}
GList* read_nextnano_extract_list_typed ( GHashTable *  input,
char *  section,
int  chunknum,
char *  key,
int *  err,
enum data_types  type 
)

Definition at line 403 of file read_nextnano_inputfile.c.

References read_nextnano_extract_list(), string_to_data(), and unlikely.

                                                                                                                                   {
  GList *c;
  GList *l = NULL;
  if(unlikely((c=read_nextnano_extract_list(input, section, chunknum, key, err)) == NULL)) {
    return NULL;
  }
  void *v;
  for(l = c; l != NULL; l = g_list_next(l)) {
    v = string_to_data(c->data, type);
    if(unlikely(v == NULL)) {
      *err = 10;
    }
    l->data = v;
  }
  return c;
}

Here is the call graph for this function:

GHashTable* read_nextnano_inputfile ( GInputStream *  istream,
GError **  err 
)
Deprecated:

Definition at line 114 of file read_nextnano_inputfile.c.

References read_nextnano_inputfile_stringy().

Referenced by read_nextnano_inputfile2().

                                                                         {
  GHashTable *h = read_nextnano_inputfile_stringy(istream, err);
  if((h == NULL) || (*err != NULL)) return NULL;
  fprintf(stderr, "WARNING: read_nextnano_inputfile: datatype conversion not yet implemented.  Returning stringy data.\n");
  return h;
}

Here is the call graph for this function:

Here is the caller graph for this function:

GHashTable* read_nextnano_inputfile2 ( GFile *  basedir,
GHashTable *  keys,
int  inputfile,
GError **  err,
char **  filename 
)

reads in a nextnano inputfile, as it should be.

Parameters:
basedirpointer to GFile with the project base directory
keysptr to GHashTable containing the processed keywords file
inputfileinteger, "sequence number" of the input file to postprocess
errptr to ptr to GError for file read errors
Returns:
GHashTable containing the fully postprocessed input file
Attention:
use this version; the original (GInputStream) version is deprecated.

Definition at line 601 of file read_nextnano_inputfile.c.

References keyword_sequence_search_foreach_info::found, keyword_sequence_search_foreach_callback(), read_nextnano_inputfile(), keyword_sequence_search_foreach_info::sought, and unlikely.

Referenced by main().

                                                                                                                     {
  *filename = NULL;
  if(unlikely((keys == NULL) || (basedir == NULL) || (err == NULL))) return NULL;
  struct keyword_sequence_search_foreach_info kssfi = {.sought = inputfile, .found = NULL};
  g_hash_table_foreach(g_hash_table_lookup(keys, "input_filename"), keyword_sequence_search_foreach_callback, &kssfi);
  if(kssfi.found == NULL) return NULL;
  GFile* infile = g_file_get_child(basedir, kssfi.found);
  GFileInputStream *istream = g_file_read(infile, NULL, err);
  if(*err != NULL) return NULL;
  GHashTable *ht = read_nextnano_inputfile(G_INPUT_STREAM(istream), err);
  GError *e = NULL;
  g_input_stream_close(G_INPUT_STREAM(istream), NULL, &e);
  /*If we fail to close the stream, we almost certainly have bigger
   *issues to worry about! Let the user know of *those* errors, not
   *the close failure*/
  if(e != NULL) g_error_free(e);
  g_object_unref(infile);
  *filename = kssfi.found;
  return ht;
}

Here is the call graph for this function:

Here is the caller graph for this function:

GHashTable* read_nextnano_inputfile_stringy ( GInputStream *  istream,
GError **  err 
)
Deprecated:

Definition at line 325 of file read_nextnano_inputfile.c.

References read_nextnano_inputfile_stringy_core(), stick_this_in_the_hash(), and unlikely.

Referenced by main(), read_nextnano_inputfile(), and read_nextnano_inputfile_stringy2().

                                                                                 {
  GHashTable *ht = read_nextnano_inputfile_stringy_core(istream, err);
  GHashTableIter hit;
  g_hash_table_iter_init(&hit, ht);
  gpointer key, value;
  GHashTable *tempt = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL);
  while(g_hash_table_iter_next(&hit, &key, &value)) {
    if(unlikely(g_list_length(((GList*) value)) != 1)) {
      if(unlikely(g_list_length(((GList*) value)) < 1)) {
  fprintf(stderr, "WARNING: PROGRAMMING ERROR: the list for section %s was empty!  This should never happen!\n", (char*)key);
  continue;
      }
      fprintf(stderr, "WARNING: there are duplicate definitions of section %s.  Will be assuming the last definition of the section.\n", (char*)key);
    }
    g_hash_table_iter_steal(&hit);
    g_hash_table_replace(tempt, key, (g_list_last((GList*)value))->data);
      /*Free the list structures for the top-level list.  They're no
      longer needed.*/
    g_list_free((GList*)value);
    //fprintf(stderr, "Raised chunklist to top-level for %s\n", (char*)key);
  }
  g_hash_table_foreach(tempt, stick_this_in_the_hash, ht);
  g_hash_table_steal_all(tempt);
  return ht;
}

Here is the call graph for this function:

Here is the caller graph for this function:

GHashTable* read_nextnano_inputfile_stringy2 ( GFile *  basedir,
GHashTable *  keys,
int  inputfile,
GError **  err 
)
Attention:
Use this variant; the normal version is DEPRECATED

Definition at line 623 of file read_nextnano_inputfile.c.

References keyword_sequence_search_foreach_info::found, keyword_sequence_search_foreach_callback(), read_nextnano_inputfile_stringy(), keyword_sequence_search_foreach_info::sought, and unlikely.

                                                                                                            {
  if(unlikely((keys == NULL) || (basedir == NULL) || (err == NULL))) return NULL;
  struct keyword_sequence_search_foreach_info kssfi = {.sought = inputfile, .found = NULL};
  g_hash_table_foreach(keys, keyword_sequence_search_foreach_callback, &kssfi);
  if(kssfi.found == NULL) return NULL;
  GFile* infile = g_file_get_child(basedir, kssfi.found);
  GFileInputStream *istream = g_file_read(infile, NULL, err);
  if(*err != NULL) return NULL;
  GHashTable *ht = read_nextnano_inputfile_stringy(G_INPUT_STREAM(istream), err);
  GError *e = NULL;
  g_input_stream_close(G_INPUT_STREAM(istream), NULL, &e);
  /*If we fail to close the stream, we almost certainly have bigger
   *issues to worry about! Let the user know of *those* errors, not
   *the close failure*/
  if(e != NULL) g_error_free(e);
  g_object_unref(infile);
  return ht;
}

Here is the call graph for this function:

GHashTable* read_nextnano_keyfile2 ( GFile *  basedir,
GError **  err 
)

reads in a keywords.in file from an istream

Parameters:
basedirdirectory in which the project is based
GErrorpointer to a pointer to use to store the last i/o error.
Returns:
a GHashTable containing the sections of the inputfile.

Definition at line 661 of file read_nextnano_inputfile.c.

References destroy_c_string(), destroy_keyword_section(), DOMAIN_READ_NEXTNANO_INPUTFILE, ERR_BAD_KEYWORD_LINE, ERR_NO_START_SECTION, free_esec(), get_keyword_from_line(), get_keyword_section_from_line(), get_section_end(), is_section(), nextnano_keyword_section::keywords, prettify_line(), sections, str, and unlikely.

Referenced by main().

                                                                 {
  GFile* keyfile = g_file_get_child(basedir, "keywords.in");
  GFileInputStream *keystream = g_file_read(keyfile, NULL, err);
  if(*err != NULL) return NULL;
  GDataInputStream *in = g_data_input_stream_new((GInputStream*)keystream);
  GHashTable *sections = g_hash_table_new_full(g_str_hash, g_str_equal, destroy_c_string, destroy_keyword_section);
  if(unlikely(sections == NULL)) return NULL;
  char *str;
  char *sname = NULL;
  gsize len;
  char *sec;
  char *esec;
  char *key;
  GList *def = NULL;
  int n;
  int goodclose;
  /*Current chunk*/
  char *saveptr;
  /*Current value list.*/
  GList *val = NULL;
  GList *tmp = NULL;
  int seqnum = 1;
  struct nextnano_keyword_section *section = NULL;
  struct nextnano_keyword *keyword;
  while((str = g_data_input_stream_read_line(in, &len, NULL, err)) != NULL) {
    prettify_line(str, '!');
    if((strlen(str)) == 0) continue;
    //fprintf(stderr, "read_nextnano_inputfile: got line (%s)\n", str);
    if((sec = is_section(str)) == NULL) {
      if(section == NULL) {
  *err = g_error_new_literal(DOMAIN_READ_NEXTNANO_INPUTFILE, ERR_NO_START_SECTION, "read_nextnano_inputfile.c::read_nextnano_keyfile2: Error reading keywords.in: there was no section given for these keywords!");
  g_hash_table_destroy(sections);
  return NULL;
      }
      keyword = get_keyword_from_line(str, &key, seqnum++);
      if(keyword == NULL) {
  /*Note that the seqnum won't get decrement if we fail to parse
   *the line; it's still a line, valid or not*/
  *err = g_error_new(DOMAIN_READ_NEXTNANO_INPUTFILE, ERR_BAD_KEYWORD_LINE, "read_nextnano_inputfile.c::read_nextnano_keyfile2: Error reading keywords.in: failed to parse malformed keyword line (%s)", str);
  g_hash_table_destroy(sections);
  return NULL;
      }
      g_hash_table_replace(section->keywords, key, keyword);
    }else{
      esec=get_section_end(str);
      if((esec != NULL) || ((sec != NULL) && (sname != NULL))) {
  if(esec != NULL){
    char *tesec;
    struct nextnano_keyword_section *tmps = get_keyword_section_from_line(esec, &tesec);
    g_hash_table_destroy(tmps->keywords);
    free(tmps);
    if(sname == NULL) {
      g_warning("read_nextnano_inputfile.c::read_nextnano_keyfile2: got a request to end section (%s) but there was no section currently open!\n", esec);
      free(tesec);
      continue;
    }else if(strcmp(tesec, sname) != 0) {
      g_warning("read_nextnano_inputfile.c::read_nextnano_keyfile2: got a request to end section (%s) but a different section is currently open (%s)!\n", esec, sname);
    }     
    free(tesec);
  }else{
    g_warning("read_nextnano_inputfile.c::read_nextnano_keyfile2: got a request to start a new section (%s) but a section is still open (%s)!\n", sec, sname);
  }
  /*Now close the section*/
  //fprintf(stderr, "\tinserting %s(%p) into hash table %p\n", sname, (void*)s, (void*)sections);
  g_hash_table_replace(sections, sname, section);
  sname = NULL;
      }else{
  /*New section*/
  if(unlikely((section = get_keyword_section_from_line(sec, &sname)) == NULL)) {
    g_warning("read_nextnano_inputfile.c::read_nextnano_keyfile2: failed to parse section name (%s)!", sec);
    /*Sequence numbers start at 1.*/
  }
  seqnum=1;
      }
      if(esec != NULL) free_esec(esec);
      esec = NULL;
      sec=NULL;
      seqnum=1;
    }
  }

  if(sname != NULL) {
    g_warning("read_nextnano_inputfile.c::read_nextnano_keyfile2: section (%s) wasn't ended before the end of the file.\n", sname);
    g_hash_table_replace(sections, sname, section);
    sname = NULL;
  }
  GError *e = NULL;
  g_input_stream_close(G_INPUT_STREAM(keystream), NULL, &e);
  /*If we fail to close the stream, we almost certainly have bigger
   *issues to worry about! Let the user know of *those* errors, not
   *the close failure*/
  if(e != NULL) 
    g_error_free(e);
  g_object_unref(in);
  return sections;
}

Here is the call graph for this function:

Here is the caller graph for this function:

 All Classes Files Functions Variables Enumerations Enumerator Defines