detect_power_meters() has the same /proc/* directory traverse code as network
and ethernet_tunable did before we switched to callback. Move read_all_nics()
code to process_directory(), which now accepts 2 parameters -- directory to 
read and callback; introduce power_meters_callback() to fill power_meters.

Signed-off-by: Sergey Senozhatsky <[email protected]>

---

 devices/network.cpp         |   24 +-----------------------
 lib.cpp                     |   21 +++++++++++++++++++++
 lib.h                       |    1 +
 measurement/measurement.cpp |   29 ++++++++---------------------
 4 files changed, 31 insertions(+), 44 deletions(-)

diff --git a/devices/network.cpp b/devices/network.cpp
index 9be4401..5f3ddf2 100644
--- a/devices/network.cpp
+++ b/devices/network.cpp
@@ -327,28 +327,6 @@ const char * network::device_name(void)
        return name;
 }
 
-void read_all_nics(callback fn)
-{
-       struct dirent *entry;
-       DIR *dir;
-       dir = opendir("/sys/class/net/");
-       if (!dir)
-               return;
-       while (1) {
-               entry = readdir(dir);
-               if (!entry)
-                       break;
-               if (entry->d_name[0] == '.')
-                       continue;
-               if (strcmp(entry->d_name, "lo")==0)
-                       continue;
-
-               fn(entry->d_name);
-       }
-
-       closedir(dir);
-}
-
 void netdev_callback(const char *d_name)
 {
        std::string f_name("/sys/class/net/");
@@ -366,7 +344,7 @@ void create_all_nics(callback fn)
 {
        if (!fn)
                fn = &netdev_callback;
-       read_all_nics(fn);
+       process_directory("/sys/class/net/", fn);
 }
 
 double network::power_usage(struct result_bundle *result, struct 
parameter_bundle *bundle)
diff --git a/lib.cpp b/lib.cpp
index 162baa0..103c8ff 100644
--- a/lib.cpp
+++ b/lib.cpp
@@ -372,4 +372,25 @@ int equals(double a, double b)
        return fabs(a - b) <= std::numeric_limits<double>::epsilon();
 }
 
+void process_directory(const char *d_name, callback fn)
+{
+       struct dirent *entry;
+       DIR *dir;
+       dir = opendir(d_name);
+       if (!dir)
+               return;
+       while (1) {
+               entry = readdir(dir);
+               if (!entry)
+                       break;
+               if (entry->d_name[0] == '.')
+                       continue;
+               if (strcmp(entry->d_name, "lo")==0)
+                       continue;
+
+               fn(entry->d_name);
+       }
+
+       closedir(dir);
+}
 
diff --git a/lib.h b/lib.h
index 273c875..f4e6889 100644
--- a/lib.h
+++ b/lib.h
@@ -72,6 +72,7 @@ extern char *pretty_print(const char *str, char *buf, int 
len);
 extern int equals(double a, double b);
 
 typedef void (*callback)(const char*);
+extern void process_directory(const char *d_name, callback fn);
 extern int utf_ok;
 
 #endif
diff --git a/measurement/measurement.cpp b/measurement/measurement.cpp
index 1a67363..3dbaab9 100644
--- a/measurement/measurement.cpp
+++ b/measurement/measurement.cpp
@@ -103,30 +103,17 @@ double global_time_left(void)
        return total;
 }
 
-
-void detect_power_meters(void)
+void power_meters_callback(const char *d_name)
 {
-       DIR *dir;
-       struct dirent *entry;
-
-       dir = opendir("/proc/acpi/battery");
-       if (!dir)
-               return;
-       while (1) {
-               class acpi_power_meter *meter;
-               entry = readdir(dir);
-               if (!entry)
-                       break;
-               if (entry->d_name[0] == '.')
-                       continue;
-
-               meter = new class acpi_power_meter(entry->d_name);
-
+       class acpi_power_meter *meter;
+       meter = new(std::nothrow) class acpi_power_meter(d_name);
+       if (meter)
                power_meters.push_back(meter);
-               
-       }
-       closedir(dir);
+}
 
+void detect_power_meters(void)
+{
+       process_directory("/proc/acpi/battery", power_meters_callback);
 }
 
 void extech_power_meter(const char *devnode)

_______________________________________________
Discuss mailing list
[email protected]
http://lists.lesswatts.org/listinfo/discuss

Reply via email to