Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The correction of a comment
[simgrid.git] / tools / tesh2 / src / directory.c
index ea93e7f..ac6bf4b 100644 (file)
-#include <directory.h>
-#include <fstreams.h>
-#include <fstream.h>
-
-XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
-
-directory_t
-directory_new(const char* name)
-{
-       directory_t directory;
-       
-       if(!name)
-       {
-               errno = EINVAL;
-               return NULL;
-       }
-       
-       directory = xbt_new0(s_directory_t, 1);
-       
-       
-       directory->name = strdup(name); 
-       
-               
-       directory->stream = NULL;
-       
-       return directory;
-}
-
-int
-directory_open(directory_t directory)
-{
-       if(!directory || directory->stream)
-               return EINVAL;
-               
-       if(!(directory->stream = opendir(directory->name)))
-               return errno;
-       
-       return 0;       
-}
-
-
-int
-directory_close(directory_t directory)
-{
-       if(!directory)
-               return EINVAL;
-               
-       if(!directory->stream)
-               return EBADF;
-               
-       if(closedir(directory->stream))
-               return errno;
-               
-       directory->stream = NULL;
-       return 0;
-}
-
-int
-directory_load(directory_t directory, fstreams_t fstreams, lstrings_t suffixes)
-{
-       struct dirent* entry ={0};
-       s_fstream_t sfstream = {0};
-       const char* suffix;
-       int has_valid_suffix;
-       int is_empty = 1;
-       int rv;
-       
-       if(!directory || !fstreams)
-               return EINVAL;
-               
-       if(!directory->stream)
-               return EBADF;
-               
-       sfstream.directory = strdup(directory->name);
-               
-       while((entry = readdir(directory->stream)))
-       {
-               has_valid_suffix = 0;
-               
-               lstrings_rewind(suffixes);
-               
-               while((suffix = lstrings_get(suffixes)))
-               {
-                       if(!strncmp(suffix, entry->d_name + (strlen(entry->d_name) - strlen(suffix)), strlen(suffix)))
-                       {
-                               has_valid_suffix = 1;
-                               break;
-                       }
-                               
-                       lstrings_move_next(suffixes);   
-               }
-               
-               if(!has_valid_suffix)
-                       continue;
-                       
-               sfstream.name = strdup(entry->d_name);
-               
-               /* check first if the file stream is already in the file streams to run */
-               if(fstreams_contains(fstreams, &sfstream))
-               {
-                       WARN1("file %s already registred", entry->d_name);
-                       free(sfstream.name);
-                       continue;
-               }
-               
-               /* add the fstream to the list of file streams to run */
-               if((rv = fstreams_add(fstreams, fstream_new(directory->name, entry->d_name))))
-               {
-                       INFO0("fstreams_add() failed");
-                       free(sfstream.directory);
-                       free(sfstream.name);
-                       return rv;
-               }
-                       
-               is_empty = 0;
-               free(sfstream.name);
-       }
-       
-       if(is_empty)
-               WARN1("no tesh file found in the directory %s", directory->name);       
-               
-       free(sfstream.directory);
-       
-                       
-       return 0;       
-}
-
-int
-directory_free(void** directoryptr)
-{
-       directory_t directory;
-       
-       if(!(*directoryptr))
-               return EINVAL;
-               
-       directory = *((directory_t*)directoryptr);
-       
-       if(directory->stream)
-               if(directory_close(directory))
-                       return errno;
-       
-       free(directory->name);
-       
-       free(*directoryptr);
-       *directoryptr  = NULL;
-       
-       return 0;       
-}
-
-const char*
-directory_get_name(directory_t directory)
-{
-       if(!directory)
-       {
-               errno = EINVAL;
-               return NULL;
-       }
-       
-       return (const char*)directory->name;
-}
+#include <directory.h>\r
+#include <fstreams.h>\r
+#include <fstream.h>\r
+\r
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);\r
+\r
+directory_t\r
+directory_new(const char* name)\r
+{\r
+       directory_t directory;\r
+       \r
+       if(!name)\r
+       {\r
+               errno = EINVAL;\r
+               return NULL;\r
+       }\r
+       \r
+       directory = xbt_new0(s_directory_t, 1);\r
+       \r
+       \r
+       directory->name = strdup(name); \r
+       \r
+               \r
+       directory->stream = NULL;\r
+       \r
+       return directory;\r
+}\r
+\r
+int\r
+directory_open(directory_t directory)\r
+{\r
+       if(!directory || directory->stream)\r
+               return EINVAL;\r
+               \r
+       if(!(directory->stream = opendir(directory->name)))\r
+               return errno;\r
+       \r
+       return 0;       \r
+}\r
+\r
+\r
+int\r
+directory_close(directory_t directory)\r
+{\r
+       if(!directory)\r
+               return EINVAL;\r
+               \r
+       if(!directory->stream)\r
+               return EBADF;\r
+               \r
+       if(closedir(directory->stream))\r
+               return errno;\r
+               \r
+       directory->stream = NULL;\r
+       return 0;\r
+}\r
+\r
+int\r
+directory_load(directory_t directory, fstreams_t fstreams, xbt_dynar_t suffixes)\r
+{\r
+       struct dirent* entry ={0};\r
+       s_fstream_t sfstream = {0};\r
+       char* suffix;\r
+       unsigned int i;\r
+       int has_valid_suffix;\r
+       int is_empty = 1;\r
+       int rv;\r
+       \r
+       if(!directory || !fstreams)\r
+               return EINVAL;\r
+               \r
+       if(!directory->stream)\r
+               return EBADF;\r
+               \r
+       sfstream.directory = strdup(directory->name);\r
+               \r
+       while((entry = readdir(directory->stream)))\r
+       {\r
+               has_valid_suffix = 0;\r
+               \r
+               xbt_dynar_foreach(suffixes, i, suffix)\r
+               {\r
+                       if(!strncmp(suffix, entry->d_name + (strlen(entry->d_name) - strlen(suffix)), strlen(suffix)))\r
+                       {\r
+                               has_valid_suffix = 1;\r
+                               break;\r
+                       }\r
+               }\r
+               \r
+               if(!has_valid_suffix)\r
+                       continue;\r
+                       \r
+               sfstream.name = strdup(entry->d_name);\r
+               \r
+               /* check first if the file stream is already in the file streams to run */\r
+               if(fstreams_contains(fstreams, &sfstream))\r
+               {\r
+                       WARN1("file %s already registred", entry->d_name);\r
+                       free(sfstream.name);\r
+                       continue;\r
+               }\r
+               \r
+               /* add the fstream to the list of file streams to run */\r
+               if((rv = fstreams_add(fstreams, fstream_new(directory->name, entry->d_name))))\r
+               {\r
+                       free(sfstream.directory);\r
+                       free(sfstream.name);\r
+                       return rv;\r
+               }\r
+                       \r
+               is_empty = 0;\r
+               free(sfstream.name);\r
+       }\r
+       \r
+       if(is_empty)\r
+               WARN1("no tesh file found in the directory %s", directory->name);       \r
+               \r
+       free(sfstream.directory);\r
+       \r
+                       \r
+       return 0;       \r
+}\r
+\r
+int\r
+directory_free(void** directoryptr)\r
+{\r
+       directory_t directory;\r
+       \r
+       if(!(*directoryptr))\r
+               return EINVAL;\r
+               \r
+       directory = *((directory_t*)directoryptr);\r
+       \r
+       if(directory->stream)\r
+               if(directory_close(directory))\r
+                       return errno;\r
+       \r
+       if(directory->name)\r
+               free(directory->name);\r
+       \r
+       free(*directoryptr);\r
+       *directoryptr  = NULL;\r
+       \r
+       return 0;       \r
+}\r
+\r
+const char*\r
+directory_get_name(directory_t directory)\r
+{\r
+       if(!directory)\r
+       {\r
+               errno = EINVAL;\r
+               return NULL;\r
+       }\r
+       \r
+       return (const char*)directory->name;\r
+}\r