gimme-alpha 0.1

read_nextnano_inputfile.c File Reference

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libcommon/gcc_hints.h>
#include <libnextnano/read_nextnano_inputfile.h>
#include <libcommon/textfile_common.h>
Include dependency graph for read_nextnano_inputfile.c:

Go to the source code of this file.

Classes

struct  keyword_sequence_search_foreach_info
 struct for the keyword_sequence_search_foreach_callback More...

Defines

#define DEFAULT_SECTION   "default"

Functions

static void destroy_chunklist (gpointer data, gpointer user_data)
static void destroy_section (gpointer data)
static void destroy_secname (gpointer data)
static void free_nothing (gpointer data)
static void free_esec (char *esec)
static void destroy_value (gpointer data, gpointer user_data)
static void destroy_values (gpointer data)
static void destroy_keyword_section (gpointer data)
static void destroy_keyword (gpointer data)
static void destroy_c_string (gpointer data)
___always_inline char * is_section (char *str)
__always_inline char * get_section_end (char *str)
static int starts_new_chunk (const char *secname, GHashTable *chunk, const char *key)
GHashTable * read_nextnano_inputfile (GInputStream *istream, GError **err)
static void print_key_value_item (gpointer data, gpointer userdata)
static void print_key_value_lists (gpointer data, gpointer userdata)
GHashTable * read_nextnano_inputfile_stringy_core (GInputStream *istream, GError **err)
 this is the core function which processes a nextnano input file.
static void print_valueitem (gpointer data, gpointer userdata)
static void print_value (gpointer key, gpointer data, gpointer userdata)
static void print_chunk (gpointer data, gpointer userdata)
static void print_section (gpointer key, gpointer value, gpointer userdata)
void print_stringy_inputfile (GHashTable *h)
void print_finalized_inputfile (GHashTable *h)
GHashTable * read_nextnano_dbfile (GInputStream *istream, GError **err)
static void stick_this_in_the_hash (gpointer key, gpointer data, gpointer hash)
GHashTable * read_nextnano_inputfile_stringy (GInputStream *istream, GError **err)
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)
enum data_types get_type_from_string (const char *s)
 gets the type from a string specifying the type.
short int is_required (const char *s)
 determines if a keyword or section is required or optional.
struct nextnano_keywordget_keyword_from_line (const char *line, char **key, int seqnum)
 processes a line which contains a keyword, and outputs a keyword structure and key name.
struct nextnano_keyword_sectionget_keyword_section_from_line (const char *line, char **secname)
 processes a line which contains the start of a keyword section, and outputs a keyword structure and key name.
void keyword_sequence_search_foreach_callback (gpointer key, gpointer value, gpointer data)
 Static helper function for.
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_stringy2 (GFile *basedir, GHashTable *keys, int inputfile, 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

Define Documentation

#define DEFAULT_SECTION   "default"

Definition at line 34 of file read_nextnano_inputfile.c.

Referenced by read_nextnano_inputfile_stringy_core().


Function Documentation

static void destroy_c_string ( gpointer  data) [static]

Definition at line 73 of file read_nextnano_inputfile.c.

Referenced by get_keyword_section_from_line(), and read_nextnano_keyfile2().

                                            {
  free((char*)data);
}

Here is the caller graph for this function:

static void destroy_chunklist ( gpointer  data,
gpointer  user_data 
) [static]

Definition at line 35 of file read_nextnano_inputfile.c.

Referenced by destroy_section().

                                                                 {
  //fprintf(stderr, "destroying chunklist\n");
  g_hash_table_destroy(data);
}

Here is the caller graph for this function:

static void destroy_keyword ( gpointer  data) [static]

Definition at line 70 of file read_nextnano_inputfile.c.

Referenced by get_keyword_section_from_line().

                                           {
  free((struct nextnano_keyword*)data);
}

Here is the caller graph for this function:

static void destroy_keyword_section ( gpointer  data) [static]

Definition at line 66 of file read_nextnano_inputfile.c.

Referenced by read_nextnano_keyfile2().

                                                   {
  g_hash_table_destroy(((struct nextnano_keyword_section*)data)->keywords);
  free((struct nextnano_keyword_section*)data);
}

Here is the caller graph for this function:

static void destroy_secname ( gpointer  data) [static]

Definition at line 43 of file read_nextnano_inputfile.c.

Referenced by read_nextnano_inputfile_stringy_core().

                                           {
  //fprintf(stderr, "destroying section name (%p)(%s0\n", data, (char*)data);
  /*Free the *line*, not just the string*/
  /*Casting to char* so that we make sure the right size is counted!
   *Thanks, little gcc pedant!*/
  free(((char*)data)-1);
}

Here is the caller graph for this function:

static void destroy_section ( gpointer  data) [static]

Definition at line 39 of file read_nextnano_inputfile.c.

References destroy_chunklist().

Referenced by read_nextnano_inputfile_stringy_core().

                                           {
  //fprintf(stderr, "destroying section (%p)\n", data);
  g_list_foreach(data, destroy_chunklist, NULL);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void destroy_value ( gpointer  data,
gpointer  user_data 
) [static]

Definition at line 58 of file read_nextnano_inputfile.c.

Referenced by destroy_values().

                                                             {
  //fprintf(stderr, "destroying value\n");
  //free(data);
}

Here is the caller graph for this function:

static void destroy_values ( gpointer  data) [static]

Definition at line 62 of file read_nextnano_inputfile.c.

References destroy_value().

Referenced by read_nextnano_inputfile_stringy_core().

                                          {
  //fprintf(stderr, "destroying values\n");
  g_list_foreach(data, destroy_value, NULL);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void free_esec ( char *  esec) [static]

Definition at line 53 of file read_nextnano_inputfile.c.

Referenced by read_nextnano_inputfile_stringy_core(), and read_nextnano_keyfile2().

                                  {
  /*free the *line*, not just the string.*/
  //fprintf(stderr, "free_esec: Freeing line, not just char.\n");
  free(esec-5);
}

Here is the caller graph for this function:

static void free_nothing ( gpointer  data) [static]

Definition at line 50 of file read_nextnano_inputfile.c.

                                        {
  //fprintf(stderr, "freeing nothing\n");
}
struct nextnano_keyword* get_keyword_from_line ( const char *  line,
char **  key,
int  seqnum 
) [read]

processes a line which contains a keyword, and outputs a keyword structure and key name.

Parameters:
lineline of input
keypointer to a pointer to a string to hold the key name
Returns:
pointer to struct nextnano_keyword which has the keyword information.

Definition at line 521 of file read_nextnano_inputfile.c.

References get_type_from_string(), is_required(), nextnano_keyword::required, nextnano_keyword::seqnum, nextnano_keyword::t, and unlikely.

Referenced by read_nextnano_keyfile2().

                                                                                         {
  struct nextnano_keyword* keyword = malloc(sizeof(struct nextnano_keyword));
  *key = NULL;
  if(unlikely(keyword == NULL)) return NULL;
  gchar **set = g_strsplit_set(line, " \t", 4);
  if(unlikely(set[0] == NULL)) {free(keyword); return NULL;}
  if(unlikely(set[1] == NULL)) {free(keyword); free(set[0]); return NULL;}
  if(unlikely(set[2] == NULL)) {free(keyword); free(set[0]); free(set[1]); return NULL;}
  /*if we have 4 sections, we have too many!*/
  if(unlikely(set[3] != NULL)) {free(keyword); free(set[0]); free(set[1]); free(set[2]); free(set[3]); return NULL;}
  keyword->t = get_type_from_string(set[1]);
  if(unlikely((keyword->required = is_required(set[2])) == 2)) {
    free(keyword);
    free(set[0]);
    free(set[1]);
    free(set[2]);
    free(set[3]);
    return NULL;
  }
  *key = set[0];
  keyword->seqnum = seqnum;
  return keyword;
}

Here is the call graph for this function:

Here is the caller graph for this function:

struct nextnano_keyword_section* get_keyword_section_from_line ( const char *  line,
char **  secname 
) [read]

processes a line which contains the start of a keyword section, and outputs a keyword structure and key name.

Parameters:
lineline of input
secnamepointer to a pointer to a string to hold the key name
Returns:
pointer to struct nextnano_keyword_section which has the keyword information.

Definition at line 551 of file read_nextnano_inputfile.c.

References destroy_c_string(), destroy_keyword(), is_required(), nextnano_keyword_section::keywords, nextnano_keyword_section::required, and unlikely.

Referenced by read_nextnano_keyfile2().

                                                                                                 {
  struct nextnano_keyword_section* keyword_section = malloc(sizeof(struct nextnano_keyword_section));
  *secname = NULL;
  if(unlikely(keyword_section == NULL)) return NULL;
  gchar **set = g_strsplit_set(line, " \t", 3);
  //fprintf(stderr, "set[0]=%s set[1]=%s set[2]=%s\n", (set[0]==NULL)?"(null)":set[0], (set[1]==NULL)?"(null)":set[1], (set[2]==NULL)?"(null)":set[2]);
  if(unlikely(set[0] == NULL)) {free(keyword_section); return NULL;}
  if(unlikely(set[1] == NULL)) {free(keyword_section); free(set[0]); return NULL;}
  /*if we have 3 sections, we have too many!*/
  if(unlikely(set[2] != NULL)) {free(keyword_section); free(set[0]); free(set[1]); free(set[2]); return NULL;}
  if(unlikely((keyword_section->required = is_required(set[1])) == 2)) {
    free(keyword_section);
    free(set[0]);
    free(set[1]);
    return NULL;
  }
  /*We're done with the optional/required keyword*/
  free(set[1]);
  keyword_section->keywords = g_hash_table_new_full(g_str_hash, g_str_equal, destroy_c_string, destroy_keyword);
  *secname = set[0];
  return keyword_section;
}

Here is the call graph for this function:

Here is the caller graph for this function:

__always_inline char* get_section_end ( char *  str)

Definition at line 89 of file read_nextnano_inputfile.c.

Referenced by read_nextnano_inputfile_stringy_core(), and read_nextnano_keyfile2().

                                                 {
  if(strncmp(str, "$end_", 5) == 0)
    return &(str[5]);
  else
    return NULL;
}

Here is the caller graph for this function:

enum data_types get_type_from_string ( const char *  s)

gets the type from a string specifying the type.

Parameters:
sstring to convert to a type
Returns:
enum data_types representing the type.(see gimme_alpha_types.h)
Warning:
assumes s to be null-terminated.
Note:
grep -v '^ *\!' keywords.in | grep -v '^ *$' | sed 's/ *!.*$//g' | sed 's/ +/ /g' | grep -v '^$' | sed 's/_array//g' | awk '{print $2;}' | sort | uniq -c | less 13 232 character 654 double 195 integer 1 logical default is to be a string type, i.e. unconverted.

Definition at line 475 of file read_nextnano_inputfile.c.

References BOOLEAN_TYPE, DOUBLE_TYPE, INT_TYPE, and STRING_TYPE.

Referenced by get_keyword_from_line().

                                                    {
  if(!(strcasecmp(s, "double"))) {
    return DOUBLE_TYPE;
  }else if(!(strcasecmp(s, "double_array"))) {
    return DOUBLE_TYPE;
  }else if(!(strcasecmp(s, "character"))) {
    return STRING_TYPE;
  }else if(!(strcasecmp(s, "integer"))) {
    return INT_TYPE;
  }else if(!(strcasecmp(s, "integer_array"))) {
    return INT_TYPE;
  }else if(!(strcasecmp(s, "logical"))) {
    return BOOLEAN_TYPE;
  }else if(!(strcasecmp(s, "logical_array"))) {
    return BOOLEAN_TYPE;
  }else{
    g_warning("didn't recognize type %s; assuming string to preserve the information", s);
    return STRING_TYPE;
  }
}

Here is the caller graph for this function:

short int is_required ( const char *  s)

determines if a keyword or section is required or optional.

Parameters:
sstring containing either "optional" or "required"
Returns:
0 if optional, 1 if required, 2 if uncertain what was meant.
Note:
13 955 optional 127 required

Definition at line 505 of file read_nextnano_inputfile.c.

Referenced by get_keyword_from_line(), and get_keyword_section_from_line().

                                     {
  if(!(strcasecmp(s, "optional"))) {
    return 0;
  }else if(!(strcasecmp(s, "required"))) {
    return 1;
  }
  g_warning("read_nextnano_inputfile.c::is_required: got unknown requirement string (%s)", s);
  return 2;
}

Here is the caller graph for this function:

___always_inline char* is_section ( char *  str)

Definition at line 81 of file read_nextnano_inputfile.c.

Referenced by read_nextnano_inputfile_stringy_core(), and read_nextnano_keyfile2().

                                             {
  if (str[0] == '$') {
    return &(str[1]);
  }else{
    return NULL;
  }
}

Here is the caller graph for this function:

void keyword_sequence_search_foreach_callback ( gpointer  key,
gpointer  value,
gpointer  data 
)

Static helper function for.

Definition at line 582 of file read_nextnano_inputfile.c.

References keyword_sequence_search_foreach_info::sought.

Referenced by read_nextnano_inputfile2(), and read_nextnano_inputfile_stringy2().

                                                                                           {
  /*wordy, but straightforward; if seqnum = sought, found = key)*/
  if((((struct nextnano_keyword*)value)->seqnum) == (((struct keyword_sequence_search_foreach_info*)data)->sought)) {
      (((struct keyword_sequence_search_foreach_info*)data)->found) = key;
    fprintf(stderr, "Y: key=%s seqnum=%d sought=%d\n", (char*)key, ((struct nextnano_keyword*)value)->seqnum, (((struct keyword_sequence_search_foreach_info*)data)->sought));
  }else{
    fprintf(stderr, "N: key=%s seqnum=%d sought=%d\n", (char*)key, ((struct nextnano_keyword*)value)->seqnum, (((struct keyword_sequence_search_foreach_info*)data)->sought));
  }
}

Here is the caller graph for this function:

static void print_chunk ( gpointer  data,
gpointer  userdata 
) [static]

Definition at line 288 of file read_nextnano_inputfile.c.

References print_value().

Referenced by print_section().

                                                         {
  printf("print_chunk: data=%p userdata=%p\n", data, userdata);
  g_hash_table_foreach(data, print_value, userdata);
  printf("\n");
}

Here is the call graph for this function:

Here is the caller graph for this function:

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:

static void print_key_value_item ( gpointer  data,
gpointer  userdata 
) [static]

Definition at line 121 of file read_nextnano_inputfile.c.

Referenced by print_key_value_lists().

                                                                   {
  fprintf(stderr, "\t%s", (char*)data);
}

Here is the caller graph for this function:

static void print_key_value_lists ( gpointer  data,
gpointer  userdata 
) [static]

Definition at line 125 of file read_nextnano_inputfile.c.

References print_key_value_item().

                                                                    {
  g_list_foreach(data, print_key_value_item, userdata);
  fprintf(stderr, "\n");
}

Here is the call graph for this function:

static void print_section ( gpointer  key,
gpointer  value,
gpointer  userdata 
) [static]

Definition at line 294 of file read_nextnano_inputfile.c.

References print_chunk().

Referenced by print_finalized_inputfile(), and print_stringy_inputfile().

                                                                          {
  fprintf(stderr, "print_section: key=%p value=%p userdata=%p\n", key, value, userdata);
  printf("[%s]\n", (char*)key);
  g_list_foreach(value, print_chunk, userdata);
}

Here is the call graph for this function:

Here is the caller 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:

static void print_value ( gpointer  key,
gpointer  data,
gpointer  userdata 
) [static]

Definition at line 282 of file read_nextnano_inputfile.c.

References print_valueitem().

Referenced by print_chunk().

                                                                        {
  printf("print_value: key=%p data=%p userdata=%p\n", key, data, userdata);
  printf("\t(%s)\n", (char*)key);
  g_list_foreach(data, print_valueitem, userdata);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void print_valueitem ( gpointer  data,
gpointer  userdata 
) [static]

Definition at line 264 of file read_nextnano_inputfile.c.

Referenced by print_value().

                                                              {
  printf("print_valueitem: data=%p userdata=%p\n", data, userdata);
  if(*((int*)userdata)) {
    printf("\t\t%s\n", (char*)data);
  }else{
    char *format;
    switch(((struct nextnano_value*)data)->t) {
    INT: format="\t\t%d\n"; break;
    DOUBLE: format="\t\t%g\n"; break;
    STRING: format="\t\t%s\n"; break;
    default:
      fprintf(stderr, "PROGRMMING ERROR: unable to recognize value type (%d).  Skipping.\n", ((struct nextnano_value*)data)->t);
      return;
    };
    printf(format, ((struct nextnano_value*)data)->store);
  }
}

Here is the caller 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_inputfile_stringy_core ( GInputStream *  istream,
GError **  err 
)

this is the core function which processes a nextnano input file.

Parameters:
istreampointer to a GInputStream to read from
GErrorpointer to a GError to store the last error
Returns:
GHashTable containing the (stringy) processed input file.

Definition at line 137 of file read_nextnano_inputfile.c.

References DEFAULT_SECTION, destroy_secname(), destroy_section(), destroy_values(), free_esec(), get_key_value_pairs(), get_section_end(), is_section(), prettify_line(), sections, starts_new_chunk(), str, and unlikely.

Referenced by read_nextnano_dbfile(), and read_nextnano_inputfile_stringy().

                                                                                      {
  GDataInputStream *in = g_data_input_stream_new(istream);
  GHashTable *sections = g_hash_table_new_full(g_str_hash, g_str_equal, destroy_secname, destroy_section);
  if(unlikely(sections == NULL)) return NULL;
  char *str;
  char *sname = NULL;
  gsize len;
  char *sec;
  char *esec;
  char *key;
  char *value;
  GList *def = NULL;
  int n;
  int goodclose;
  /*Current section*/
  GList *s = NULL;
  /*Current chunk*/
  GHashTable *chunk = NULL;
  char *saveptr;
  /*Current value list.*/
  GList *val = NULL;
  GList *tmp = NULL;
  
  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) {
      //fprintf(stderr, "*is not a section(%s)\n", str);
      /*Not the beginning or end of a new section.*/
      GList *v = get_key_value_pairs(str, "=");
      /*
      fprintf(stderr, "key/value list for %s:\n", str);
      g_list_foreach(v, print_key_value_lists, NULL);
      printf("\n");
      */
      if(v == NULL) continue;
      GList *c;
      if((g_list_first(v)->data != NULL)) {
  //fprintf(stderr, "APPENDING APPENDED DATA\n");
  /*This ought to preserve the head of val.*/
  val = g_list_concat(val, g_list_first(v)->data);
      }
      /*Go through the list of key/value sets and add them to the list.*/
      for(c = g_list_next(v); c != NULL; c = g_list_next(c)) {
  GList *sl = c->data;
  GList *kl = g_list_first(sl);
  /*
  fprintf(stderr, "\tsl(%p)=%s\n", sl, (char*)((sl == NULL)?NULL:sl->data));
  fprintf(stderr, "*is a new value/key pair: ");
  */
  key = kl->data;
  //fprintf(stderr, "\tsl=%p\n", sl);
  sl = g_list_remove_link(g_list_first(sl), kl);
  //fprintf(stderr, "\tAfter remove_link, sl=%p\n", sl);
  //fprintf(stderr, "key=%s", key);
  if(sname == NULL) {
    fprintf(stderr, "WARNING in read_nextnano_inputfile: have data but does not belong to a section!  Assuming section \"%s\".\n", DEFAULT_SECTION);
    /*If there's no "default" section, create it, add it, then add
      the data.*/
    sname = DEFAULT_SECTION;
  }
  //fprintf(stderr, "*section is: %s\n", sname);
  /*Create a new chunk if we need one; save off the current
    chunk if we're starting a new one.*/
  if((chunk == NULL) || starts_new_chunk(sname, chunk, key)) {
    //fprintf(stderr, "*is a new chunk\n");
    /*We've not yet started a chunk.*/
    chunk = g_hash_table_new_full(g_str_hash, g_str_equal, free, destroy_values);
    /*Go ahead and add the chunk to the section now; 
     * we'll still have the pointer around later to add to the
     * chunk.
     */
    s = g_list_append(s, chunk);
  }
  val = sl;
  g_hash_table_replace(chunk, key, val);
      }
      //fprintf(stderr, "\n*done with key/value pair line.\n");
    }else{
      esec=get_section_end(str);
      if((esec != NULL) || ((sec != NULL) && (sname != NULL))) {
  if(esec != NULL){
    if(sname == NULL) {
      fprintf(stderr, "WARNING: got a request to end section (%s) but there was no section currently open!\n", esec);
      continue;
    }else if(strcmp(esec, sname) != 0) {
      fprintf(stderr, "WARNING: got a request to end section (%s) but a different section is currently open (%s)!\n", esec, sname);
    }     
  }else{
      fprintf(stderr, "WARNING: 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);
  if((tmp = g_hash_table_lookup(sections, sname)) == NULL) {
    g_hash_table_replace(sections, sname, g_list_append(NULL, s));
  }else{
    tmp = g_list_append(tmp, s);
  }
  s = NULL;
  sname = NULL;
      }else{
  sname=sec;
      }
      if(esec != NULL) free_esec(esec);
      esec = NULL;
      sec=NULL;
      s = NULL;
      val = NULL;
      chunk = NULL;
    }
  }

  if(sname != NULL) {
    fprintf(stderr, "WARNING: section (%s) wasn't ended before the end of the file.\n", sname);
    if((tmp = g_hash_table_lookup(sections, sname)) == NULL) {
      g_hash_table_replace(sections, sname, g_list_append(NULL, s));
    }else{
      tmp = g_list_append(tmp, s);
    }
    s = NULL;
    sname = NULL;
  }
  g_object_unref(in);
  return sections;
}

Here is the call graph for this function:

Here is the caller 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:

static int starts_new_chunk ( const char *  secname,
GHashTable *  chunk,
const char *  key 
) [static]

Definition at line 105 of file read_nextnano_inputfile.c.

Referenced by read_nextnano_inputfile_stringy_core().

                                                                                     {
  if(chunk == NULL) return 1;
  if(g_hash_table_lookup(chunk, key) != NULL) return 1;
  return 0;
}

Here is the caller graph for this function:

static void stick_this_in_the_hash ( gpointer  key,
gpointer  data,
gpointer  hash 
) [static]

Definition at line 318 of file read_nextnano_inputfile.c.

Referenced by read_nextnano_inputfile_stringy().

                                                                               {
  g_hash_table_insert(hash, key, data);
}

Here is the caller graph for this function:

 All Classes Files Functions Variables Enumerations Enumerator Defines