Drizzled Public API Documentation

mf_dirname.cc
1 /* Copyright (C) 2000 MySQL AB
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include <config.h>
17 
18 #include <drizzled/internal/my_sys.h>
19 #include <drizzled/internal/m_string.h>
20 
21 #include <algorithm>
22 
23 using namespace std;
24 
25 namespace drizzled
26 {
27 namespace internal
28 {
29 
30  /* Functions definied in this file */
31 
32 size_t dirname_length(const char *name)
33 {
34  const char *pos, *gpos;
35 #ifdef FN_DEVCHAR
36  if ((pos=(char*)strrchr(name,FN_DEVCHAR)) == 0)
37 #endif
38  pos=name-1;
39 
40  gpos= pos++;
41  for ( ; *pos ; pos++) /* Find last FN_LIBCHAR */
42  {
43  if (*pos == FN_LIBCHAR || *pos == '/')
44  gpos=pos;
45  }
46  return gpos-name+1;
47 }
48 
49 
50 /*
51  Gives directory part of filename. Directory ends with '/'
52 
53  SYNOPSIS
54  dirname_part()
55  to Store directory name here
56  name Original name
57  to_length Store length of 'to' here
58 
59  RETURN
60  # Length of directory part in 'name'
61 */
62 
63 size_t dirname_part(char *to, const char *name, size_t *to_res_length)
64 {
65  size_t length;
66 
67  length=dirname_length(name);
68  *to_res_length= (size_t) (convert_dirname(to, name, name+length) - to);
69  return(length);
70 } /* dirname */
71 
72 
73 /*
74  Convert directory name to use under this system
75 
76  SYNPOSIS
77  convert_dirname()
78  to Store result here. Must be at least of size
79  min(FN_REFLEN, strlen(from) + 1) to make room
80  for adding FN_LIBCHAR at the end.
81  from Original filename. May be == to
82  from_end Pointer at end of filename (normally end \0)
83 
84  IMPLEMENTATION
85  If MSDOS converts '/' to '\'
86  If VMS converts '<' to '[' and '>' to ']'
87  Adds a FN_LIBCHAR to end if the result string if there isn't one
88  and the last isn't dev_char.
89  Copies data from 'from' until ASCII(0) for until from == from_end
90  If you want to use the whole 'from' string, just send NULL as the
91  last argument.
92 
93  If the result string is larger than FN_REFLEN -1, then it's cut.
94 
95  RETURN
96  Returns pointer to end \0 in to
97 */
98 
99 #ifndef FN_DEVCHAR
100 #define FN_DEVCHAR '\0' /* For easier code */
101 #endif
102 
103 char *convert_dirname(char *to, const char *from, const char *from_end)
104 {
105  char *to_org=to;
106 
107  /* We use -2 here, becasue we need place for the last FN_LIBCHAR */
108  if (!from_end || (from_end - from) > FN_REFLEN-2)
109  from_end=from+FN_REFLEN -2;
110 
111 #if FN_LIBCHAR != '/'
112  {
113  for (; from != from_end && *from ; from++)
114  {
115  if (*from == '/')
116  *to++= FN_LIBCHAR;
117  else
118  {
119  *to++= *from;
120  }
121  }
122  *to=0;
123  }
124 #else
125  /* This is ok even if to == from, becasue we need to cut the string */
126  size_t len= min(strlen(from),(size_t)(from_end-from));
127  void *ret= memmove(to, from, len);
128  assert(ret != NULL);
129  to+= len;
130  to[0]= '\0';
131 #endif
132 
133  /* Add FN_LIBCHAR to the end of directory path */
134  if (to != to_org && (to[-1] != FN_LIBCHAR && to[-1] != FN_DEVCHAR))
135  {
136  *to++=FN_LIBCHAR;
137  *to=0;
138  }
139  return(to); /* Pointer to end of dir */
140 } /* convert_dirname */
141 
142 } /* namespace internal */
143 } /* namespace drizzled */