summaryrefslogtreecommitdiffstats
path: root/lib/qpath.h
blob: 27c61dff5535e21a30818e51ca1c4f63f34986d1 (plain)
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
/* Some primitive path handling -- header
 * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra 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 2, or (at your
 * option) any later version.
 *
 * GNU Zebra 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 GNU Zebra; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef _ZEBRA_QPATH_H
#define _ZEBRA_QPATH_H

#include "zebra.h"
#include "misc.h"
#include "memory.h"







/*==============================================================================
 * For these purposes a path is "parts" separated by one more '/' characters.
 *
 * As per POSIX, a pair of leading "//" (not three or more leading '/') is
 * very special.
 *
 * The following sexes of qpath are established after any "path reduction"
 * has been done.  Path reduction removes extraneous '/'.
 */

typedef enum qpath_sex qpath_sex_t ;

enum qpath_sex
{
  qp_empty,             /* nothing at all                       */
  qp_relative,          /* something, not starting '/'          */
  qp_root,              /* "/" all on its own                   */
  qp_absolute,          /* something, starting with single '/'  */
  qp_homed,             /* something, starting with '~'         */
  qp_double_root,       /* "//" all on its own                  */
  qp_double_absolute    /* something, starting with "//"        */
} ;

typedef struct qpath  qpath_t ;
typedef struct qpath* qpath ;

/* The qpath structure is largely a qstring, but one in which there is always
 * a body, even if it only contains "\0", and the len is kept up to date.
 */
struct qpath
{
  qstring_t     path ;
} ;

/*==============================================================================
 * Functions
 */

extern qpath
qpath_init_new(qpath qp, const char* path) ;

extern qpath
qpath_reset(qpath qp, bool free_structure) ;

Inline qpath
qpath_reset_free(qpath qp) { qpath_reset(qp, true) ; } ;

Inline qpath
qpath_reset_keep(qpath qp) { qpath_reset(qp, false) ; } ;

Inline const char*
qpath_path(qpath qp) ;

extern qpath
qpath_trim(qpath qp) ;

extern qpath
qpath_copy(qpath qp) ;

extern qpath
qpath_pop(qpath qp) ;

extern qpath
qpath_push(qpath qp, qpath qp_a) ;

extern qpath
qpath_join(qpath qp, qpath qp_a) ;

/*==============================================================================
 * Inline stuff
 */

/*------------------------------------------------------------------------------
 * Get *temporary* pointer to actual path contained in the given qpath.
 *
 * This is *temporary* to the extent that when the qpath is changed or freed,
 * this pointer will be INVALID -- you have been warned.
 *
 * This is a *const* pointer.
 *
 * For a NULL qpath, or an empty qpath, returns pointer to an empty string
 * ('\0' terminated "").
 */
Inline const char*
qpath_path(qpath qp)
{
  return qs_string(qp != NULL ? &qp->path : qp) ;
} ;

#endif /* _ZEBRA_QPATH_H */