summaryrefslogtreecommitdiff
path: root/src/glautils/gla.c
blob: 7922c4fa35ff3ace150acecf2ab0cacb1f60f058 (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
/**
 * gla.c - main source program file for displaying the glass map.
 *
 * Copyright (C) 2025 https://optics-design.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * See the COPYING file for the full license text.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[]){
    int filecount = 0;
    char rfilename[FILENAME_MAX];
    char glassname[50];

    FILE *readfile;

    if (argc==1){
        printf("This program is designed to work with glass data from the manufacturers\n");
        return  1;
    }
    //Argument parsing
    while (--argc>0) {
        if ((strstr((++argv)[0], ".xml"))){
            filecount++;
            strcpy(rfilename, argv[0]);
        }
        else {
            strcpy(glassname, argv[0]);
            for (int i =0; i<50; i++) {
                if (!glassname[i]){
                    break;
                }
                if (glassname[i] >= 'a' && glassname[i] <= 'z'){
                    glassname[i] = glassname[i] - ('a'- 'A');
                }
            
            }
            // while(*glassname){
            //     if (*glassname >= 'a' && *glassname <= 'z'){
            //         *glassname = *glassname - ('a'- 'A');
            //     }
            //     glassname++;
            // }
            // glassname = glassname -1;
        
        }

    }
    // printf("%s\n", rfilename);
    readfile = fopen(rfilename, "rb");
    if (readfile == NULL) {
        perror("Failed to open file");
        return 1;
    }

    fseek(readfile, 0, SEEK_END);

    long file_size = ftell(readfile);
    if (file_size == -1) {
        perror("ftell failed");
        fclose(readfile);
        return 1;
    }
    char* buffer = malloc(file_size + 1);

    fseek(readfile, 0, SEEK_SET);

    if (buffer == NULL) {
        perror("Failed to allocate memory");
        fclose(readfile);
        return 1;
    }
    // Read the file into the buffer
    size_t read_size = fread(buffer, 1, file_size, readfile);
    if (read_size != file_size) {
        perror("Failed to read the complete file");
        free(buffer);
        fclose(readfile);
        return 1;
    } 
    // Null terminate the buffer (in case it's treated as a string)
    buffer[file_size] = '\0';
    fclose(readfile);

    // char *newline_ptr = NULL;
    // for (int i = 0; i < file_size; i++) {
    //     if (buffer[i] == '\n') {
    //         newline_ptr = &buffer[i];
    //         break;
    //     }
    // }
    // fwrite(buffer, 1, newline_ptr - buffer, stdout);
    char* line_start = buffer;
    char* line_end = NULL;
    int line_length;
    int glafound = 0;

    while ((line_end=strchr(line_start, '\n'))) {
        *line_end = '\0';
        if (!glafound){
            if (strstr(line_start, glassname) && strstr(line_start, "<GlassName>")) {
                line_length = strlen(line_start)-27; // 2*Length_of_tag+1 + 4
                printf("Name: %.*s, ", line_length, line_start+14); // Length_of_tag+3
                glafound = 1;
            }
        }

        else{
            if (strstr(line_start, "</NumericName>")) {
                line_length = strlen(line_start)-31; // 2*Length_of_tag+1 + 4
                printf("Code: %.*s", line_length, line_start+16); // Length_of_tag+3
            }
            // if (strstr(line_start, "</EquationType>")) {
            //     line_length = strlen(line_start)-33; // 2*Length_of_tag+1 + 4
            //     printf("Equation: %.*s\n", line_length, line_start+17); // Length_of_tag+3
            // }
            // if (strstr(line_start, "</Coefficient>")) {
            //     line_length = strlen(line_start)-32; // 2*Length_of_tag+1 + 4
            //     printf("Coefficient: %.*s\n", line_length, line_start+17); // Length_of_tag+3
            // }
            if (strstr(line_start, "</Glass>")) {
                printf("\n");
                glafound = 0;
            }
        }
        line_start = line_end+1;

    }

    free(buffer);
    return 0;
}
Back to https://optics-design.com