Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r169:fe2fdb378e56
Date: 2014-12-05 17:15 +0100
http://bitbucket.org/cffi/creflect/changeset/fe2fdb378e56/

Log:    Demo files (direct ports from hg/cffi/demo)

diff --git a/demo/gmp.crx b/demo/gmp.crx
new file mode 100644
--- /dev/null
+++ b/demo/gmp.crx
@@ -0,0 +1,13 @@
+#include <gmp.h>
+
+
+/* CREFLECT: start */
+
+typedef struct { } MP_INT;
+typedef MP_INT mpz_t[1];
+
+int mpz_init_set_str (MP_INT *dest_integer, char *src_cstring, int base);
+void mpz_add (MP_INT *sum, MP_INT *addend1, MP_INT *addend2);
+char * mpz_get_str (char *string, int base, MP_INT *integer);
+
+/* CREFLECT: end */
diff --git a/demo/gmp.py b/demo/gmp.py
new file mode 100644
--- /dev/null
+++ b/demo/gmp.py
@@ -0,0 +1,15 @@
+import sys
+import zeffir
+
+ffi = zeffir.FFI()
+lib = ffi.load_library("gmp-creflect", relative_to=__file__)
+
+a = ffi.new("mpz_t")
+b = ffi.new("mpz_t")
+
+lib.mpz_init_set_str(a, sys.argv[1], 10)       # Assume decimal integers
+lib.mpz_init_set_str(b, sys.argv[2], 10)       # Assume decimal integers
+lib.mpz_add(a, a, b)                   # a=a+b
+
+s = lib.mpz_get_str(ffi.NULL, 10, a)
+print ffi.string(s)
diff --git a/demo/readdir2.crx b/demo/readdir2.crx
new file mode 100644
--- /dev/null
+++ b/demo/readdir2.crx
@@ -0,0 +1,29 @@
+#ifndef _ATFILE_SOURCE
+#  define _ATFILE_SOURCE
+#endif
+#ifndef _BSD_SOURCE
+#  define _BSD_SOURCE
+#endif
+#include <fcntl.h>
+#include <sys/types.h>
+#include <dirent.h>
+
+
+/* CREFLECT: start */
+
+typedef ... DIR;
+
+struct dirent {
+    unsigned char  d_type;      /* type of file; not supported
+                                   by all file system types */
+    char           d_name[];    /* filename */
+};
+
+int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
+int openat(int dirfd, const char *pathname, int flags);
+DIR *fdopendir(int fd);
+int closedir(DIR *dirp);
+
+#define DT_DIR ...
+
+/* CREFLECT: end */
diff --git a/demo/readdir2.py b/demo/readdir2.py
new file mode 100644
--- /dev/null
+++ b/demo/readdir2.py
@@ -0,0 +1,32 @@
+import zeffir
+
+ffi = zeffir.FFI()
+lib = ffi.load_library("readdir2-creflect", relative_to=__file__)
+
+p = ffi.new("struct dirent *")
+print p
+
+
+def walk(basefd, path):
+    print '{', path
+    dirfd = lib.openat(basefd, path, 0)
+    if dirfd < 0:
+        # error in openat()
+        return
+    dir = lib.fdopendir(dirfd)
+    dirent = ffi.new("struct dirent *")
+    result = ffi.new("struct dirent **")
+    while True:
+        if lib.readdir_r(dir, dirent, result):
+            # error in readdir_r()
+            break
+        if result[0] == ffi.NULL:
+            break
+        name = ffi.string(dirent.d_name)
+        print '%3d %s' % (dirent.d_type, name)
+        if dirent.d_type == lib.DT_DIR and name != '.' and name != '..':
+            walk(dirfd, name)
+    lib.closedir(dir)
+    print '}'
+
+walk(-1, "/tmp")
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to