~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_dirname.c

  • Committer: Brian Aker
  • Date: 2008-07-03 00:14:39 UTC
  • Revision ID: brian@tangent.org-20080703001439-pit0mcl0wk8elxlq
Cleanup of sql-common and mysqldump

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
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
 
{
 
16
#include "mysys_priv.h"
 
17
#include <m_string.h>
29
18
 
30
19
        /* Functions definied in this file */
31
20
 
32
21
size_t dirname_length(const char *name)
33
22
{
34
 
  register const char *pos, *gpos;
 
23
  register char *pos, *gpos;
 
24
#ifdef BASKSLASH_MBTAIL
 
25
  CHARSET_INFO *fs= fs_character_set();
 
26
#endif
35
27
#ifdef FN_DEVCHAR
36
28
  if ((pos=(char*)strrchr(name,FN_DEVCHAR)) == 0)
37
29
#endif
38
 
    pos=name-1;
 
30
    pos=(char*) name-1;
39
31
 
40
32
  gpos= pos++;
41
33
  for ( ; *pos ; pos++)                         /* Find last FN_LIBCHAR */
42
34
  {
43
 
    if (*pos == FN_LIBCHAR || *pos == '/')
 
35
#ifdef BASKSLASH_MBTAIL
 
36
    uint l;
 
37
    if (use_mb(fs) && (l= my_ismbchar(fs, pos, pos + 3)))
 
38
    {
 
39
      pos+= l - 1;
 
40
      continue;
 
41
    }
 
42
#endif
 
43
    if (*pos == FN_LIBCHAR || *pos == '/'
 
44
#ifdef FN_C_AFTER_DIR
 
45
        || *pos == FN_C_AFTER_DIR || *pos == FN_C_AFTER_DIR_2
 
46
#endif
 
47
        )
44
48
      gpos=pos;
45
49
  }
46
 
  return gpos-name+1;
 
50
  return (size_t) (gpos+1-(char*) name);
47
51
}
48
52
 
49
53
 
63
67
size_t dirname_part(char *to, const char *name, size_t *to_res_length)
64
68
{
65
69
  size_t length;
 
70
  DBUG_ENTER("dirname_part");
 
71
  DBUG_PRINT("enter",("'%s'",name));
66
72
 
67
73
  length=dirname_length(name);
68
74
  *to_res_length= (size_t) (convert_dirname(to, name, name+length) - to);
69
 
  return(length);
 
75
  DBUG_RETURN(length);
70
76
} /* dirname */
71
77
 
72
78
 
87
93
    Adds a FN_LIBCHAR to end if the result string if there isn't one
88
94
    and the last isn't dev_char.
89
95
    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
 
96
    If you want to use the whole 'from' string, just send NullS as the
91
97
    last argument.
92
98
 
93
99
    If the result string is larger than FN_REFLEN -1, then it's cut.
103
109
char *convert_dirname(char *to, const char *from, const char *from_end)
104
110
{
105
111
  char *to_org=to;
 
112
#ifdef BACKSLASH_MBTAIL
 
113
  CHARSET_INFO *fs= fs_character_set();
 
114
#endif
 
115
  DBUG_ENTER("convert_dirname");
106
116
 
107
117
  /* We use -2 here, becasue we need place for the last FN_LIBCHAR */
108
118
  if (!from_end || (from_end - from) > FN_REFLEN-2)
109
119
    from_end=from+FN_REFLEN -2;
110
120
 
111
 
#if FN_LIBCHAR != '/'
 
121
#if FN_LIBCHAR != '/' || defined(FN_C_BEFORE_DIR_2)
112
122
  {
113
123
    for (; from != from_end && *from ; from++)
114
124
    {
115
125
      if (*from == '/')
116
126
        *to++= FN_LIBCHAR;
 
127
#ifdef FN_C_BEFORE_DIR_2
 
128
      else if (*from == FN_C_BEFORE_DIR_2)
 
129
        *to++= FN_C_BEFORE_DIR;
 
130
      else if (*from == FN_C_AFTER_DIR_2)
 
131
        *to++= FN_C_AFTER_DIR;
 
132
#endif
117
133
      else
118
134
      {
119
 
        *to++= *from;
 
135
#ifdef BACKSLASH_MBTAIL
 
136
        uint l;
 
137
        if (use_mb(fs) && (l= my_ismbchar(fs, from, from + 3)))
 
138
        {
 
139
          memmove(to, from, l);
 
140
          to+= l;
 
141
          from+= l - 1;
 
142
          to_org= to; /* Don't look inside mbchar */
 
143
        }
 
144
        else
 
145
#endif
 
146
        {
 
147
          *to++= *from;
 
148
        }
120
149
      }
121
150
    }
122
151
    *to=0;
123
152
  }
124
153
#else
125
154
  /* 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';
 
155
  to= strmake(to, from, (size_t) (from_end-from));
131
156
#endif
132
157
 
133
158
  /* Add FN_LIBCHAR to the end of directory path */
136
161
    *to++=FN_LIBCHAR;
137
162
    *to=0;
138
163
  }
139
 
  return(to);                              /* Pointer to end of dir */
 
164
  DBUG_RETURN(to);                              /* Pointer to end of dir */
140
165
} /* convert_dirname */
141
 
 
142
 
} /* namespace internal */
143
 
} /* namespace drizzled */