~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
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
16
#include "config.h"
17
18
#include "drizzled/internal/my_sys.h"
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
19
#include "drizzled/internal/m_string.h"
1 by brian
clean slate
20
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
21
#include <algorithm>
22
23
using namespace std;
24
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
25
namespace drizzled
26
{
27
namespace internal
28
{
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
29
1 by brian
clean slate
30
	/* Functions definied in this file */
31
32
size_t dirname_length(const char *name)
33
{
266.6.6 by Andy Lester
fixing some constants
34
  register const char *pos, *gpos;
1 by brian
clean slate
35
#ifdef FN_DEVCHAR
36
  if ((pos=(char*)strrchr(name,FN_DEVCHAR)) == 0)
37
#endif
266.7.7 by Andy Lester
const happiness
38
    pos=name-1;
1 by brian
clean slate
39
40
  gpos= pos++;
41
  for ( ; *pos ; pos++)				/* Find last FN_LIBCHAR */
42
  {
1122.2.5 by Monty Taylor
Killed some dead code.
43
    if (*pos == FN_LIBCHAR || *pos == '/')
1 by brian
clean slate
44
      gpos=pos;
45
  }
266.7.7 by Andy Lester
const happiness
46
  return gpos-name+1;
1 by brian
clean slate
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);
51.3.17 by Jay Pipes
Phase 4 - Remove DBUG from mysys
69
  return(length);
1 by brian
clean slate
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
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
79
    				min(FN_REFLEN, strlen(from) + 1) to make room
1 by brian
clean slate
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
461 by Monty Taylor
Removed NullS. bu-bye.
90
    If you want to use the whole 'from' string, just send NULL as the
1 by brian
clean slate
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
1122.2.5 by Monty Taylor
Killed some dead code.
111
#if FN_LIBCHAR != '/'
1 by brian
clean slate
112
  {
113
    for (; from != from_end && *from ; from++)
114
    {
115
      if (*from == '/')
116
	*to++= FN_LIBCHAR;
117
      else
118
      {
1054.2.19 by Monty Taylor
Removed some more unused code.
119
        *to++= *from;
1 by brian
clean slate
120
      }
121
    }
122
    *to=0;
123
  }
124
#else
125
  /* 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.
126
  size_t len= min(strlen(from),(size_t)(from_end-from));
1090.1.3 by Monty Taylor
Removed dangerous asserts... mainly to upset Stewart.
127
  void *ret= memmove(to, from, len);
128
  assert(ret != NULL);
629.5.5 by Toru Maesaka
Fifth pass of replacing MySQL's strmake() with libc calls
129
  to+= len;
130
  to[0]= '\0';
1 by brian
clean slate
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
  }
51.3.17 by Jay Pipes
Phase 4 - Remove DBUG from mysys
139
  return(to);                              /* Pointer to end of dir */
1 by brian
clean slate
140
} /* convert_dirname */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
141
142
} /* namespace internal */
143
} /* namespace drizzled */