|
gimme-alpha 0.1
|
#include <stdlib.h>#include <string.h>#include <stdio.h>#include "gimme_alpha_types.h"#include "textfile_common.h"#include "gcc_hints.h"
Include dependency graph for textfile_common.c:Go to the source code of this file.
Functions | |
| hotfunc void | prettify_line (char *line, char comment) |
| static void | print_list (gpointer s, gpointer u) |
| static void | print_lists (gpointer s, gpointer u) |
| static void | split_list_into_sublist (gpointer s, gpointer listlist) |
| static void | pop_n_push_keys (void **l, GList **k) |
| GList * | get_key_value_pairs (char *s, const char *delim) |
| static void | free_if_not_null (gpointer data) |
| GList * | read_titled_whitespaced_data (GInputStream *istream, GError **err, GList **titles, char comment, enum data_types t) |
| static void free_if_not_null | ( | gpointer | data | ) | [static] |
Definition at line 201 of file textfile_common.c.
Referenced by read_titled_whitespaced_data().
{
if(data != NULL) free(data);
}
Here is the caller graph for this function:| 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:| static void pop_n_push_keys | ( | void ** | l, |
| GList ** | k | ||
| ) | [static] |
Definition at line 113 of file textfile_common.c.
References unlikely.
Referenced by get_key_value_pairs().
{
if(unlikely((l == NULL) || (k == NULL))) return;
GList **list = (GList**)l;
/*
fprintf(stderr, "\tpop_n_push_keys: list=%p(%p) key=%p(%p)\n", list, *list, k, *k);
fprintf(stderr, "\t\tList status:\n");
if(*list == NULL) {
fprintf(stderr, "\t\t(nil)\n");
}else{
g_list_foreach(*list, print_list, NULL);
}
*/
if((*k) != NULL) {
//fprintf(stderr, "\t\tkey=%s\n", (char*)((*k)->data));
*list = g_list_concat(*k, g_list_first(*list));
//fprintf(stderr, "\t\tafter prepend: list=%p key=%p (%s)\n", *list, *k, (char*)((*k)->data));
}
/*
fprintf(stderr, "\t\tList status:\n");
if(*list == NULL) {
fprintf(stderr, "\t\t(nil)\n");
}else{
g_list_foreach(*list, print_list, NULL);
}
*/
*k = g_list_last(*list);
/*
fprintf(stderr, "\t\tafter setting k: list=%p key=%p (%s)\n", *list, *k, (char*)((*k)->data));
fprintf(stderr, "\t\tList status:\n");
if(*list == NULL) {
fprintf(stderr, "\t\t(nil)\n");
}else{
g_list_foreach(*list, print_list, NULL);
}
*/
*list = g_list_remove_link(*list, *k);
/*
fprintf(stderr, "\t\tafter removing k: list=%p key=%p (%s)\n", *list, *k, (char*)((*k)->data));
fprintf(stderr, "\t\tList status:\n");
if(*list == NULL) {
fprintf(stderr, "\t\t(nil)\n");
}else{
g_list_foreach(*list, print_list, NULL);
}
*/
}
Here is the caller graph for this function:| hotfunc 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:| static void print_list | ( | gpointer | s, |
| gpointer | u | ||
| ) | [static] |
Definition at line 84 of file textfile_common.c.
Referenced by print_lists().
{
printf("\t\t(%s)\n", (char*)s);
}
Here is the caller graph for this function:| static void print_lists | ( | gpointer | s, |
| gpointer | u | ||
| ) | [static] |
Definition at line 87 of file textfile_common.c.
References print_list().
{
g_list_foreach(g_list_first(s), print_list, u);
if(s == NULL) printf("[null list]\n");
printf("\n");
}
Here is the call 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:| static void split_list_into_sublist | ( | gpointer | s, |
| gpointer | listlist | ||
| ) | [static] |
Definition at line 94 of file textfile_common.c.
Referenced by get_key_value_pairs().
{
GList *slist = NULL;
GList **ll = (GList **)listlist;
char *p;
char *saveptr = NULL;
//fprintf(stderr, "\tsplit_list_into_sublist: listlist=%p(%p), s=(%s)\n", listlist, (listlist==NULL)?NULL:(*ll), (char*)s);
slist = g_list_append(slist, strtok_r((char*)s, "\t", &saveptr));
while((p=strtok_r(NULL, "\t", &saveptr)) != NULL) {
//fprintf(stderr, "\t\tp=(%s)\n", p);
slist = g_list_append(slist, p);
}
*ll = g_list_append(*ll, slist);
}
Here is the caller graph for this function: