/tags/rel-0-1-2/FreeSpeech/data-flow/src/PathList.cc

# · C++ · 136 lines · 81 code · 29 blank · 26 comment · 19 complexity · 4b3028e2d488de580a72c656e934f0dd MD5 · raw file

  1. // Copyright (C) 1999 Dominic Letourneau
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but
  9. // WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this file. If not, write to the Free Software Foundation,
  15. // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. #include "PathList.h"
  17. #include "Object.h"
  18. #include "ObjectRef.h"
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <dirent.h>
  22. //DECLARE_NODE(PathList)
  23. NODE_INFO(PathList,"IO", "PATH:EXTENSION", "FULLNAME_OUT:FILENAME_OUT", "")
  24. PathList::PathList(string nodeName, ParameterSet params)
  25. : Node(nodeName, params)
  26. , path (object_cast<String>(parameters.get("PATH")))
  27. , extension (object_cast<String>(parameters.get("EXTENSION"))) {
  28. try {
  29. fullPathID = addOutput("FULLNAME_OUT");
  30. fileNameID = addOutput("FILENAME_OUT");
  31. //Let's read the path
  32. ReadPath();
  33. }
  34. catch (BaseException *e) {
  35. //e->print();
  36. throw e->add(new NodeException (NULL,"Caught Exception in PathList Constructor",__FILE__,__LINE__));
  37. }
  38. }
  39. ObjectRef PathList::getOutput (int output_id, int count) {
  40. if (!hasOutput(output_id)) throw new NodeException (this, "Cannot getOutput id",__FILE__,__LINE__);
  41. if (count != processCount) {
  42. //We are updating our output only if needed
  43. try {
  44. if (fileQueue.empty()) {
  45. //returning an invalid object
  46. fullName = ObjectRef(new Object(Object::nil));
  47. shortName = ObjectRef (new Object(Object::nil));
  48. }
  49. else {
  50. //returning the file
  51. fullName = ObjectRef (new String(fileQueue.front()));
  52. shortName = ObjectRef(new String(removePath(fileQueue.front())));
  53. fileQueue.pop();
  54. }
  55. //updating processCount
  56. processCount = count;
  57. } //end of the try block
  58. catch (GenericCastException *e) {
  59. //We had a problem casting, our inputs are invalid?
  60. e->print();
  61. fullName = ObjectRef(new Object(Object::nil));
  62. shortName = ObjectRef(new Object(Object::nil));
  63. }
  64. catch (BaseException *e) {
  65. //Something weird happened
  66. //e->print();
  67. throw e->add(new NodeException (this,string("Error!!! "), __FILE__,__LINE__));
  68. }
  69. }
  70. if (output_id == fileNameID) {
  71. return shortName;
  72. }
  73. else {
  74. return fullName;
  75. }
  76. }
  77. void PathList::ReadPath() {
  78. DIR *my_directory;
  79. struct dirent *current_entry;
  80. my_directory = opendir (path.c_str());
  81. if (my_directory == NULL) {
  82. throw new NodeException(this,string("Invalid Path : ") + path,__FILE__,__LINE__);
  83. }
  84. for (current_entry = readdir(my_directory);
  85. current_entry != NULL; current_entry = readdir(my_directory)) {
  86. if (string(current_entry->d_name).find(extension) != string::npos) {
  87. if (debugMode) {
  88. cerr<<"Adding : "<<current_entry->d_name<<endl;
  89. }
  90. fileQueue.push(path + string(current_entry->d_name));
  91. }
  92. }
  93. closedir(my_directory);
  94. }
  95. string PathList::removePath(const string &fullName) {
  96. string output;
  97. for (int i = fullName.size() - 1; i >= 0; i--) {
  98. if (fullName[i] != '/') {
  99. //inserting at the beginning
  100. output.insert(output.begin(),fullName[i]);
  101. }
  102. else {
  103. break;
  104. }
  105. }
  106. return output;
  107. }