[m-dev.] for review: foreign_decl multiple inclusion guards.

Tyson Dowd trd at cs.mu.OZ.AU
Sun Jan 28 11:23:50 AEDT 2001


Hi,

Here's another solution to the multiple header file inclusion problem.
It puts guards around each foreign_decl in the .h files that get
generated, and makes sure the guards are the same for each foreign_decl
that is the same.

With this change, you can remove the ML_CFLOAT_CHOICEPOINT_GUARD
in extras/clpr and it compiles fine in hlc grades.

===================================================================


Estimated hours taken: 8

Implement automatic header file multiple inclusion guards.

Note that this should not affect the LLDS backend because it doesn't
use header files to implement foreign_decls, so there is no chance of
multiple inclusions.

NEWS:
	Mention the new library functions.

compiler/mlds_to_c.m:
	Output header guards based on the contents of the foreign_decl.

	We use the md5 message digest of the contents of the foreign_decl to
	"uniquely" identify it.  Although md5 message digest are not
	unique, they are pretty close -- there is a believed to be on
	the order of 2 ^ 64 chance of two strings having the same md5
	message digest.  These are pretty good odds considering that
	the 

	If the foreign_decl *are* identical, they will of course get the
	same md5 message digest, and so will not be included twice.  At
	worst this might stop the C compiler from giving an error
	message where it otherwise would have, but since the error
	message would just be about duplicate declarations, this should
	not be a problem.  If people are abusing foreign_decls by
	putting something other than declarations in there, I guess they
	might get what they deserve.

library/string.m:
	Implement the functions string__md5/1, string__md5_bytes/1, and 
	string__md5_bytes_to_string/1.  

	The MD5 algorithm is implemented in terms of some public domain
	C code (for efficiency, as calculating md5 message digests can
	be quite slow, and because it's not a simple algorithm and I
	don't want to rewrite the code).

	For the .NET backend we just use the md5 routines in the .NET
	libraries.

tests/hard_coded/Mmakefile:
tests/hard_coded/md5sum.exp:
tests/hard_coded/md5sum.m:
	A test case for the new library function.


Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.189
diff -u -r1.189 NEWS
--- NEWS	2001/01/17 04:35:16	1.189
+++ NEWS	2001/01/25 23:19:48
@@ -20,8 +20,9 @@
   call "univ_value(Univ)" to extract the value before calling deconstruct.
   (Doing that also works in Mercury 0.9 and Mercury 0.10.)
 
-* We've addes some new library predicates: assoc_list__keys_and_values,
-  list__map2 and list__map3.
+* We've addes some new library predicates and functions: 
+  assoc_list__keys_and_values, list__map2 and list__map3,
+  string__md5, string__md5_bytes and string__md5_bytes_to_string.
 
 NEWS since Mercury release 0.9:
 -------------------------------
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.77
diff -u -r1.77 mlds_to_c.m
--- compiler/mlds_to_c.m	2001/01/17 17:37:17	1.77
+++ compiler/mlds_to_c.m	2001/01/25 23:44:31
@@ -504,11 +504,26 @@
 	foreign_decl_code, io__state, io__state).
 :- mode mlds_output_c_hdr_decl(in, in, di, uo) is det.
 
-mlds_output_c_hdr_decl(_Indent, foreign_decl_code(Lang, Code, Context)) -->
+mlds_output_c_hdr_decl(Indent, foreign_decl_code(Lang, Code, Context)) -->
 		% only output C code in the C header file.
 	( { Lang = c } ->
+		{ MD5Digest = string__md5(Code) },
+		io__write_string("#ifndef MR_HEADER_GUARD_foreign_decl_"),
+		io__write_string(MD5Digest),
+		io__nl,
+		mlds_indent(Indent),
+		io__write_string("#define MR_HEADER_GUARD_foreign_decl_"),
+		io__write_string(MD5Digest),
+		io__nl,
+		io__nl,
+
 		mlds_output_context(mlds__make_context(Context)),
-		io__write_string(Code)
+		io__write_string(Code),
+
+		io__nl,
+		io__write_string("#endif /* MR_HEADER_GUARD_foreign_decl_"),
+		io__write_string(MD5Digest),
+		io__write_string(" */\n")
 	;
 		{ sorry(this_file, "foreign code other than C") }
 	).
Index: library/string.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.141
diff -u -r1.141 string.m
--- library/string.m	2001/01/01 04:03:54	1.141
+++ library/string.m	2001/01/28 00:09:42
@@ -368,6 +368,21 @@
 :- mode string__hash(in, out) is det.
 %	Compute a hash value for a string.
 
+:- func string__md5(string) = string.
+% 	string__md5(String) = Result.
+%	Result is the MD5 message digest of String in typical hexidecimal
+%	string notation (Such as "6f5902ac237024bdd0c176cb93063dc4").
+
+:- func string__md5_bytes(string) = list(int).
+% 	string__md5_bytes(String) = Result.
+%	Result is the MD5 message digest of String represented as list of 
+%	8-bit values (stored in integers).  There will always be
+%	16 bytes in the list.
+
+:- func string__md5_bytes_to_string(list(int)::in) = (string::out) is det.
+% 	Converts the md5 digest bytes to a typical hexidecimal string
+% 	notation.
+
 :- pred string__sub_string_search(string, string, int).
 :- mode string__sub_string_search(in, in, out) is semidet.
 %	string__sub_string_search(String, SubString, Index).
@@ -996,6 +1011,376 @@
 	H1 = H0 << 5,
 	H2 = H1 `xor` H0,
 	H = H2 `xor` X.
+
+%-----------------------------------------------------------------------------%
+
+
+:- pragma foreign_decl("C",
+"
+
+/*
+ * This code implements the MD5 message-digest algorithm.
+ * The algorithm is due to Ron Rivest.  This code was
+ * written by Colin Plumb in 1993, no copyright is claimed.
+ * This code is in the public domain; do with it what you wish.
+ *
+ * Equivalent code is available from RSA Data Security, Inc.
+ * This code has been tested against that, and is equivalent,
+ * except that you don't need to include two pages of legalese
+ * with every copy.
+ *
+ * To compute the message digest of a chunk of bytes, declare an
+ * MD5Context structure, pass it to MD5Init, call MD5Update as
+ * needed on buffers full of bytes, and then call MD5Final, which
+ * will fill a supplied 16-byte array with the digest.
+ *
+ * Changed so as no longer to depend on Colin Plumb's `usual.h' header
+ * definitions; now uses stuff from dpkg's config.h.
+ *  - Ian Jackson <ijackson at nyx.cs.du.edu>.
+ * Still in the public domain.
+ */
+
+struct ML_MD5Context {
+	MR_uint_least32_t buf[4];
+	MR_uint_least32_t bytes[2];
+	MR_uint_least32_t in[16];
+};
+
+static void ML_MD5Init(struct ML_MD5Context *context);
+static void ML_MD5Update(struct ML_MD5Context *context,
+	MR_UnsignedChar const *buf, unsigned len);
+static void ML_MD5Final(unsigned char digest[16],
+	struct ML_MD5Context *context);
+static void ML_MD5Transform(MR_uint_least32_t buf[4],
+	MR_uint_least32_t const in[16]);
+
+").
+
+:- pragma foreign_code("C",
+"
+
+#ifdef MR_BIG_ENDIAN
+static void
+ML_byteSwap(MR_uint_least32_t *buf, unsigned words)
+{
+	MR_UnsignedChar *p = (MR_UnsignedChar *)buf;
+
+	do {
+		*buf++ = (MR_uint_least32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
+			((unsigned)p[1] << 8 | p[0]);
+		p += 4;
+	} while (--words);
+}
+#else
+#define ML_byteSwap(buf,words)
+#endif
+
+/*
+ * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
+ * initialization constants.
+ */
+static void
+ML_MD5Init(struct ML_MD5Context *ctx)
+{
+	ctx->buf[0] = 0x67452301;
+	ctx->buf[1] = 0xefcdab89;
+	ctx->buf[2] = 0x98badcfe;
+	ctx->buf[3] = 0x10325476;
+
+	ctx->bytes[0] = 0;
+	ctx->bytes[1] = 0;
+}
+
+/*
+ * Update context to reflect the concatenation of another buffer full
+ * of bytes.
+ */
+static void
+ML_MD5Update(struct ML_MD5Context *ctx,
+	MR_UnsignedChar const *buf, unsigned len)
+{
+	MR_uint_least32_t t;
+
+	/* Update byte count */
+
+	t = ctx->bytes[0];
+	if ((ctx->bytes[0] = t + len) < t)
+		ctx->bytes[1]++;	/* Carry from low to high */
+
+	t = 64 - (t & 0x3f);	/* Space available in ctx->in (at least 1) */
+	if (t > len) {
+		memcpy((MR_UnsignedChar *)ctx->in + 64 - t, buf, len);
+		return;
+	}
+	/* First chunk is an odd size */
+	memcpy((MR_UnsignedChar *)ctx->in + 64 - t, buf, t);
+	ML_byteSwap(ctx->in, 16);
+	ML_MD5Transform(ctx->buf, ctx->in);
+	buf += t;
+	len -= t;
+
+	/* Process data in 64-byte chunks */
+	while (len >= 64) {
+		memcpy(ctx->in, buf, 64);
+		ML_byteSwap(ctx->in, 16);
+		ML_MD5Transform(ctx->buf, ctx->in);
+		buf += 64;
+		len -= 64;
+	}
+
+	/* Handle any remaining bytes of data. */
+	memcpy(ctx->in, buf, len);
+}
+
+/*
+ * Final wrapup - pad to 64-byte boundary with the bit pattern 
+ * 1 0* (64-bit count of bits processed, MSB-first)
+ */
+static void
+ML_MD5Final(MR_UnsignedChar digest[16], struct ML_MD5Context *ctx)
+{
+	int count = ctx->bytes[0] & 0x3f;	/* Number of bytes in ctx->in */
+	MR_UnsignedChar *p = (MR_UnsignedChar *)ctx->in + count;
+
+	/* Set the first char of padding to 0x80.  There is always room. */
+	*p++ = 0x80;
+
+	/* Bytes of padding needed to make 56 bytes (-8..55) */
+	count = 56 - 1 - count;
+
+	if (count < 0) {	/* Padding forces an extra block */
+		memset(p, 0, count + 8);
+		ML_byteSwap(ctx->in, 16);
+		ML_MD5Transform(ctx->buf, ctx->in);
+		p = (MR_UnsignedChar *)ctx->in;
+		count = 56;
+	}
+	memset(p, 0, count);
+	ML_byteSwap(ctx->in, 14);
+
+	/* Append length in bits and transform */
+	ctx->in[14] = ctx->bytes[0] << 3;
+	ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
+	ML_MD5Transform(ctx->buf, ctx->in);
+
+	ML_byteSwap(ctx->buf, 4);
+	memcpy(digest, ctx->buf, 16);
+	memset(ctx, 0, sizeof(ctx));	/* In case it's sensitive */
+}
+
+/* The four core functions - ML_F1 is optimized somewhat */
+
+/* #define ML_F1(x, y, z) (x & y | ~x & z) */
+#define ML_F1(x, y, z) (z ^ (x & (y ^ z)))
+#define ML_F2(x, y, z) ML_F1(z, x, y)
+#define ML_F3(x, y, z) (x ^ y ^ z)
+#define ML_F4(x, y, z) (y ^ (x | ~z))
+
+/* This is the central step in the MD5 algorithm. */
+#define ML_MD5STEP(f,w,x,y,z,in,s) \
+	 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
+
+/*
+ * The core of the MD5 algorithm, this alters an existing MD5 hash to
+ * reflect the addition of 16 longwords of new data.  MD5Update blocks
+ * the data and converts bytes into longwords for this routine.
+ */
+void
+ML_MD5Transform(MR_uint_least32_t buf[4], MR_uint_least32_t const in[16])
+{
+	register MR_uint_least32_t a, b, c, d;
+
+	a = buf[0];
+	b = buf[1];
+	c = buf[2];
+	d = buf[3];
+
+	ML_MD5STEP(ML_F1, a, b, c, d, in[0] + 0xd76aa478, 7);
+	ML_MD5STEP(ML_F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
+	ML_MD5STEP(ML_F1, c, d, a, b, in[2] + 0x242070db, 17);
+	ML_MD5STEP(ML_F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
+	ML_MD5STEP(ML_F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
+	ML_MD5STEP(ML_F1, d, a, b, c, in[5] + 0x4787c62a, 12);
+	ML_MD5STEP(ML_F1, c, d, a, b, in[6] + 0xa8304613, 17);
+	ML_MD5STEP(ML_F1, b, c, d, a, in[7] + 0xfd469501, 22);
+	ML_MD5STEP(ML_F1, a, b, c, d, in[8] + 0x698098d8, 7);
+	ML_MD5STEP(ML_F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
+	ML_MD5STEP(ML_F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
+	ML_MD5STEP(ML_F1, b, c, d, a, in[11] + 0x895cd7be, 22);
+	ML_MD5STEP(ML_F1, a, b, c, d, in[12] + 0x6b901122, 7);
+	ML_MD5STEP(ML_F1, d, a, b, c, in[13] + 0xfd987193, 12);
+	ML_MD5STEP(ML_F1, c, d, a, b, in[14] + 0xa679438e, 17);
+	ML_MD5STEP(ML_F1, b, c, d, a, in[15] + 0x49b40821, 22);
+
+	ML_MD5STEP(ML_F2, a, b, c, d, in[1] + 0xf61e2562, 5);
+	ML_MD5STEP(ML_F2, d, a, b, c, in[6] + 0xc040b340, 9);
+	ML_MD5STEP(ML_F2, c, d, a, b, in[11] + 0x265e5a51, 14);
+	ML_MD5STEP(ML_F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
+	ML_MD5STEP(ML_F2, a, b, c, d, in[5] + 0xd62f105d, 5);
+	ML_MD5STEP(ML_F2, d, a, b, c, in[10] + 0x02441453, 9);
+	ML_MD5STEP(ML_F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
+	ML_MD5STEP(ML_F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
+	ML_MD5STEP(ML_F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
+	ML_MD5STEP(ML_F2, d, a, b, c, in[14] + 0xc33707d6, 9);
+	ML_MD5STEP(ML_F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
+	ML_MD5STEP(ML_F2, b, c, d, a, in[8] + 0x455a14ed, 20);
+	ML_MD5STEP(ML_F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
+	ML_MD5STEP(ML_F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
+	ML_MD5STEP(ML_F2, c, d, a, b, in[7] + 0x676f02d9, 14);
+	ML_MD5STEP(ML_F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
+
+	ML_MD5STEP(ML_F3, a, b, c, d, in[5] + 0xfffa3942, 4);
+	ML_MD5STEP(ML_F3, d, a, b, c, in[8] + 0x8771f681, 11);
+	ML_MD5STEP(ML_F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
+	ML_MD5STEP(ML_F3, b, c, d, a, in[14] + 0xfde5380c, 23);
+	ML_MD5STEP(ML_F3, a, b, c, d, in[1] + 0xa4beea44, 4);
+	ML_MD5STEP(ML_F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
+	ML_MD5STEP(ML_F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
+	ML_MD5STEP(ML_F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
+	ML_MD5STEP(ML_F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
+	ML_MD5STEP(ML_F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
+	ML_MD5STEP(ML_F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
+	ML_MD5STEP(ML_F3, b, c, d, a, in[6] + 0x04881d05, 23);
+	ML_MD5STEP(ML_F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
+	ML_MD5STEP(ML_F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
+	ML_MD5STEP(ML_F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
+	ML_MD5STEP(ML_F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
+
+	ML_MD5STEP(ML_F4, a, b, c, d, in[0] + 0xf4292244, 6);
+	ML_MD5STEP(ML_F4, d, a, b, c, in[7] + 0x432aff97, 10);
+	ML_MD5STEP(ML_F4, c, d, a, b, in[14] + 0xab9423a7, 15);
+	ML_MD5STEP(ML_F4, b, c, d, a, in[5] + 0xfc93a039, 21);
+	ML_MD5STEP(ML_F4, a, b, c, d, in[12] + 0x655b59c3, 6);
+	ML_MD5STEP(ML_F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
+	ML_MD5STEP(ML_F4, c, d, a, b, in[10] + 0xffeff47d, 15);
+	ML_MD5STEP(ML_F4, b, c, d, a, in[1] + 0x85845dd1, 21);
+	ML_MD5STEP(ML_F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
+	ML_MD5STEP(ML_F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
+	ML_MD5STEP(ML_F4, c, d, a, b, in[6] + 0xa3014314, 15);
+	ML_MD5STEP(ML_F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
+	ML_MD5STEP(ML_F4, a, b, c, d, in[4] + 0xf7537e82, 6);
+	ML_MD5STEP(ML_F4, d, a, b, c, in[11] + 0xbd3af235, 10);
+	ML_MD5STEP(ML_F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
+	ML_MD5STEP(ML_F4, b, c, d, a, in[9] + 0xeb86d391, 21);
+
+	buf[0] += a;
+	buf[1] += b;
+	buf[2] += c;
+	buf[3] += d;
+}
+
+").
+
+:- type md5context ---> md5context(c_pointer).
+
+:- impure pred allocate_context(md5context::out) is det. 
+:- impure pred md5init(md5context::in) is det. 
+:- impure pred md5update(md5context::in, string::in, int::in) is det. 
+:- impure pred md5final(list(int)::out, md5context::in) is det. 
+
+:- pragma foreign_code("C",
+		allocate_context(Ctxt::out),
+		[will_not_call_mercury, thread_safe], "
+	MR_incr_hp(Ctxt, ((sizeof(struct ML_MD5Context) 
+		+ sizeof(MR_Word) - 1) / sizeof(MR_Word)));
+").
+
+:- pragma foreign_code("C",
+		md5init(Ctxt::in), [will_not_call_mercury, thread_safe], "
+	ML_MD5Init((struct ML_MD5Context *) Ctxt);
+").
+
+:- pragma foreign_code("C",
+		md5update(Ctxt::in, Str::in, Length::in), 
+			[will_not_call_mercury, thread_safe], "
+	ML_MD5Update((struct ML_MD5Context *) Ctxt,
+		(MR_UnsignedChar *) Str, (unsigned) Length);
+").
+
+:- pragma foreign_code("C",
+		md5final(Digest::out, Ctxt::in), 
+			[will_not_call_mercury, thread_safe], "
+	unsigned char d[16];
+	int i;
+
+	ML_MD5Final(d, (struct ML_MD5Context *) Ctxt);
+	Digest = MR_list_empty_msg(MR_PROC_LABEL);
+	for (i = 15; i >= 0; i--) {
+		Digest = MR_list_cons_msg((MR_Integer) d[i], Digest,
+			MR_PROC_LABEL);
+	}
+").
+
+:- pragma foreign_decl("MC++", "
+
+#define ML_CastToWord(word, cptr) MR_c_pointer_to_word(word, cptr)
+#define	ML_CastToMD5(md5, word) \
+	md5 = dynamic_cast<System::Security::Cryptography::MD5>(word)
+
+").
+
+:- pragma foreign_code("MC++", 
+	allocate_context(Ctxt::out),
+		[will_not_call_mercury, thread_safe], "{
+	System::Security::Cryptography::MD5 *md5;
+	md5 = new System::Security::Cryptography::MD5_CSP();
+	ML_CastToWord(Ctxt, md5);
+}").
+
+:- pragma foreign_code("MC++", 
+	md5init(_Ctxt::in), [will_not_call_mercury, thread_safe], "{
+	// do nothing, we have already initialized.
+}").
+
+:- pragma foreign_code("MC++",
+		md5update(Ctxt::in, Str::in, _Length::in), 
+			[will_not_call_mercury, thread_safe], "
+	System::Security::Cryptography::MD5 *md5;
+	ML_CastToMD5(md5, Ctxt);
+	md5->Write(Str);
+").
+
+:- pragma foreign_code("MC++",
+		md5final(Digest::out, Ctxt::in), 
+			[will_not_call_mercury, thread_safe], "
+	unsigned char d __gc[];
+
+	System::Security::Cryptography::MD5 *md5;
+	ML_CastToMD5(md5, Ctxt);
+	md5->CloseStream(Str);
+	d = md5->Hash(Str);
+
+	MR_list_nil(Digest);
+	for (i = 15; i >= 0; i--) {
+		MR_list_cons_msg(Digest, (MR_Integer) d[i], Digest);
+	}
+").
+
+
+:- pragma promise_pure(string__md5_bytes/1).
+
+string__md5_bytes(String) = Digest :-
+	impure allocate_context(Context),
+	impure md5init(Context),
+	impure md5update(Context, String, string__length(String)),
+	impure md5final(Digest, Context).
+
+
+string__md5(String) = string__md5_bytes_to_string(string__md5_bytes(String)).
+
+string__md5_bytes_to_string(MD5Result) =
+        string__to_lower(string__append_list(list__map(
+                (func(X) =
+                        ( X < 16 ->
+                                "0" ++ string__int_to_base_string(X, 16)
+                        ;
+                                string__int_to_base_string(X, 16)
+                        )
+                ), MD5Result)
+        )).
+
+
+
 
 %-----------------------------------------------------------------------------%
 
Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.109
diff -u -r1.109 Mmakefile
--- tests/hard_coded/Mmakefile	2001/01/07 03:05:46	1.109
+++ tests/hard_coded/Mmakefile	2001/01/27 22:38:17
@@ -68,6 +68,7 @@
 	impossible_unify \
 	impure_prune \
 	integer_test \
+	md5sum \
 	merge_and_remove_dups \
 	minint_bug \
 	mode_choice \
@@ -222,6 +223,10 @@
 			< $@.tmp > $@; \
 		rm -f $@.tmp; \
 	fi
+
+# We need to give a command line parameter to md5sum
+md5sum.out: md5sum
+	./md5sum md5sum.m > $@ 2>&1
 
 #-----------------------------------------------------------------------------#
 
Index: tests/hard_coded/md5sum.exp
===================================================================
RCS file: md5sum.exp
diff -N md5sum.exp
--- /dev/null	Tue Nov 21 11:53:28 2000
+++ md5sum.exp	Sun Jan 28 09:41:16 2001
@@ -0,0 +1 @@
+c30b5fd076c7b198cf4930092f7d160b  md5sum.m
Index: tests/hard_coded/md5sum.m
===================================================================
RCS file: md5sum.m
diff -N md5sum.m
--- /dev/null	Tue Nov 21 11:53:28 2000
+++ md5sum.m	Sun Jan 28 09:41:33 2001
@@ -0,0 +1,46 @@
+% This module provides a test case for the string__md5 code.
+% It calculates the md5sum of the files given on the command line.
+
+:- module md5sum.
+
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+:- import_module string, int, require, list.
+
+main -->
+	io__command_line_arguments(Args),
+	foldl(process_files(md5file), Args).
+
+:- pred process_files(pred(string, io__state, io__state),
+		string, io__state, io__state) is det.
+:- mode process_files(pred(in, di, uo) is det, in, di, uo) is det.
+
+process_files(Pred, FileName) -->
+	io__see(FileName, Res),
+	( { Res = ok } ->
+		Pred(FileName)
+	;
+		{ error("error opening file") }
+	).
+
+
+:- pred md5file(string::in, io__state::di, io__state::uo) is det.
+
+md5file(FileName) -->
+	io__read_file_as_string(Res, Str),
+	( { Res = ok } ->
+		io__write_string(string__md5(Str)),
+		io__write_string("  "),
+		io__write_string(FileName)
+	;			
+		{ error("error reading file") }
+	),
+	io__nl.
+	


-- 
       Tyson Dowd           # 
                            #  Surreal humour isn't everyone's cup of fur.
     trd at cs.mu.oz.au        # 
http://www.cs.mu.oz.au/~trd #
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list