Changeset 1432


Ignore:
Timestamp:
Dec 18, 2011, 6:36:40 PM (11 years ago)
Author:
Peter Johansson
Message:

implement file_name and directory_name. fixes #511

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/utility.cc

    r1430 r1432  
    117117
    118118
    119   std::string directory_name(std::string path)
    120   {
    121     return dirname(path.c_str());
    122   }
    123 
    124 
    125   std::string file_name(const std::string& full_path)
    126   {
    127     return basename(full_path.c_str());
     119  std::string directory_name(const std::string& path)
     120  {
     121    if (path.size()==1) {
     122      if (path=="/")
     123        return path;
     124      else
     125        return ".";
     126    }
     127    assert(path.size()>=2);
     128    size_t pos = path.find_last_of('/', path.size()-2);
     129    if (pos==std::string::npos)
     130      return ".";
     131    if (pos==0)
     132      return "/";
     133    return path.substr(0,pos);
     134    return "";
     135  }
     136
     137
     138  std::string file_name(const std::string& path)
     139  {
     140    if (path.size()==1)
     141      return path;
     142    size_t pos = path.find_last_of('/');
     143    if (pos==std::string::npos)
     144      return path;
     145    if (pos==path.size()-1)
     146      return file_name(path.substr(0, path.size()-1));
     147    return path.substr(pos+1);
    128148  }
    129149
  • trunk/lib/utility.h

    r1430 r1432  
    8989     \return directory of path.
    9090   */
    91   std::string directory_name(std::string path);
     91  std::string directory_name(const std::string& path);
    9292
    9393  ///
  • trunk/test/utility.cc

    r1164 r1432  
    3030#include <string>
    3131
     32void test_directory_name(theplu::svndigest::test::Suite& suite);
     33void test_file_name(theplu::svndigest::test::Suite& suite);
    3234bool test_hex(int, unsigned int, std::string);
    3335bool test_fnmatch(bool, std::string, std::string);
    34 bool test_regexp(bool, std::string, std::string, 
     36bool test_regexp(bool, std::string, std::string,
    3537                 const std::vector<std::string>&);
    3638
     
    3941  theplu::svndigest::test::Suite suite(argc, argv);
    4042  bool ok=true;
    41  
    42   ok &= test_hex(15,2, "0f"); 
    43   ok &= test_hex(17,1, "1"); 
    44   ok &= test_hex(16,2, "10"); 
    45 
    46   ok &= test_fnmatch(true,"peter", "peter"); 
    47   ok &= test_fnmatch(false,"peter", "peterj"); 
    48 
    49   ok &= test_fnmatch(true,"*", "peterj"); 
    50   ok &= test_fnmatch(true,"p*", "peterj"); 
    51   ok &= test_fnmatch(true, "p*", "peter"); 
    52   ok &= test_fnmatch(false, "p*j", "peter"); 
    53   ok &= test_fnmatch(true, "p*j", "peterj"); 
    54   ok &= test_fnmatch(true, "*peter", "peter"); 
    55 
    56   ok &= test_fnmatch(true, "p?ter", "peter"); 
    57   ok &= test_fnmatch(false, "p?er", "peter"); 
    58   ok &= test_fnmatch(false, "p?eter", "peter"); 
     43
     44  ok &= test_hex(15,2, "0f");
     45  ok &= test_hex(17,1, "1");
     46  ok &= test_hex(16,2, "10");
     47
     48  ok &= test_fnmatch(true,"peter", "peter");
     49  ok &= test_fnmatch(false,"peter", "peterj");
     50
     51  ok &= test_fnmatch(true,"*", "peterj");
     52  ok &= test_fnmatch(true,"p*", "peterj");
     53  ok &= test_fnmatch(true, "p*", "peter");
     54  ok &= test_fnmatch(false, "p*j", "peter");
     55  ok &= test_fnmatch(true, "p*j", "peterj");
     56  ok &= test_fnmatch(true, "*peter", "peter");
     57
     58  ok &= test_fnmatch(true, "p?ter", "peter");
     59  ok &= test_fnmatch(false, "p?er", "peter");
     60  ok &= test_fnmatch(false, "p?eter", "peter");
    5961
    6062  ok &= test_fnmatch(true, "filename", "filename");
     
    7779
    7880  std::vector<std::string> vec;
    79   ok &= test_regexp(true,"abcde", "abcde", vec); 
     81  ok &= test_regexp(true,"abcde", "abcde", vec);
    8082  vec.push_back("c");
    81   ok &= test_regexp(true,"ab?de", "abcde", vec); 
     83  ok &= test_regexp(true,"ab?de", "abcde", vec);
    8284  vec[0] = "bcd";
    83   ok &= test_regexp(true,"a*e", "abcde", vec); 
     85  ok &= test_regexp(true,"a*e", "abcde", vec);
    8486  vec.push_back("");
    85   ok &= test_regexp(true,"a*d*f", "abcddf", vec); 
     87  ok &= test_regexp(true,"a*d*f", "abcddf", vec);
    8688  vec[0] = "bc";
    8789  vec[1] = "ef";
    88   ok &= test_regexp(true,"a*d*g", "abcdefg", vec); 
     90  ok &= test_regexp(true,"a*d*g", "abcdefg", vec);
    8991  vec.push_back("");
    9092  vec[1]="e";
    9193  vec[2]="f";
    92   ok &= test_regexp(true,"a*d*?g", "abcdefg", vec); 
    93   ok &= test_regexp(true,"a*d??g", "abcdefg", vec); 
     94  ok &= test_regexp(true,"a*d*?g", "abcdefg", vec);
     95  ok &= test_regexp(true,"a*d??g", "abcdefg", vec);
    9496  vec.resize(2);
    9597  vec[0]="f";
     
    98100
    99101  suite.add(ok);
     102  test_directory_name(suite);
     103  test_file_name(suite);
    100104  return suite.exit_status();
    101105}
     106
     107
     108void test_directory_name(theplu::svndigest::test::Suite& suite)
     109{
     110  std::vector<std::string> path;
     111  path.push_back("/usr/lib");
     112  path.push_back("/usr/");
     113  path.push_back("usr");
     114  path.push_back("/");
     115  path.push_back(".");
     116  path.push_back("..");
     117  std::vector<std::string> dir;
     118  dir.push_back("/usr");
     119  dir.push_back("/");
     120  dir.push_back(".");
     121  dir.push_back("/");
     122  dir.push_back(".");
     123  dir.push_back(".");
     124  assert(dir.size()==path.size());
     125  using theplu::svndigest::directory_name;
     126  for (size_t i=0; i<dir.size(); ++i) {
     127    if (dir[i] != directory_name(path[i])) {
     128      suite.add(false);
     129      suite.out() << "error:\n";
     130      suite.out() << "path:           " << path[i] << "\n";
     131      suite.out() << "directory_name: " << directory_name(path[i]) << "\n";
     132      suite.out() << "expected:       " << dir[i] << "\n";
     133    }
     134  }
     135}
     136
     137
     138void test_file_name(theplu::svndigest::test::Suite& suite)
     139{
     140  std::vector<std::string> path;
     141  path.push_back("/usr/lib");
     142  path.push_back("/usr/");
     143  path.push_back("usr");
     144  path.push_back("/");
     145  path.push_back(".");
     146  path.push_back("..");
     147  std::vector<std::string> file;
     148  file.push_back("lib");
     149  file.push_back("usr");
     150  file.push_back("usr");
     151  file.push_back("/");
     152  file.push_back(".");
     153  file.push_back("..");
     154  assert(file.size()==path.size());
     155  using theplu::svndigest::file_name;
     156  for (size_t i=0; i<file.size(); ++i) {
     157    if (file[i] != file_name(path[i])) {
     158      suite.add(false);
     159      suite.out() << "error:\n";
     160      suite.out() << "path:           " << path[i] << "\n";
     161      suite.out() << "file_name:      " << file_name(path[i]) << "\n";
     162      suite.out() << "expected:       " << file[i] << "\n";
     163    }
     164  }
     165}
     166
    102167
    103168bool test_fnmatch(bool answ, std::string a, std::string b)
     
    126191    return true;
    127192  std::cerr << "hex(" << x << ", " << w << ") results "
    128             << theplu::svndigest::hex(x,w) << ". Expects " << facit 
     193            << theplu::svndigest::hex(x,w) << ". Expects " << facit
    129194            << std::endl;
    130195  return false;
    131196}
    132197
    133 bool test_regexp(bool ans, std::string a, std::string b, 
     198bool test_regexp(bool ans, std::string a, std::string b,
    134199                 const std::vector<std::string>& vec)
    135200{
     
    141206              << res << ". Expected " << ans << "\n"
    142207              << "resulting vector:\n";
    143     std::copy(v.begin(), v.end(), 
     208    std::copy(v.begin(), v.end(),
    144209              std::ostream_iterator<std::string>(std::cerr, "\n"));
    145210    std::cerr << "expected:\n";
    146     std::copy(vec.begin(), vec.end(), 
     211    std::copy(vec.begin(), vec.end(),
    147212              std::ostream_iterator<std::string>(std::cerr, "\n"));
    148213    return false;
Note: See TracChangeset for help on using the changeset viewer.