summaryrefslogtreecommitdiffstats
path: root/input.c
blob: 250676df1b6e538969e74a1b4aa61f801f9744d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

int read_float_line(FILE *input, float **vector, int *count)
{
        float   data_buffer[4096];
        char    line_buffer[4096];

        char   *ptr;
        float   datum;
        int     status;
        int     i = 0;
        int     n = 0;

        if (NULL != fgets(line_buffer, 4096, input)) {

                ptr = line_buffer;

                for (;;) {

                        /* Skip any leading whitespace */
                        while ((ptr[i]==' ' || ptr[i]=='\t')) {
                                i++;
                        }

                        /* 
                         * If we hit a newline, that's 
                         * the end of the loop. 
                         */
                        if (ptr[i] == '\n') {
                                break;
                        } else {
                                /* 
                                 * Otherwise, we must have
                                 * stopped in front of a
                                 * non-whitespace character.
                                 *
                                 * Let's scan it.
                                 */
                                status = sscanf(&(ptr[i]), "%f", &datum);

                                if (status == 0 || status == EOF) {
                                        printf("Scan error.\n");
                                        return 0;
                                } else {
                                        /* Add the new item to our data */ 
                                        data_buffer[n] = datum;
                                        n++;

                                        /* 
                                         * Scan until the end of the
                                         * thing we just read.
                                         */
                                        while ((ptr[i]!=' ' && ptr[i]!='\n')) {
                                                i++;
                                        }
                                }
                        }
                }
        } else {
                return EOF;
        }

        /*
         * If the user gave us a count pointer
         * to fill in, go ahead and do that.
         */
        if (count != NULL) {
                *count = n;
        }

        /*
         * If the user gave us a NULL pointer,
         * allocate some memory for it and
         * put the contents of data_buffer at
         * that memory location.
         */
        if (vector != NULL) {
                if (*vector == NULL) {
                        *vector = calloc(1, n*sizeof(float));
                }
                for (i=0; i<n; i++) {
                        (*vector)[i] = data_buffer[i];
                }
        }

        return 1;
}



/**
 * read_square_matrix()
 * -------------------- 
 * Read a (possibly un-seekable) input stream into memory.
 *
 * @input: Input file stream
 * @count: Number of rows in the matrix
 * Return: Square matrix of floating-point values.
 *
 * NOTE
 * The method used for reading and allocating is slightly
 * complicated, because you can't just read the stream
 * once to count the number of '\n' characters, and use
 * that to determine the number of lines, then allocate
 * them, then read it again. STDIN, for example, cannot be
 * seeked on, so functions like rewind() will not work.
 */
float **read_square_matrix(FILE *input, int *count)
{
        float **matrix;
        float  *first_line = NULL;
        int     n = 0;
        int     i = 0;

        /*
         * Parse the first line into an array of floats.
         */
        if (EOF == read_float_line(input, &first_line, &n)) {
                fprintf(stderr, "Couldn't read first line.\n");
                return NULL;
        }
                
        /*
         * Now we know how many fields there are, we can
         * allocate the right number of row pointers
         * (since the matrix is square). 
         */
        if (NULL == (matrix=calloc(n, sizeof(float *)))) {
                fprintf(stderr, "Out of memory.\n");
                return NULL;
        }

        /* 
         * Assign the first row (We already scanned it in)
         */
        matrix[0] = first_line;

        /*
         * Scan the remaining lines into the allocated
         * matrix region.
         */
        for (i=1; i<n; i++) {
                float *line = NULL;
                if (EOF != read_float_line(input, &line, NULL)) {
                        matrix[i] = line; 
                }
        }

        /* Assign for the caller to have a count. */
        if (count != NULL) {
                *count = n;
        }

        return matrix;
}