openrave.org

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.cpp
Go to the documentation of this file.
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2006-2012 Rosen Diankov <rosen.diankov@gmail.com>
3 //
4 // This file is part of OpenRAVE.
5 // OpenRAVE is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "libopenrave.h"
18 #include <openrave/utils.h>
19 
20 #include "md5.h"
21 
22 namespace OpenRAVE {
23 namespace utils {
24 
25 std::string GetMD5HashString(const std::string& s)
26 {
27  if( s.size() == 0 )
28  return "";
29 
30  md5_state_t state;
31  md5_byte_t digest[16];
32 
33  md5_init(&state);
34  md5_append(&state, (const md5_byte_t *)s.c_str(), s.size());
35  md5_finish(&state, digest);
36  string hex_output;
37  hex_output.resize(32);
38  for (int di = 0; di < 16; ++di) {
39  int n = (digest[di]&0xf);
40  hex_output[2*di+1] = n > 9 ? ('a'+n-10) : ('0'+n);
41  n = (digest[di]&0xf0)>>4;
42  hex_output[2*di+0] = n > 9 ? ('a'+n-10) : ('0'+n);
43  }
44  return hex_output;
45 }
46 
47 std::string GetMD5HashString(const std::vector<uint8_t>&v)
48 {
49  if( v.size() == 0 )
50  return "";
51 
52  md5_state_t state;
53  md5_byte_t digest[16];
54 
55  md5_init(&state);
56  md5_append(&state, (const md5_byte_t *)&v[0], v.size());
57  md5_finish(&state, digest);
58  string hex_output;
59  hex_output.resize(32);
60  for (int di = 0; di < 16; ++di) {
61  int n = (digest[di]&0xf);
62  hex_output[2*di+0] = n > 9 ? ('a'+n-10) : ('0'+n);
63  n = (digest[di]&0xf0)>>4;
64  hex_output[2*di+1] = n > 9 ? ('a'+n-10) : ('0'+n);
65  }
66  return hex_output;
67 }
68 
69 bool PairStringLengthCompare(const std::pair<std::string, std::string>&p0, const std::pair<std::string, std::string>&p1)
70 {
71  return p0.first.size() > p1.first.size();
72 }
73 
74 std::string& SearchAndReplace(std::string& out, const std::string& in, const std::vector< std::pair<std::string, std::string> >&_pairs)
75 {
76  BOOST_ASSERT(&out != &in);
77  FOREACHC(itp,_pairs) {
78  BOOST_ASSERT(itp->first.size()>0);
79  }
80  std::vector< std::pair<std::string, std::string> > pairs = _pairs;
81  stable_sort(pairs.begin(),pairs.end(),PairStringLengthCompare);
82  out.resize(0);
83  size_t startindex = 0;
84  while(startindex < in.size()) {
85  size_t nextindex=std::string::npos;
86  std::vector< std::pair<std::string, std::string> >::const_iterator itbestp;
87  FOREACHC(itp,pairs) {
88  size_t index = in.find(itp->first,startindex);
89  if((nextindex == std::string::npos)|| ((index != std::string::npos)&&(index < nextindex)) ) {
90  nextindex = index;
91  itbestp = itp;
92  }
93  }
94  if( nextindex == std::string::npos ) {
95  out += in.substr(startindex);
96  break;
97  }
98  out += in.substr(startindex,nextindex-startindex);
99  out += itbestp->second;
100  startindex = nextindex+itbestp->first.size();
101  }
102  return out;
103 }
104 
105 std::string GetFilenameUntilSeparator(std::istream& sinput, char separator)
106 {
107  std::string filename;
108  if( !getline(sinput, filename, separator) ) {
109  // just input directly
110  sinput >> filename;
111  }
112 
113  // trim leading spaces
114  std::size_t startpos = filename.find_first_not_of(" \t");
115  std::size_t endpos = filename.find_last_not_of(" \t");
116 
117  // if all spaces or empty return an empty string
118  if( string::npos == startpos || string::npos == endpos ) {
119  return "";
120  }
121  return filename.substr( startpos, endpos-startpos+1 );
122 }
123 
124 } // utils
125 } // OpenRAVE