LCOV - code coverage report
Current view: top level - common - append_filename_arg.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 10 13 76.9 %
Date: 2019-06-30 05:20:33 Functions: 1 1 100.0 %
Branches: 6 12 50.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file append_filename_arg.h
       2                 :            :  *  @brief Append filename argument to a command string with suitable escaping
       3                 :            :  */
       4                 :            : /* Copyright (C) 2003,2004,2007,2012,2019 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      19                 :            :  */
      20                 :            : 
      21                 :            : #ifndef XAPIAN_INCLUDED_APPEND_FILENAME_ARG_H
      22                 :            : #define XAPIAN_INCLUDED_APPEND_FILENAME_ARG_H
      23                 :            : 
      24                 :            : #include <cstring>
      25                 :            : #include <string>
      26                 :            : 
      27                 :            : /// Append filename argument arg to command cmd with suitable escaping.
      28                 :            : static bool
      29                 :          3 : append_filename_argument(std::string& cmd,
      30                 :            :                          const std::string& arg,
      31                 :            :                          bool leading_space = true) {
      32                 :            : #ifdef __WIN32__
      33                 :            :     cmd.reserve(cmd.size() + arg.size() + 5);
      34                 :            :     // Prevent a leading "-" on the filename being interpreted as a command
      35                 :            :     // line option.
      36                 :            :     const char* prefix = (arg[0] == '-') ? " \".\\" : " \"";
      37                 :            :     if (!leading_space)
      38                 :            :         ++prefix;
      39                 :            :     cmd += prefix;
      40                 :            : 
      41                 :            :     for (std::string::const_iterator i = arg.begin(); i != arg.end(); ++i) {
      42                 :            :         if (*i == '/') {
      43                 :            :             // Convert Unix path separators to backslashes.  C library
      44                 :            :             // functions understand "/" in paths, but we are going to
      45                 :            :             // call commands like "xcopy" or "rd" which don't.
      46                 :            :             cmd += '\\';
      47                 :            :         } else if (*i < 32 || std::strchr("<>\"|*?", *i)) {
      48                 :            :             // Check for illegal characters in filename.
      49                 :            :             return false;
      50                 :            :         } else {
      51                 :            :             cmd += *i;
      52                 :            :         }
      53                 :            :     }
      54                 :            :     cmd += '"';
      55                 :            : #else
      56                 :            :     // Allow for the typical case of a filename without single quote characters
      57                 :            :     // in - this reserving is just an optimisation, and the string will grow
      58                 :            :     // larger if necessary.
      59                 :          3 :     cmd.reserve(cmd.size() + arg.size() + 5);
      60                 :            : 
      61                 :            :     // Prevent a leading "-" on the filename being interpreted as a command
      62                 :            :     // line option.
      63         [ -  + ]:          3 :     const char* prefix = (arg[0] == '-') ? " './" : " '";
      64         [ -  + ]:          3 :     if (!leading_space)
      65                 :          0 :         ++prefix;
      66                 :          3 :     cmd += prefix;
      67                 :            : 
      68         [ +  + ]:         59 :     for (std::string::const_iterator i = arg.begin(); i != arg.end(); ++i) {
      69         [ -  + ]:         56 :         if (*i == '\'') {
      70                 :            :             // Wrapping the whole argument in single quotes works for
      71                 :            :             // everything except a single quote - for that we drop out of
      72                 :            :             // single quotes, then use a backslash-escaped single quote, then
      73                 :            :             // re-enter single quotes.
      74         [ #  # ]:          0 :             cmd += "'\\''";
      75                 :          0 :             continue;
      76                 :            :         }
      77         [ +  - ]:         56 :         cmd += *i;
      78                 :            :     }
      79                 :          3 :     cmd += '\'';
      80                 :            : #endif
      81                 :          3 :     return true;
      82                 :            : }
      83                 :            : 
      84                 :            : #endif // XAPIAN_INCLUDED_APPEND_FILENAME_ARG_H

Generated by: LCOV version 1.11