source: trunk/shellenv.cpp @ 4

Last change on this file since 4 was 4, checked in by willem, 11 years ago

willem

File size: 1.4 KB
Line 
1/*
2   Copyright 2012 Willem Vermin, SARA
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */
16#include <string>
17#include "shellenv.h"
18//
19// Example:
20//   shellenv("aap noot mies", "LEESPLANK", SHELL_SH);
21// returns the string:
22//   LEESPLANK='aap noot mies';export LEESPLANK;
23//
24//  "'" characters in s are escaped
25//
26
27std::string shellenv(std::string s, const std::string name, int shelltype)
28{
29  std::string r = "'";
30  std::string::iterator it;
31  for ( it=s.begin() ; it < s.end(); it++ )
32  {
33    if ( *it == '\'')
34    {
35      r.push_back('\'');
36      r.push_back('\\');
37      r.push_back('\'');
38      r.push_back('\'');
39    }
40    else
41      r.push_back(*it);
42  }
43  r.push_back('\'');
44  switch (shelltype)
45  {
46    case SHELL_CSH:
47      r = "setenv " + name + " " + r + ";\n";
48      break;
49    case SHELL_SH: 
50      r = name + "=" + r + ";" + "export " + name + ";\n";
51      break;
52  }
53  return r;
54}
Note: See TracBrowser for help on using the repository browser.