coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
filesys.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
3  * All rights reserved. *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * *
12  * Redistributions in binary form must reproduce the above copyright *
13  * notice, this list of conditions and the following disclaimer in the *
14  * documentation and/or other materials provided with the distribution. *
15  * *
16  * Neither the name of Mike Kinghan nor the names of its contributors *
17  * may be used to endorse or promote products derived from this software *
18  * without specific prior written permission. *
19  * *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
31  * DAMAGE. *
32  * *
33  **************************************************************************/
34 
35 #include "filesys.h"
36 #include "diagnostic.h"
37 #include "path.h"
38 #include <iomanip>
39 
44 
46 using namespace std;
47 
48 string fs::tempname(string const & format)
49 {
50  size_t ndx = format.find("XXXXXX");
51  if (ndx != string::npos && ndx + 6 == format.length()) {
52  ostringstream ostr(format.substr(0,ndx));
53  ostr.seekp(ndx);
54  for ( unsigned i = 0; i <= 0xffffff; ++i,ostr.seekp(ndx)) {
55  ostr << setw(6) << setfill('0') << hex << i;
56  if (obj_type(ostr.str()) == OBJ_NONE) {
57  return ostr.str();
58  }
59  }
60  }
61  return string();
62 }
63 
64 string fs::abs_path(string const & filename)
65 {
66  obj_type_t type = obj_type(filename);
67  if (type != OBJ_NONE) {
68  return real_path(filename);
69  } else if (fs::is_absolute(filename)) {
70  return filename;
71  } else {
72  path_t cwd_path(cwd());
73  cwd_path += filename;
74  return cwd_path.str();
75  }
76 }
77 
79 
80 /* EOF*/
unsigned obj_type_t
Abstract type of filesystem object types.
Definition: filesys.h:61
bool is_absolute(std::string pathname)
Say whether a filename is absolute or relative.
std::string cwd()
Get the absolute real pathname of the current working directory.
std::string abs_path(std::string const &filename)
Get the absolute pathname for a filename.
std::string real_path(std::string const &relname)
Get the absolute real pathname of a file or directory name.
obj_type_t obj_type(std::string const &name)
Get the type of the object putatively designated by a filename.
std::string tempname(std::string const &format)
Create a tempory filename from a template.
No such object.
Definition: filesys.h:51