Skip to content

Instantly share code, notes, and snippets.

@ntBre
Created October 5, 2022 19:26
Show Gist options
  • Select an option

  • Save ntBre/527ebbcac4ef342405e9470ef0707485 to your computer and use it in GitHub Desktop.

Select an option

Save ntBre/527ebbcac4ef342405e9470ef0707485 to your computer and use it in GitHub Desktop.
trying to read mopac parameters from the input file in C
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/*
INTERNAL keyword requested by INTERNAL=block_name, where the parameters are
included in the same format used by EXTERNAL but inside of a block started by
block_name and ended "end block_name"
*/
/*
read parameters from `fname` into `params`
*/
int read_internal(char* fname, char* params) {
FILE *input = fopen(fname, "r");
if (input == NULL) {
return 1;
}
char line[100];
int in_params = 0;
char block_start[100];
char block_end[100] = "end ";
char *param_ptr = params;
while (fgets(line, 100, input) != NULL) {
/* determine block_name by taking apart keywords */
if (strstr(line, "internal=") != NULL) {
char *l = strtok(line, "=");
while (strstr(l, "internal") == NULL) {
l = strtok(NULL, "=");
}
/* l here contains the block_name but may be followed by whitespace and/or
the start of another keyword, so save only the part we want */
l = strtok(NULL, "=");
int i = 0;
while(!isspace(*l)) {
block_start[i] = *l;
block_end[4 + i++] = *l++;
}
} else if (strstr(line, block_end) != NULL) {
in_params = 0;
} else if (strstr(line, block_start) != NULL) {
in_params = 1;
} else if (in_params) {
char* p = line;
while(*p != '\0') {
*param_ptr++ = *p;
p++;
}
}
}
return 0;
}
int main(int argc, char *argv[]) {
char params[1000];
int c = read_internal(argv[1], params);
printf("%s\n", params);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment