#include <ldns/common.h>
-#define MAXTOKEN_LEN 1024
-#define LDNS_EAT_SPACE "\f\n\r\t\v"
+#define LDNS_PARSE_SKIP_SPACE "\f\n\r\t\v"
+#define LDNS_PARSE_NORMAL " \f\n\r\t\v"
#define MAXLINE_LEN 512
#define MAXKEYWORD_LEN 32
-/* what we can parse */
-enum ldns_enum_parse
-{
- LDNS_SPACE_STR, /* str with spaces */
- LDNS_STR, /* str without spaces */
- LDNS_QUOTE_STR, /* str with \ in it */
- LDNS_QUOTE_SPACE_STR /* str with \ in it and spaces */
-};
-typedef enum ldns_enum_parse ldns_parse;
-
/*
* get a token/char from the stream F
* return 0 on error of EOF of F
*/
ssize_t ldns_get_token(FILE *f, char *token, const char *delim);
-/*
- * get the next string and supply the type we want
- * return 0 on error, otherwise the length
- */
-ssize_t ldns_get_str(FILE *f, char *word, ldns_parse type);
-
/*
* search for keyword and delim. Give everything back
- * after the delimeter(s)
+ * after the keyword + k_del until we hit d_del
*/
+ssize_t
+ldns_get_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data, const char *d_del);
+
#endif /* _PARSE_H_ */
#include <ldns/dns.h>
#include "util.h"
-
-
-/*
- * search for keyword and delim. Give everything back
- * after the delimeter(s)
- */
ssize_t
-ldns_get_keyword_data(FILE *f, char *keyword, char *del , char *data, ldns_parse d_type)
+ldns_get_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data, const char *d_del)
{
/* we assume: keyword|sep|data */
char *fkeyword;
+ ssize_t i;
fkeyword = XMALLOC(char, MAXKEYWORD_LEN);
+ i = 0;
- ldns_get_str(f, fkeyword, LDNS_STR);
-
- printf("%s\n", fkeyword);
- return 0;
-}
-
+ i = ldns_get_token(f, fkeyword, k_del);
-ssize_t
-ldns_get_str(FILE *f, char *word, ldns_parse type)
-{
- ssize_t i;
+ printf("[%s]\n", fkeyword);
- i = 0;
- switch (type) {
- case LDNS_SPACE_STR:
- i = ldns_get_token(f, word, LDNS_EAT_SPACE);
- return i;
- case LDNS_STR:
- i = ldns_get_token(f, word, NULL);
+ /* case??? */
+ if (strncmp(fkeyword, keyword, strlen(keyword)) == 0) {
+ /* whee, the match! */
+ printf("Matching keyword\n\n");
+ /* retrieve it's data */
+ i = ldns_get_token(f, data, d_del);
return i;
- case LDNS_QUOTE_STR:
- i = ldns_get_token(f, word, NULL);
- break;
- case LDNS_QUOTE_SPACE_STR:
- i = ldns_get_token(f, word, LDNS_EAT_SPACE);
- break;
+ } else {
+ return -1;
}
- /* only reach this spot if the str was quoted */
- /* mangle the quoted string and return what we got */
- return i;
}
+
ssize_t
ldns_get_token(FILE *f, char *token, const char *delim)
{
/* standard delimeters */
if (!delim) {
/* from isspace(3) */
- del = " \f\n\r\t\v";
+ del = LDNS_PARSE_NORMAL;
} else {
del = delim;
}
}
/* read an entire line in from the file */
- if ((t = ldns_get_str(fp, line, LDNS_SPACE_STR)) == -1) {
+ if ((t = ldns_get_token(fp, line, LDNS_PARSE_SKIP_SPACE)) == -1) {
FREE(line);
return NULL;
}
tok = XMALLOC(char, 1024);
- while ((b = ldns_get_str(f, tok, LDNS_SPACE_STR)) != 0) {
+ while ((b = ldns_get_token(f, tok, LDNS_PARSE_SKIP_SPACE)) != 0) {
fprintf(stdout, "%d: %s\n", (int)b, tok);
}
fclose(f);
exit(1);
}
- ldns_get_keyword_data(f, "Algorithm", ":", tok, LDNS_STR);
-
+ /* ldns_get_keyword_data(f, "Algorithm", ": \t", tok, LDNS_STR);*/
+ if (ldns_get_keyword_data(f, "Private-key-format",
+ ":", tok, LDNS_PARSE_SKIP_SPACE) != -1) {
+ printf("found it, found it\n");
+ printf("%s\n", tok);
+ }
fclose(f);
return 0;