stdlib: Strings, modified concatenation operations (avoids creating a useless buffer)
[nit.git] / lib / standard / file_nit.c
index cf14118..56b26da 100644 (file)
@@ -2,6 +2,7 @@
  *
  * Copyright 2004-2008 Jean Privat <jean@pryen.org>
  * Copyright 2008 Floréal Morandat <morandat@lirmm.fr> 
+ * Copyright 2008 Jean-Sébastien Gélinas <calestar@gmail.com>
  *
  * This file is free software, which comes along with NIT.  This software is
  * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  * another product.
  */
 
-#define _POSIX_C_SOURCE 1
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <string.h>
+#include <dirent.h>
+#include <poll.h>
+
+#include "file_nit.h"
+
+#ifndef NONITCNI
+/*
+C implementation of file::String::files
+
+Imported methods signatures:
+       HashSet new_HashSet(  ) for hash_collection::HashSet::init
+       void HashSet_add( HashSet recv, Object item ) for hash_collection::HashSet::(abstract_collection::SimpleCollection::add)
+       String new_String_from_cstring( char * str ) for string::String::from_cstring
+       int HashSet_is_a_Set( HashSet value ) to check if a HashSet[String] is a Set[String]
+       Set HashSet_as_Set( HashSet value ) to cast from HashSet[String] to Set[String]
+*/
+Set String_files___impl( String recv )
+{
+       char *dir_path;
+       DIR *dir;
+
+       dir_path = String_to_cstring( recv );
+       if ((dir = opendir(dir_path)) == NULL)
+       {
+               perror( dir_path );
+               exit( 1 );
+       }
+       else
+       {
+               HashSet results;
+               String file_name;
+               struct dirent *de;
+
+               results = new_HashSet();
+
+               while ( ( de = readdir( dir ) ) != NULL )
+                       if ( strcmp( de->d_name, ".." ) != 0 &&
+                               strcmp( de->d_name, "." ) != 0 )
+                       {
+                               file_name = new_String_from_cstring( strdup( de->d_name ) );
+                               HashSet_add( results, String_as_Object( file_name ) );
+                       }
+
+               closedir( dir );
+               return HashSet_as_Set( results );
+       }
+}
+#endif
 
 int string_NativeString_NativeString_file_exists_0(char *f){
        FILE *hdl = fopen(f,"r");
@@ -28,22 +76,36 @@ int string_NativeString_NativeString_file_exists_0(char *f){
        return hdl != NULL;
 }
 
-static int to_nit_file_stat(struct stat* st){
+void *to_nit_file_stat(struct stat* st){
        struct stat* stat_element;
        stat_element = malloc(sizeof(struct stat));
-       return (int)memcpy(stat_element, st, sizeof(struct stat));
+       return memcpy(stat_element, st, sizeof(struct stat));
 }
 
-int string_NativeString_NativeString_file_stat_0(char *f){
+void *string_NativeString_NativeString_file_stat_0(char *f){
        struct stat buff;
        if(stat(f, &buff) != -1)
                return to_nit_file_stat(&buff);
        return 0;
 }
 
-int file_NativeFile_NativeFile_file_stat_0(FILE *f){
+void *file_NativeFile_NativeFile_file_stat_0(FILE *f){
        struct stat buff;
        if(fstat(fileno(f), &buff) != -1)
                return to_nit_file_stat(&buff);
        return 0;
 }
+
+extern int string_NativeString_NativeString_file_delete_0(char *f){
+  return (remove(f) == 0);
+}
+
+int file_stdin_poll_in_(void) {
+       struct pollfd fd = {0, POLLIN, 0};
+       int res = poll(&fd, 1, 0);
+       if (res == -1) {
+               perror("Error poll stdin");
+               exit(EXIT_FAILURE);
+       }
+       return res > 0;
+}