Commit f540d17d authored by Matthias Schroeder's avatar Matthias Schroeder

homsearch.c added

parent f7a844d2
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <math.h>
#include <ctype.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#define LINE_SIZE 300
typedef struct {
int id; // index of formula
int C, H, O;
char rest[32];
int group;
} MOLS;
MOLS* mols;
int lastmols = 0;
void read_homsearch_csv(char* fname) {
char line[LINE_SIZE], buf[32];
int line_count, bufcnt, colidx = 0,
colC=-1, colH=-1, colO=-1, colMass=-1;
FILE* infh = fopen(fname, "r");
if (!infh) {
printf("homsearch-File %s nicht zu oeffnen\n", fname);
exit(1);
}
line_count= 0;
while (fgets(line, LINE_SIZE, infh) != NULL) {
// printf("<%s>", line);
line_count += 1;
}
printf("linecount=%d\n", line_count);
mols = (MOLS *)malloc(line_count * sizeof(MOLS));
rewind(infh);
buf[0]= '\0'; bufcnt = 0;
fgets(line, LINE_SIZE, infh);
for (char* c = line; *c; c++) {
if (*c==',' || *c=='\n') {
buf[bufcnt++] = '\0';
if (strcmp(buf, "C") == 0) {
colC = colidx;
} else if (strcmp(buf, "H") == 0) {
colH = colidx;
} else if (strcmp(buf, "O") == 0) {
colO = colidx;
}
buf[0]='\0'; bufcnt = 0; colidx += 1;
} else {
buf[bufcnt++] = *c;
}
}
colMass = colidx - 1;
printf("C:%d\nH:%d\nO:%d\nMASS:%d\n", colC, colH, colO, colMass);
int i = 0;
char* end;
while (fgets(line, LINE_SIZE, infh) != NULL) {
colidx = 0;
mols[i].rest[0] = '\0';
for (char* c = line; *c; c++) {
if (*c==',' || *c=='\n') {
buf[bufcnt++] = '\0';
if (colidx == 0) {
mols[i].id = strtol(buf, NULL, 10);
} else if (colidx == colC) {
mols[i].C = strtol(buf, NULL, 10);
} else if (colidx == colH) {
mols[i].H = strtol(buf, NULL, 10);
} else if (colidx == colO) {
mols[i].O = (int)strtol(buf, NULL, 10);
} else if (colidx == colMass) {
} else {
strcat(mols[i].rest, buf);
strcat(mols[i].rest, ",");
}
buf[0]='\0'; bufcnt = 0; colidx += 1;
} else {
buf[bufcnt++] = *c;
}
}
printf("id=%8d C:%3d H:%3d O:%3d rest:%s\n", mols[i].id, mols[i].C, mols[i].H, mols[i].O, mols[i].rest);
lastmols = i;
i+=1;
}
fclose(infh);
}
void do_homsearch() {
idx = 0;
}
void main(int argc, char* *argv) {
read_homsearch_csv("homsearch.csv");
do_homsearch();
free(mols);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment