summaryrefslogtreecommitdiff
path: root/demo.cc
blob: 078a4f5094adb63729bddc0bdd119aeb4643aca3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <cstdlib>
#include <dux/io>
#include <iostream>
#include <zp/str>

template<typename ltyp,typename rtyp> static auto cmp(long const lin,::zp::i8 const mth,ltyp const& lval,rtyp const& rval) -> void {
	auto const cmp = [&lval,&mth,&rval]() -> bool {
		switch (mth) {
		default:
			::zp::unrch();
		case 0x0u:
			return lval == rval;
		case 0x1u:
			return lval >= rval;
		case 0x2u:
			return lval >  rval;
		case 0x3u:
			return lval <= rval;
		case 0x4u:
			return lval <  rval;
		case 0x5u:
			return lval != rval;
		}
	}();

	auto const mthstr = [&mth]() -> char const* {
		switch (mth) {
		default:
			::zp::unrch();
		case 0x0u:
			return "!=";
		case 0x1u:
			return "<";
		case 0x2u:
			return "<=";
		case 0x3u:
			return ">";
		case 0x4u:
			return ">=";
		case 0x5u:
			return "==";
		}
	}();

	if (!cmp) {
		::std::cout << lin << ": " << lval << ' ' << mthstr << ' ' << rval << ::std::endl;
		::std::exit(EXIT_SUCCESS);
	}
};
#define chkeq(lval,rval) (::cmp(__LINE__,0x0u,(lval),(rval)))
#define chkge(lval,rval) (::cmp(__LINE__,0x1u,(lval),(rval)))
#define chkgt(lval,rval) (::cmp(__LINE__,0x2u,(lval),(rval)))
#define chkle(lval,rval) (::cmp(__LINE__,0x3u,(lval),(rval)))
#define chklt(lval,rval) (::cmp(__LINE__,0x4u,(lval),(rval)))
#define chkne(lval,rval) (::cmp(__LINE__,0x5u,(lval),(rval)))

int main() {
	//::dux_pri("dux {i04}.{i04}, demo\n",dux_api,dux_ext);
	::std::cout << "dux " << ::dux::ver.api << '.' << ::dux::ver.ext << ", demo" << ::std::endl;

	::dux_err err = ::dux_err_oky;

	auto const chkerr = [&err](char const* const msg) noexcept {
		if (err != ::dux_err_oky) {
			::zp::siz const errmsglen = ::dux_errmsg(nullptr,err);
			char *    const errmsg    = new char[errmsglen+0x1u];
			
			::dux_errmsg(errmsg,err);
			errmsg[errmsglen] = '\x00';

			::std::cout << msg << ": " << errmsg << ::std::endl;
			delete[] errmsg;

			::std::exit(EXIT_FAILURE);
		}
	};

	::zp::siz const homdirlen = ::dux_homdir(nullptr);
	char * const    homdir    = new char[homdirlen+0x1u];

	::dux_homdir(homdir);
	homdir[homdirlen] = '\x00';

	::std::cout << "Home directory: " << homdir << ::std::endl;
	delete[] homdir;

	err = ::dux_crtdir("testdir",0755u);
	chkerr("unable to create directory");

	::dux_fil * fil = nullptr;

	err = ::dux_crt(&fil,"testdir/dux-demo-file",0644u);
	chkerr("unable to create file");

	constexpr char src[] =
		"static void msg(void);\n"
		"\n"
		"int main(void) {\n"
		"\tmsg();\n"
		"\treturn 0x45;\n"
		"}\n"
		"\n"
		"#include <ly/io.h>\n"
		"\n"
		"static void msg(void) {\n"
		"\tly_wrtstr(ly_dflout,\"flux ftw\\n\");\n"
		"} /* Remember the last newline -> */\n";
	constexpr ::zp::siz srclen = sizeof (src)-0x1u;

	err = ::dux_wrtstr(fil,src);
	chkerr("unable to write to file");

	err = ::dux_cls(fil);
	chkerr("unable to close file");

	::dux_pthinf pthinf;
	err = ::dux_sttpth(&pthinf,"testdir/dux-demo-file");
	chkerr("unable to stat file");

	chkeq(pthinf.siz,srclen);

	err = ::dux_cpy("testdir/dux-demo-file.c","testdir/dux-demo-file",0644u);
	chkerr("unable to copy file");

	err = ::dux_opn(&fil,"testdir/dux-demo-file.c");
	chkerr("unable to open file");

	char buf[srclen+0x1u];
	buf[srclen] = '\x00';

	::zp::siz numred = 0x0u;
	err = ::dux_red(buf,fil,srclen,&numred);
	chkerr("unable to read file");

	chkeq(numred,srclen);

	chkeq(::zp::strequ(buf,src),true);

	::dux_cls(fil);

	err = ::dux_wrtstr(dux_odfl,"Goodbye!\n");
	chkerr("unable to say goodbye");
}