~drizzle-trunk/drizzle/development

1 by brian
clean slate
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
994.2.4 by Monty Taylor
Blast. Fixed some make distcheck issues.
16
#include "mysys/mysys_priv.h"
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
17
#include <mystrings/m_string.h>
1 by brian
clean slate
18
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
19
#include <algorithm>
20
21
using namespace std;
22
23
1 by brian
clean slate
24
	/* Functions definied in this file */
25
26
size_t dirname_length(const char *name)
27
{
266.6.6 by Andy Lester
fixing some constants
28
  register const char *pos, *gpos;
1 by brian
clean slate
29
#ifdef FN_DEVCHAR
30
  if ((pos=(char*)strrchr(name,FN_DEVCHAR)) == 0)
31
#endif
266.7.7 by Andy Lester
const happiness
32
    pos=name-1;
1 by brian
clean slate
33
34
  gpos= pos++;
35
  for ( ; *pos ; pos++)				/* Find last FN_LIBCHAR */
36
  {
37
    if (*pos == FN_LIBCHAR || *pos == '/'
38
#ifdef FN_C_AFTER_DIR
39
	|| *pos == FN_C_AFTER_DIR || *pos == FN_C_AFTER_DIR_2
40
#endif
41
	)
42
      gpos=pos;
43
  }
266.7.7 by Andy Lester
const happiness
44
  return gpos-name+1;
1 by brian
clean slate
45
}
46
47
48
/*
49
  Gives directory part of filename. Directory ends with '/'
50
51
  SYNOPSIS
52
    dirname_part()
53
    to		Store directory name here
54
    name	Original name
55
    to_length	Store length of 'to' here
56
57
  RETURN
58
   #  Length of directory part in 'name'
59
*/
60
61
size_t dirname_part(char *to, const char *name, size_t *to_res_length)
62
{
63
  size_t length;
64
65
  length=dirname_length(name);
66
  *to_res_length= (size_t) (convert_dirname(to, name, name+length) - to);
51.3.17 by Jay Pipes
Phase 4 - Remove DBUG from mysys
67
  return(length);
1 by brian
clean slate
68
} /* dirname */
69
70
71
/*
72
  Convert directory name to use under this system
73
74
  SYNPOSIS
75
    convert_dirname()
76
    to				Store result here. Must be at least of size
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
77
    				min(FN_REFLEN, strlen(from) + 1) to make room
1 by brian
clean slate
78
    				for adding FN_LIBCHAR at the end.
79
    from			Original filename. May be == to
80
    from_end			Pointer at end of filename (normally end \0)
81
82
  IMPLEMENTATION
83
    If MSDOS converts '/' to '\'
84
    If VMS converts '<' to '[' and '>' to ']'
85
    Adds a FN_LIBCHAR to end if the result string if there isn't one
86
    and the last isn't dev_char.
87
    Copies data from 'from' until ASCII(0) for until from == from_end
461 by Monty Taylor
Removed NullS. bu-bye.
88
    If you want to use the whole 'from' string, just send NULL as the
1 by brian
clean slate
89
    last argument.
90
91
    If the result string is larger than FN_REFLEN -1, then it's cut.
92
93
  RETURN
94
   Returns pointer to end \0 in to
95
*/
96
97
#ifndef FN_DEVCHAR
98
#define FN_DEVCHAR '\0'				/* For easier code */
99
#endif
100
101
char *convert_dirname(char *to, const char *from, const char *from_end)
102
{
103
  char *to_org=to;
104
105
  /* We use -2 here, becasue we need place for the last FN_LIBCHAR */
106
  if (!from_end || (from_end - from) > FN_REFLEN-2)
107
    from_end=from+FN_REFLEN -2;
108
109
#if FN_LIBCHAR != '/' || defined(FN_C_BEFORE_DIR_2)
110
  {
111
    for (; from != from_end && *from ; from++)
112
    {
113
      if (*from == '/')
114
	*to++= FN_LIBCHAR;
115
#ifdef FN_C_BEFORE_DIR_2
116
      else if (*from == FN_C_BEFORE_DIR_2)
117
	*to++= FN_C_BEFORE_DIR;
118
      else if (*from == FN_C_AFTER_DIR_2)
119
	*to++= FN_C_AFTER_DIR;
120
#endif
121
      else
122
      {
1054.2.19 by Monty Taylor
Removed some more unused code.
123
        *to++= *from;
1 by brian
clean slate
124
      }
125
    }
126
    *to=0;
127
  }
128
#else
129
  /* This is ok even if to == from, becasue we need to cut the string */
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
130
  size_t len= min(strlen(from),(size_t)(from_end-from));
678 by Brian Aker
Fix a valgrind warning.
131
  assert(memmove(to, from, len));
629.5.5 by Toru Maesaka
Fifth pass of replacing MySQL's strmake() with libc calls
132
  to+= len;
133
  to[0]= '\0';
1 by brian
clean slate
134
#endif
135
136
  /* Add FN_LIBCHAR to the end of directory path */
137
  if (to != to_org && (to[-1] != FN_LIBCHAR && to[-1] != FN_DEVCHAR))
138
  {
139
    *to++=FN_LIBCHAR;
140
    *to=0;
141
  }
51.3.17 by Jay Pipes
Phase 4 - Remove DBUG from mysys
142
  return(to);                              /* Pointer to end of dir */
1 by brian
clean slate
143
} /* convert_dirname */