drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
mf_format.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
20#include <fcntl.h>
21
22# include <sys/stat.h>
23
24#include <algorithm>
25
26#include <drizzled/internal/m_string.h>
27
28using namespace std;
29
30namespace drizzled
31{
32namespace internal
33{
34
35static size_t strlength(const char *str);
36
37/*
38 Formats a filename with possible replace of directory of extension
39 Function can handle the case where 'to' == 'name'
40 For a description of the flag values, consult my_sys.h
41 The arguments should be in unix format.
42*/
43
44char * fn_format(char * to, const char *name, const char *dir,
45 const char *extension, uint32_t flag)
46{
47 char dev[FN_REFLEN], buff[FN_REFLEN], *pos;
48 const char *startpos = name;
49 const char *ext;
50 size_t length;
51 size_t dev_length;
52
53 /* Copy and skip directory */
54 name+=(length=dirname_part(dev, startpos, &dev_length));
55 if (length == 0 || (flag & MY_REPLACE_DIR))
56 {
57 /* Use given directory */
58 convert_dirname(dev,dir,NULL); /* Fix to this OS */
59 }
60 else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev))
61 {
62 /* Put 'dir' before the given path */
63 snprintf(buff, sizeof(buff), "%s", dev);
64 pos=convert_dirname(dev,dir,NULL);
65 snprintf(pos, FN_REFLEN - (pos - dev), "%s", buff);
66 }
67
68 if (flag & MY_UNPACK_FILENAME)
69 (void) unpack_dirname(dev,dev); /* Replace ~/.. with dir */
70
71 if (!(flag & MY_APPEND_EXT) &&
72 (pos= (char*) strchr(name,FN_EXTCHAR)) != NULL)
73 {
74 if ((flag & MY_REPLACE_EXT) == 0) /* If we should keep old ext */
75 {
76 length=strlength(name); /* Use old extension */
77 ext = "";
78 }
79 else
80 {
81 length= (size_t) (pos-(char*) name); /* Change extension */
82 ext= extension;
83 }
84 }
85 else
86 {
87 length=strlength(name); /* No ext, use the now one */
88 ext=extension;
89 }
90
91 if (strlen(dev)+length+strlen(ext) >= FN_REFLEN || length >= FN_LEN )
92 {
93 /* To long path, return original or NULL */
94 size_t tmp_length;
95 if (flag & MY_SAFE_PATH)
96 return NULL;
97 tmp_length= min(strlength(startpos), (size_t)(FN_REFLEN-1));
98 memcpy(to,startpos,tmp_length);
99 to[tmp_length]= '\0';
100 }
101 else
102 {
103 if (to == startpos)
104 {
105 memmove(buff, name, length); /* Save name for last copy */
106 name=buff;
107 }
108 char *tmp= strcpy(to, dev) + strlen(dev);
109 memcpy(tmp,name,length);
110 pos= tmp + length;
111 (void) strcpy(pos,ext); /* Don't convert extension */
112 }
113 /*
114 If MY_RETURN_REAL_PATH and MY_RESOLVE_SYMLINK is given, only do
115 realpath if the file is a symbolic link
116 */
117 if (flag & MY_RETURN_REAL_PATH)
118 {
119 struct stat stat_buff;
120 char rp_buff[PATH_MAX];
121 if ((!lstat(to,&stat_buff) && S_ISLNK(stat_buff.st_mode)))
122 {
123 if (!realpath(to,rp_buff))
124 my_load_path(rp_buff, to, NULL);
125 rp_buff[FN_REFLEN-1]= '\0';
126 strcpy(to,rp_buff);
127 }
128 }
129 return(to);
130} /* fn_format */
131
132
133/*
134 strlength(const string str)
135 Return length of string with end-space:s not counted.
136*/
137
138static size_t strlength(const char *str)
139{
140 const char* found= str;
141 const char* pos= str;
142
143 while (*pos)
144 {
145 if (*pos != ' ')
146 {
147 while (*++pos && *pos != ' ') {};
148 if (!*pos)
149 {
150 found=pos; /* String ends here */
151 break;
152 }
153 }
154 found=pos;
155 while (*++pos == ' ') {};
156 }
157 return((size_t) (found - str));
158} /* strlength */
159
160} /* namespace internal */
161} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.
STL namespace.