File: path.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (406 lines) | stat: -rw-r--r-- 8,937 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * ====================================================================
 * Copyright (c) 2002-2009 The RapidSvn Group.  All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (in the file GPL.txt.
 * If not, see <http://www.gnu.org/licenses/>.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://rapidsvn.tigris.org/.
 * ====================================================================
 */

// subversion api
#include "svn_path.h"
#include "svn_dirent_uri.h"

// apr api
#include "apr_file_io.h"

// svncpp
#include "kdevsvncpp/path.hpp"
#include "kdevsvncpp/pool.hpp"
#include "kdevsvncpp/url.hpp"

namespace svn
{
  const PathVector EmptyPathVector;

  Path::Path(const char * path)
  {
    init(path);
  }

  Path::Path(const std::string & path)
  {
    init(path.c_str());
  }

  Path::Path(const Path & path)
  {
    init(path.c_str());
  }

  void
  Path::init(const char * path)
  {
    Pool pool;

    m_pathIsUrl = false;

    if (path == nullptr)
      m_path = "";
    else
    {
      const char * int_path = svn_dirent_canonicalize(path, pool);

      m_path = int_path;

      if (svn::Url::isValid(int_path))
        m_pathIsUrl = true;
    }
  }

  const std::string &
  Path::path() const
  {
    return m_path;
  }

  const char *
  Path::c_str() const
  {
    return m_path.c_str();
  }

  Path&
  Path::operator= (const Path & path)
  {
    if (this == &path)
      return *this;

    init(path.c_str());

    return *this;
  }

  bool
  Path::operator== (const Path& path) const
  {
    if (path.path() == this->path())
      return true;

    return false;
  }

  bool
  Path::isSet() const
  {
    return m_path.length() > 0;
  }

  bool
  Path::isUrl() const
  {
    return m_pathIsUrl;
  }

  static bool
  isAbsolute(const char * path)
  {
    if (nullptr == path)
      return false;

    std::string p(path);

    if (0 == p.length())
      return false;

    // a path that begins with "/" is absolute
    if ('/' == p [0])
      return true;

    // a path with a ":" like "http://xxx" or
    // "c:/foo" is absolute too
    if (p.find(":", 0) != std::string::npos)
      return true;

    // Well it's relative
    return false;
  }

  void
  Path::addComponent(const char * component)
  {
    Pool pool;

    if (nullptr == component)
      return;

    // in case of an empty string, return
    if (*component == 0)
      return;

    // if the @a component is absolute, simply
    // use it
    if (isAbsolute(component))
    {
      m_path = component;
      return;
    }

    if (Url::isValid(m_path.c_str()))
    {
      const char * newPath =
        svn_path_url_add_component(m_path.c_str(),
                                   component,
                                   pool);
      m_path = newPath;
    }
    else
    {
      svn_stringbuf_t * pathStringbuf =
        svn_stringbuf_create(m_path.c_str(), pool);

      svn_path_add_component(pathStringbuf,
                             component);

      m_path = pathStringbuf->data;
    }
  }

  void
  Path::addComponent(const std::string & component)
  {
    addComponent(component.c_str());
  }

  void
  Path::split(std::string & dirpath, std::string & basename) const
  {
    Pool pool;

    const char * cdirpath;
    const char * cbasename;

    svn_path_split(m_path.c_str(), &cdirpath, &cbasename, pool);

    dirpath = cdirpath;
    basename = cbasename;
  }

  void
  Path::split(std::string & dir, std::string & filename, std::string & ext) const
  {
    std::string basename;

    // first split path into dir and filename+ext
    split(dir, basename);

    // next search for last .
    size_t pos = basename.find_last_of(".");
    if (pos == std::string::npos)
    {
      filename = basename;
      ext = "";
    }
    else
    {
      filename = basename.substr(0, pos);
      ext = basename.substr(pos);
    }
  }

  std::string
  Path::basename() const
  {
    std::string dir;
    std::string filename;

    split(dir, filename);

    return filename;
  }

  std::string
  Path::dirpath() const
  {
    std::string dir;
    std::string filename;

    split(dir, filename);

    return dir;
  }

  std::string
  Path::substr(const size_t count) const
  {
    if (m_path.length() > count)
      return m_path.substr(count);
    else
      return "";
  }

  std::string
  Path::unescape() const
  {
    return svn::Url::unescape(m_path.c_str());
  }


  /* ===================================================================
   * The next two Fixed_* functions are copies of the APR
   * apr_temp_dir_get functionality with a fix applied.
   * This should turn up in APR release 0.9.5 or 1.0, but
   * for now is reproduced here.
   *
   * TODO: Remove this section!
   */
#include "apr_env.h"

#define test_tempdir    Fixed_test_tempdir
#define apr_temp_dir_get    Fixed_apr_temp_dir_get

  static char global_temp_dir[APR_PATH_MAX+1] = { 0 };

  /* Try to open a temporary file in the temporary dir, write to it,
    and then close it. */
  static int Fixed_test_tempdir(const char *temp_dir, apr_pool_t *p)
  {
    apr_file_t *dummy_file;
    // This is the only actual fix - adding the ".XXXXXX"!
    const char *path = apr_pstrcat(p, temp_dir, "/apr-tmp.XXXXXX", NULL);

    if (apr_file_mktemp(&dummy_file, (char *)path, 0, p) == APR_SUCCESS) {
      if (apr_file_putc('!', dummy_file) == APR_SUCCESS) {
        if (apr_file_close(dummy_file) == APR_SUCCESS) {
          apr_file_remove(path, p);
          return 1;
        }
      }
    }
    return 0;
  }

  static apr_status_t Fixed_apr_temp_dir_get(const char **temp_dir, apr_pool_t *p)
  {
    apr_status_t apr_err;
    const char *try_dirs[] = { "/tmp", "/usr/tmp", "/var/tmp" };
    const char *try_envs[] = { "TMP", "TEMP", "TMPDIR" };
    char *cwd;
    size_t i;

    /* Our goal is to find a temporary directory suitable for writing
       into.  We'll only pay the price once if we're successful -- we
       cache our successful find.  Here's the order in which we'll try
       various paths:

          $TMP
          $TEMP
          $TMPDIR
          "/tmp"
          "/var/tmp"
          "/usr/tmp"
          `pwd`

       NOTE: This algorithm is basically the same one used by Python
       2.2's tempfile.py module.  */

    /* Try the environment first. */
    for (i = 0; i < (sizeof(try_envs) / sizeof(const char *)); i++) {
      char *value;
      apr_err = apr_env_get(&value, try_envs[i], p);
      if ((apr_err == APR_SUCCESS) && value) {
        apr_size_t len = strlen(value);
        if (len && (len < APR_PATH_MAX) && test_tempdir(value, p)) {
          memcpy(global_temp_dir, value, len + 1);
          goto end;
        }
      }
    }

    /* Next, try a set of hard-coded paths. */
    for (i = 0; i < (sizeof(try_dirs) / sizeof(const char *)); i++) {
      if (test_tempdir(try_dirs[i], p)) {
        memcpy(global_temp_dir, try_dirs[i], strlen(try_dirs[i]) + 1);
        goto end;
      }
    }

    /* Finally, try the current working directory. */
    if (APR_SUCCESS == apr_filepath_get(&cwd, APR_FILEPATH_NATIVE, p)) {
      if (test_tempdir(cwd, p)) {
        memcpy(global_temp_dir, cwd, strlen(cwd) + 1);
        goto end;
      }
    }

end:
    if (global_temp_dir[0]) {
      *temp_dir = apr_pstrdup(p, global_temp_dir);
      return APR_SUCCESS;
    }
    return APR_EGENERAL;
  }
  /* ===================================================================
   * End of inserted fixed APR code
   */

  Path
  Path::getTempDir()
  {
    const char * tempdir = nullptr;
    Pool pool;

    if (apr_temp_dir_get(&tempdir, pool) != APR_SUCCESS)
    {
      tempdir = nullptr;
    }

    return tempdir;
  }

  size_t
  Path::length() const
  {
    return m_path.length();
  }

  std::string
  Path::native() const
  {
    if (m_pathIsUrl)
    {
      // this converts something like
      // http://foo/my%20location
      // to
      // http://foo/my location
	    return Url::unescape(m_path.c_str());
    }
    else
    {
      // On Windows, p://foo/bar will be converted to p:\foo\bar
	    Pool pool;
      return svn_path_local_style(m_path.c_str(), pool);
    }
  }
}

/* -----------------------------------------------------------------
 * local variables:
 * eval: (load-file "../../rapidsvn-dev.el")
 * end:
 */