1 //==============================================================================
2 //
3 //  This file is part of GPSTk, the GPS Toolkit.
4 //
5 //  The GPSTk is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU Lesser General Public License as published
7 //  by the Free Software Foundation; either version 3.0 of the License, or
8 //  any later version.
9 //
10 //  The GPSTk 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
16 //  License along with GPSTk; if not, write to the Free Software Foundation,
17 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 //  This software was developed by Applied Research Laboratories at the
20 //  University of Texas at Austin.
21 //  Copyright 2004-2020, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 //  This software was developed by Applied Research Laboratories at the
28 //  University of Texas at Austin, under contract to an agency or agencies
29 //  within the U.S. Department of Defense. The U.S. Government retains all
30 //  rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 //  Pursuant to DoD Directive 523024
33 //
34 //  DISTRIBUTION STATEMENT A: This software has been approved for public
35 //                            release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 // expandtilde.cpp Expand tilde (~) in filenames.
40 
41 #include <iostream>
42 #include <fstream>
43 #include "expandtilde.hpp"
44 #include "StringUtils.hpp"
45 
46 using namespace std;
47 using namespace gpstk;
48 
expand_filename(string & filename)49 void expand_filename(string& filename)
50 {
51 #ifndef _WIN32
52    static char *chome = getenv("HOME");
53    static string home = string(chome);
54 
55    // assume tilde occurs only once
56    string::size_type pos = filename.find_first_of("~");
57    if(pos == string::npos) return;
58    string newname;
59    if(pos > 0) newname = filename.substr(0,pos);
60    filename = filename.substr(pos+1);
61    StringUtils::stripLeading(filename,"/");
62    StringUtils::stripTrailing(home,"/");
63    newname += home + string("/") + filename;
64    filename = newname;
65 #endif
66 }
67 
expand_filename(vector<string> & sarray)68 void expand_filename(vector<string>& sarray)
69 {
70    for(size_t i=0; i<sarray.size(); i++) expand_filename(sarray[i]);
71 }
72 
include_path(string path,string & file)73 void include_path(string path, string& file)
74 {
75    if(!path.empty()) {
76       StringUtils::stripTrailing(path,"/");
77       StringUtils::stripTrailing(path,"\\");
78       file = path + string("/") + file;
79    }
80 }
81 
include_path(string path,vector<string> & sarray)82 void include_path(string path, vector<string>& sarray)
83 {
84    if(!path.empty()) {
85       StringUtils::stripTrailing(path,"/");
86       StringUtils::stripTrailing(path,"\\");
87       for(size_t i=0; i<sarray.size(); i++)
88          sarray[i] = path + string("/") + sarray[i];
89    }
90 }
91 
92 // return false if file cannot be opened
expand_list_file(string & filename,vector<string> & values)93 bool expand_list_file(string& filename, vector<string>& values)
94 {
95    string line,word;
96    // DO NOT clear values, add to it
97 
98    // open list file
99    ifstream infile;
100    infile.open(filename.c_str());
101    if(!infile.is_open()) return false;
102 
103    // read the list file
104    while(1) {
105       getline(infile,line);
106       StringUtils::stripTrailing(line,'\r');
107       StringUtils::stripLeading(line);
108       while(!line.empty()) {
109          word = StringUtils::stripFirstWord(line);
110          if(word.substr(0,1) == "#") break;        // skip '#...' to end of line
111          values.push_back(word);
112       }
113       if(infile.eof() || !infile.good()) break;
114    }
115 
116    infile.close();
117 
118    return true;
119 }
120