Advertisement
upcFrost

dir2.patch

Sep 29th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 6.64 KB | None | 0 0
  1. --- octave-3.8.2-orig/libinterp/corefcn/dirfns.cc   2014-08-06 21:54:14.000000000 +0400
  2. +++ octave-3.8.2/libinterp/corefcn/dirfns.cc    2014-10-02 18:07:29.256491360 +0400
  3. @@ -800,3 +800,196 @@
  4.  {
  5.    return SET_INTERNAL_VARIABLE (confirm_recursive_rmdir);
  6.  }
  7. +
  8. +
  9. +DEFUN (dir2, args, nargout,
  10. +"-*- texinfo -*-\n\
  11. +@deftypefn  {Function File} {} dir2\n\
  12. +@deftypefnx {Function File} {} dir2 (@var{directory})\n\
  13. +@deftypefnx {Function File} {[@var{list}] =} dir2 (@var{directory})\n\
  14. +Display file listing for directory @var{directory}.\n\
  15. +\n\
  16. +If @var{directory} is not specified then list the present working directory.\n\
  17. +\n\
  18. +If a return value is requested, return a structure array with the fields\n\
  19. +\n\
  20. +@table @asis\n\
  21. +@item name\n\
  22. +File or directory name.\n\
  23. +\n\
  24. +@item date\n\
  25. +Timestamp of file modification (string value).\n\
  26. +\n\
  27. +@item bytes\n\
  28. +File size in bytes.\n\
  29. +\n\
  30. +@item isdir\n\
  31. +True if name is a directory.\n\
  32. +\n\
  33. +@item datenum\n\
  34. +Timestamp of file modification as serial date number (double).\n\
  35. +\n\
  36. +@item statinfo\n\
  37. +Information structure returned from @code{stat}.\n\
  38. +@end table\n\
  39. +\n\
  40. +If @var{directory} is a filename, rather than a directory, then return\n\
  41. +information about the named file.  @var{directory} may also be a list rather\n\
  42. +than a single directory or file.\n\
  43. +\n\
  44. +@var{directory} is subject to shell expansion if it contains any wildcard\n\
  45. +characters @samp{*}, @samp{?}, @samp{[]}.  If you want to find a\n\
  46. +literal example of a wildcard character you must escape it using the\n\
  47. +backslash operator @samp{\}.\n\
  48. +\n\
  49. +Note that for symbolic links, @code{dir} returns information about\n\
  50. +the file that the symbolic link points to rather than the link itself.\n\
  51. +However, if the link points to a nonexistent file, @code{dir} returns\n\
  52. +information about the link.\n\
  53. +@seealso{ls, readdir, glob, what, stat}\n\
  54. +@end deftypefn")
  55. +/*
  56. +       "-*- texinfo -*-\n\
  57. +@deftypefn  {Built-in Function} {@var{files} =} dir2 (@var{dir})\n\
  58. +@deftypefnx {Built-in Function} {[@var{files}, @var{err}, @var{msg}] =} dir2 (@var{dir})\n\
  59. +Return the names of files in the directory @var{dir} as a cell array of\n\
  60. +strings.\n\
  61. +\n\
  62. +If an error occurs, return an empty cell array in @var{files}.\n\
  63. +If successful, @var{err} is 0 and @var{msg} is an empty string.\n\
  64. +Otherwise, @var{err} is nonzero and @var{msg} contains a\n\
  65. +system-dependent error message.\n\
  66. +@seealso{ls, dir, glob, what}\n\
  67. +@end deftypefn") */
  68. +{
  69. +  octave_value_list retval;
  70. +  octave_value_list Fstat(const octave_value_list &args, int);
  71. +  
  72. +  const char* date_fmt = "%d-%b-%Y %T";
  73. +
  74. +  string_vector header (6);
  75. +  header(0) = "name";
  76. +  header(1) = "date";
  77. +  header(2) = "bytes";
  78. +  header(3) = "isdir";
  79. +  header(4) = "datenum";
  80. +  header(5) = "statinfo";
  81. +
  82. +  retval(2) = std::string ();
  83. +  retval(1) = -1.0;
  84. +  retval(0) = octave_map(dim_vector(0,1), header);
  85. +  
  86. +  string_vector dir_list;
  87. +  octave_idx_type dir_len;
  88. +  file_stat stat;
  89. +
  90. +  if (args.length() <= 1)
  91. +    {
  92. +      std::string dirname = (args.length() == 0) ? "." : args(0).string_value();
  93. +
  94. +      // dir2('*') == dir2('.')
  95. +      if (strcmp(dirname.c_str(), "*") == 0) {
  96. +        dirname = ".";
  97. +      }
  98. +
  99. +      // Get dir length
  100. +      if (strcmp(dirname.c_str(), ".") == 0) {
  101. +        dir_len = 1;
  102. +      } else {
  103. +        glob_match pattern (file_ops::tilde_expand (dirname));
  104. +        dir_list = pattern.glob();
  105. +        dir_len = dir_list.length();
  106. +      }
  107. +      
  108. +      // If glob returned only 1 result - check if it's a dir
  109. +      if (dir_len == 1) {
  110. +        stat.get_stats(dirname);
  111. +        // If it is - get its file list
  112. +        if (stat.is_dir()) {
  113. +          dir_list = dir_entry(dirname).read().sort();
  114. +          dir_len = dir_list.length();
  115. +        }
  116. +      }
  117. +      
  118. +      // Create a dim vector and a map based on it
  119. +      dim_vector map_dim = dim_vector(dir_len, 1);
  120. +      octave_map map = octave_map(map_dim, header);
  121. +      // Assign cell ref to each field
  122. +      Cell& m_name = map.contents(header.elem(0));
  123. +      Cell& m_date = map.contents(header.elem(1));
  124. +      Cell& m_bytes = map.contents(header.elem(2));
  125. +      Cell& m_isdir = map.contents(header.elem(3));
  126. +      Cell& m_datenum = map.contents(header.elem(4));
  127. +      Cell& m_statinfo = map.contents(header.elem(5));
  128. +      // File stat and time vars
  129. +      octave_localtime lt;
  130. +
  131. +      // Time variables
  132. +      double y, m, d;
  133. +      
  134. +      // Populate value lists
  135. +      for (int i = 0; i < dir_len; i++) {
  136. +        // Get file info
  137. +        stat.get_stats(dirname + "/" + dir_list.elem(i));
  138. +        lt = octave_localtime(stat.mtime().double_value());
  139. +        // Populate each list
  140. +        m_name(i) = dir_list.elem(i);
  141. +        m_date(i) = lt.strftime(date_fmt);
  142. +        m_bytes(i) = stat.size();
  143. +        m_isdir(i) = stat.is_dir() ? 1 : 0;
  144. +        // Next is the Rata Die algorithm
  145. +        // (http://mysite.verizon.net/aesir_research/date/rata.htm)
  146. +        // But here i should add 366 days,
  147. +        // because it counts from 01-01-0001, while MatLAB uses 00-00-0000 (srsly, that's not even a day O_o)
  148. +        if (lt.mon() < 3) {
  149. +          y = lt.year() + 1899;
  150. +          m = lt.mon() + 12;
  151. +        } else {
  152. +          y = lt.year() + 1900;
  153. +          m = lt.mon();
  154. +        }
  155. +        d = lt.mday();
  156. +        // And the main formula
  157. +        m_datenum(i) = gnulib::round(
  158. +          365*y + y/4 - y/100 + y/400 + // Year
  159. +          (153*m - 457)/5 + // Month
  160. +          d - 306 + 366); // Day
  161. +        
  162. +        // For statinfo i'm using the Fstat function result's (0) member, just like for the internal stat.
  163. +        m_statinfo(i) = Fstat(octave_value (dirname + "/" + dir_list.elem(i)), 0) (0);
  164. +      }
  165. +
  166. +      // Return the map
  167. +      retval(1) = 0.0;
  168. +      retval(0) = map;
  169. +        // Check if we need to return smth or just to show the dir_names
  170. +      if (nargout < 1)
  171. +      {
  172. +        // Check if our result is empty
  173. +        if (map.is_empty())
  174. +        {
  175. +          #ifndef PATH_MAX
  176. +            char buff[4096];
  177. +          #else
  178. +            char buff[PATH_MAX];
  179. +          #endif
  180. +          sprintf(buff, "\ndir2: non-existent directory %s\n", dirname.c_str());
  181. +          retval(0) = buff;
  182. +        }
  183. +        else
  184. +        {
  185. +          // Create a nice-looking list
  186. +          std::ostringstream buf;
  187. +          buf.put('\n');
  188. +          string_vector s = string_vector(map.contents(0).cellstr_value());
  189. +          s.list_in_columns(buf);
  190. +          
  191. +          retval(0) = buf.str();
  192. +        }
  193. +      }
  194. +    }
  195. +  else
  196. +    print_usage ();
  197. +
  198. +  return retval;
  199. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement