Home | History | Annotate | Download | only in io
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #ifndef TENSORFLOW_LIB_IO_PATH_H_
     17 #define TENSORFLOW_LIB_IO_PATH_H_
     18 
     19 #include "tensorflow/core/lib/core/status.h"
     20 #include "tensorflow/core/lib/core/stringpiece.h"
     21 
     22 namespace tensorflow {
     23 namespace io {
     24 namespace internal {
     25 string JoinPathImpl(std::initializer_list<tensorflow::StringPiece> paths);
     26 }
     27 
     28 // Utility routines for processing filenames
     29 
     30 #ifndef SWIG  // variadic templates
     31 // Join multiple paths together, without introducing unnecessary path
     32 // separators.
     33 // For example:
     34 //
     35 //  Arguments                  | JoinPath
     36 //  ---------------------------+----------
     37 //  '/foo', 'bar'              | /foo/bar
     38 //  '/foo/', 'bar'             | /foo/bar
     39 //  '/foo', '/bar'             | /foo/bar
     40 //
     41 // Usage:
     42 // string path = io::JoinPath("/mydir", filename);
     43 // string path = io::JoinPath(FLAGS_test_srcdir, filename);
     44 // string path = io::JoinPath("/full", "path", "to", "filename);
     45 template <typename... T>
     46 string JoinPath(const T&... args) {
     47   return internal::JoinPathImpl({args...});
     48 }
     49 #endif /* SWIG */
     50 
     51 // Return true if path is absolute.
     52 bool IsAbsolutePath(tensorflow::StringPiece path);
     53 
     54 // Returns the part of the path before the final "/".  If there is a single
     55 // leading "/" in the path, the result will be the leading "/".  If there is
     56 // no "/" in the path, the result is the empty prefix of the input.
     57 tensorflow::StringPiece Dirname(tensorflow::StringPiece path);
     58 
     59 // Returns the part of the path after the final "/".  If there is no
     60 // "/" in the path, the result is the same as the input.
     61 tensorflow::StringPiece Basename(tensorflow::StringPiece path);
     62 
     63 // Returns the part of the basename of path after the final ".".  If
     64 // there is no "." in the basename, the result is empty.
     65 tensorflow::StringPiece Extension(tensorflow::StringPiece path);
     66 
     67 // Collapse duplicate "/"s, resolve ".." and "." path elements, remove
     68 // trailing "/".
     69 //
     70 // NOTE: This respects relative vs. absolute paths, but does not
     71 // invoke any system calls (getcwd(2)) in order to resolve relative
     72 // paths with respect to the actual working directory.  That is, this is purely
     73 // string manipulation, completely independent of process state.
     74 string CleanPath(tensorflow::StringPiece path);
     75 
     76 // Populates the scheme, host, and path from a URI. scheme, host, and path are
     77 // guaranteed by this function to point into the contents of uri, even if
     78 // empty.
     79 //
     80 // Corner cases:
     81 // - If the URI is invalid, scheme and host are set to empty strings and the
     82 //   passed string is assumed to be a path
     83 // - If the URI omits the path (e.g. file://host), then the path is left empty.
     84 void ParseURI(tensorflow::StringPiece uri, tensorflow::StringPiece* scheme,
     85               tensorflow::StringPiece* host, tensorflow::StringPiece* path);
     86 
     87 // Creates a URI from a scheme, host, and path. If the scheme is empty, we just
     88 // return the path.
     89 string CreateURI(tensorflow::StringPiece scheme, tensorflow::StringPiece host,
     90                  tensorflow::StringPiece path);
     91 
     92 // Creates a temporary file name with an extension.
     93 string GetTempFilename(const string& extension);
     94 
     95 }  // namespace io
     96 }  // namespace tensorflow
     97 
     98 #endif  // TENSORFLOW_LIB_IO_PATH_H_
     99