FILE *fp;
ssize_t i;
+ memset(buf,0,META_MAX_LINE_SIZE);
+
if(NULL==path) { return -1; }
if(NULL==entry) { return -1; }
char *p;
size_t len, left;
+ if((NULL==buf)||(0==buf_size)) { return -1; }
+
+ // guarantee that buf is NULL-terminated
+ buf[buf_size-1] = '\0';
+
+ if(0==strlen(buf)) { return -1; }
+
const char to_escape[] = "<\\>&";
p = strpbrk(buf,to_escape);
switch(p[0]) {
case '<':
- if(left<=4) { return -1; }
- memmove(&(p[4]),p,len);
- strcpy(p,"<");
- p += 4;
+ if(left<4) { return -1; }
+ memmove(&(p[3]),p,len);
+ memmove(p,"<",4);
break;
case '\\':
if(p[1]=='n') {
p[0] = '\n';
memmove(&(p[1]),&(p[2]),len);
- p++;
+ p[len-1] = '\0';
}
break;
case '>':
- if(left<=4) { return -1; }
- memmove(&(p[4]),p,len);
- strcpy(p,">");
- p += 4;
+ if(left<4) { return -1; }
+ memmove(&(p[3]),p,len);
+ memmove(p,">",4);
break;
case '&':
- if(left<=5) { return -1; }
- memmove(&(p[5]),p,len);
- strcpy(p,"&");
- p += 5;
+ if(left<5) { return -1; }
+ memmove(&(p[4]),p,len);
+ memmove(p,"&",5);
break;
default:
return -1;
}
+ p++;
p = strpbrk(p,to_escape);
}
p = concat(path,".meta");
if(NULL==p) { return NULL; }
} else {
- p = malloc(sizeof(char)*len);
+ p = malloc(sizeof(char)*(len+6)); // strlen(.meta)[5] + '\0'
if(NULL==p) { return NULL; }
strcpy(p,path);
}
fp = fopen(p,"r");
+ free(p);
return fp;
}
static ssize_t next_line(FILE *fp, char **key, char **value, char *buf, size_t buf_size) {
- if(fgets(buf,buf_size,fp)==NULL) { return -1; }
+ size_t len;
+
+ if(fgets(buf,buf_size,fp)==NULL) { return (feof(fp))?0:-1; }
+
+ len = strlen(buf);
+ if(buf[len-1]=='\n') {
+ buf[len-1] = '\0';
+ }
if(meta_escape(buf,buf_size)<0) { return -1; }
(**value) = '\0';
(*value)++;
+
return buf_size - strlen(*key);
}
int main();
static void meta_entry_basic_test();
+static void meta_escape_basic_test();
+static void meta_escape_length_test();
static void meta_info_basic_test();
+static void meta_search_basic_test();
int main() {
setup_env();
+ meta_escape_basic_test();
+ meta_escape_length_test();
+
+ meta_search_basic_test();
+
meta_entry_basic_test();
meta_info_basic_test();
// (and as such will have invalid fields)
assert(meta_entry(PREFIX,entry)==-1);
+ // half written entries must be free'd
+ rss_entry_free(entry);
+ assert(1==rss_entry_init(&entry));
+
assert(meta_entry(TEST_FILE_1,entry)==1);
assert(strcmp(entry->title,"test title")==0);
rss_entry_free(entry);
}
+static void meta_escape_length_test() {
+ char buf[10];
+
+ memset(buf,0,10);
+
+ strcpy(buf,"<");
+ assert(meta_escape(buf,1)==-1);
+ strcpy(buf,"<");
+ assert(meta_escape(buf,2)==-1);
+ strcpy(buf,"<");
+ assert(meta_escape(buf,3)==-1);
+ strcpy(buf,"<");
+ assert(meta_escape(buf,4)==-1);
+ strcpy(buf,"<");
+ assert(meta_escape(buf,5)==1);
+
+ assert(strcmp(buf,"<")==0);
+}
+
+static void meta_escape_basic_test() {
+ char buf[1024];
+
+ memset(buf,0,1024);
+
+ assert(meta_escape(NULL,0)==-1);
+
+ assert(meta_escape(buf,0)==-1);
+
+ strcpy(buf,"test");
+ assert(meta_escape(buf,1024)==1);
+
+ strcpy(buf,"<test");
+ assert(meta_escape(buf,4)==-1);
+
+ strcpy(buf,"<test");
+ assert(meta_escape(buf,10)==1);
+ assert(strcmp(buf,"<test")==0);
+
+ strcpy(buf,">test");
+ assert(meta_escape(buf,4)==-1);
+
+ strcpy(buf,">test");
+ assert(meta_escape(buf,10)==1);
+ assert(strcmp(buf,">test")==0);
+
+ strcpy(buf,"\\ntest");
+ assert(meta_escape(buf,7)==1);
+ assert(strcmp(buf,"\ntest")==0);
+
+ strcpy(buf,"&test");
+ assert(meta_escape(buf,4)==-1);
+ strcpy(buf,"&test");
+ assert(meta_escape(buf,10)==1);
+ assert(strcmp(buf,"&test")==0);
+}
+
static void meta_info_basic_test() {
struct rss_channel_info *info;
// (and as such will have invalid fields)
assert(meta_info(TEST_FILE_1,info)==-1);
+ // half written values must be free'd
+ rss_channel_info_free(info);
+ assert(rss_channel_info_init(&info)==1);
+
assert(meta_info(PREFIX,info)==1);
assert(strcmp(info->title,"TITLE")==0);
rss_channel_info_free(info);
}
+
+static void meta_search_basic_test() {
+ FILE *fp;
+ fp = meta_search(NULL);
+ assert(NULL==fp);
+
+ fp = meta_search("noasdfasdlkdsaofijasdlkfjasdklfjaslkdfj");
+ assert(NULL==fp);
+
+ fp = meta_search(TEST_FILE_3);
+ assert(fp!=NULL);
+ fclose(fp);
+}