Rename get_dir_size() to get_dir_count()
[libi2ncommon] / src / filefunc.cpp
index a760d3d..e1e4882 100644 (file)
@@ -18,7 +18,7 @@ This exception does not invalidate any other reasons why a work based
 on this file might be covered by the GNU General Public License.
 */
 /***************************************************************************
-                          escape.cpp  -  escaping of strings
+                          filefunc.cpp  -  functions for working with FS
                              -------------------
     begin                : Sun Nov 14 1999
     copyright            : (C) 1999 by Intra2net AG
@@ -308,6 +308,7 @@ bool get_dir(
     std::vector< std::string >& result,
     bool include_dot_names )
 {
+    // code copied to get_dir_count; keep in sync
     DIR* dir = ::opendir( path.c_str());
     if (!dir)
     {
@@ -342,6 +343,32 @@ std::vector< std::string > get_dir(const std::string& path, bool include_dot_nam
 } // eo get_dir(const std::string&,bool)
 
 
+/**
+ * @brief count entries in directory, like get_dir(path, include_dot_names).size()
+ * @param path the path to the directory whose contents should be read.
+ * @param include_dot_names determines if dot-files should be included in the count.
+ * @return the number of entries in the directory; return -1 in case of error
+ */
+int get_dir_count(const std::string& path, bool include_dot_names)
+{
+    // code is a simplified copy of get_dir, above. Keep in sync
+    int result = 0;
+    DIR* dir = ::opendir( path.c_str());
+    if (!dir)
+        return -1;
+    struct dirent store, *entry = NULL;
+    while (readdir_r(dir, &store, &entry) == 0 && entry != NULL)
+    {
+        if (entry->d_name == NULL)
+            continue;    // should not happen
+        else if (! include_dot_names && (entry->d_name)[0] == '.')
+            continue;
+        ++result;
+    }
+    ::closedir(dir);
+    return result;
+}
+
 
 /**
  * @brief removes a file from a filesystem.
@@ -802,10 +829,10 @@ bool recursive_delete(const std::string &path,
 /**
     Create a unique temporary directory from path_template.
     @param Path template. The last six characters must be XXXXXX.
-    @param error Will contain the error if the return value is false [optional]
+    @param error Will contain the error if the return value is empty [optional]
     @return Name of new directory or empty string on error.
 
-    @seealso: classes in tmpfstream which offers funktionality based on mkstemp
+    @seealso: classes in tmpfstream which offer functionality based on mkstemp
 */
 std::string mkdtemp(const std::string &path_template, std::string *error)
 {