29 lines
2.4 KiB
C
29 lines
2.4 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
#include "xtc/xtc_settings.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#define CHECK(x) do {if(!(x)){fprintf(stderr,"%d: %s\n",__LINE__,#x);return 1;}}while(0)
|
|
int main(void)
|
|
{
|
|
char path[]="/tmp/xtc-settings-XXXXXX",value[4096]; int fd=mkstemp(path); size_t i,n;
|
|
xtc_settings a,b,before; struct stat st; FILE *f;
|
|
CHECK(fd>=0);close(fd);unlink(path);
|
|
xtc_settings_defaults(&a);CHECK(xtc_settings_validate(&a)==XTC_OK);
|
|
b=a;CHECK(xtc_settings_load(&b,path)==XTC_ERR_NOT_FOUND);
|
|
strcpy(a.clap_paths,"~/space folder:/tmp/quoted\"name\\path");a.loop_enabled=1;a.follow_playhead=1;a.transport_detached=1;a.mixer_detached=1;a.transport_x=-30000;a.transport_width=400;a.note_velocity=87;strcpy(a.midi_input_port,"Keyboard:out");strcpy(a.midi_output_port,"Synth:in");a.midi_input_channel=2;a.midi_output_channel=4;a.midi_thru=1;
|
|
CHECK(xtc_settings_save(&a,path)==XTC_OK);CHECK(stat(path,&st)==0 && (st.st_mode&0777)==0600);
|
|
CHECK(xtc_settings_load(&b,path)==XTC_OK);CHECK(memcmp(&a,&b,sizeof(a))==0);
|
|
xtc_settings_schema(&n);
|
|
for(i=0;i<n;++i){CHECK(xtc_settings_get(&a,i,value,sizeof(value))==XTC_OK);CHECK(xtc_settings_set(&b,i,value)==XTC_OK);}
|
|
CHECK(memcmp(&a,&b,sizeof(a))==0);
|
|
{const char *bad[]={"[midi]\nmidi_input_channel=16\n","[midi]\nmidi_thru=2\n","[unknown]\n","[interface]\ntransport_width=399\n","[interface]\nmixer_detached=2\n","[audio]\nchannels=3\n","[audio]\nchannels=1\nchannels=2\n","[audio]\nchannels=9999999999999999999999\n","[plugins]\nclap_paths=\"unterminated\n","[transport]\nloop_start=4000\nloop_end=2\n"};
|
|
for(i=0;i<sizeof(bad)/sizeof(bad[0]);++i){f=fopen(path,"wb");CHECK(f);CHECK(fputs(bad[i],f)>=0);CHECK(fclose(f)==0);before=b;CHECK(xtc_settings_load(&b,path)==XTC_ERR_FORMAT);CHECK(memcmp(&before,&b,sizeof(b))==0);}}
|
|
f=fopen(path,"wb");CHECK(f);CHECK(fwrite(";x\0hidden",1,9,f)==9);fclose(f);CHECK(xtc_settings_load(&b,path)==XTC_ERR_FORMAT);
|
|
CHECK(xtc_settings_save(&a,path)==XTC_OK);b=a;b.loop_end=b.loop_start;CHECK(xtc_settings_save(&b,path)==XTC_ERR_INVALID_ARGUMENT);CHECK(xtc_settings_load(&b,path)==XTC_OK && b.loop_end==a.loop_end);
|
|
CHECK(xtc_settings_save(&a,"/no-such-xtc-directory/settings")==XTC_ERR_IO);
|
|
CHECK(xtc_settings_path(value,sizeof(value))==XTC_OK && strstr(value,"/.xtc.conf"));
|
|
unlink(path);puts("Preferences INI validation, roundtrip, permissions and failure tests passed.");return 0;
|
|
}
|