~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_format.c

Renamed more stuff to drizzle.

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
 
 
20
 
#include <fcntl.h>
21
 
 
22
 
#ifdef HAVE_SYS_STAT_H
23
 
# include <sys/stat.h>
24
 
#endif
25
 
 
26
 
#include <algorithm>
27
 
 
28
 
#include "drizzled/internal/m_string.h"
29
 
 
30
 
using namespace std;
31
 
 
32
 
namespace drizzled
33
 
{
34
 
namespace internal
35
 
{
 
16
#include "mysys_priv.h"
 
17
#include <m_string.h>
36
18
 
37
19
/*
38
20
  Formats a filename with possible replace of directory of extension
42
24
*/
43
25
 
44
26
char * fn_format(char * to, const char *name, const char *dir,
45
 
                    const char *extension, uint32_t flag)
 
27
                    const char *extension, uint flag)
46
28
{
47
 
  char dev[FN_REFLEN], buff[FN_REFLEN], *pos;
48
 
  const char *startpos = name;
 
29
  char dev[FN_REFLEN], buff[FN_REFLEN], *pos, *startpos;
49
30
  const char *ext;
50
31
  register size_t length;
51
32
  size_t dev_length;
 
33
  DBUG_ENTER("fn_format");
 
34
  DBUG_PRINT("enter",("name: %s  dir: %s  extension: %s  flag: %d",
 
35
                       name,dir,extension,flag));
52
36
 
53
37
  /* Copy and skip directory */
54
 
  name+=(length=dirname_part(dev, startpos, &dev_length));
 
38
  name+=(length=dirname_part(dev, (startpos=(char *) name), &dev_length));
55
39
  if (length == 0 || (flag & MY_REPLACE_DIR))
56
40
  {
57
41
    /* Use given directory */
58
 
    convert_dirname(dev,dir,NULL);              /* Fix to this OS */
 
42
    convert_dirname(dev,dir,NullS);             /* Fix to this OS */
59
43
  }
60
44
  else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev))
61
45
  {
62
46
    /* Put 'dir' before the given path */
63
 
    strncpy(buff,dev,sizeof(buff)-1);
64
 
    pos=convert_dirname(dev,dir,NULL);
65
 
    strncpy(pos,buff,sizeof(buff)-1- (int) (pos-dev));
 
47
    strmake(buff,dev,sizeof(buff)-1);
 
48
    pos=convert_dirname(dev,dir,NullS);
 
49
    strmake(pos,buff,sizeof(buff)-1- (int) (pos-dev));
66
50
  }
67
51
 
 
52
  if (flag & MY_PACK_FILENAME)
 
53
    pack_dirname(dev,dev);                      /* Put in ./.. and ~/.. */
68
54
  if (flag & MY_UNPACK_FILENAME)
69
55
    (void) unpack_dirname(dev,dev);             /* Replace ~/.. with dir */
70
56
 
71
57
  if (!(flag & MY_APPEND_EXT) &&
72
 
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NULL)
 
58
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NullS)
73
59
  {
74
60
    if ((flag & MY_REPLACE_EXT) == 0)           /* If we should keep old ext */
75
61
    {
93
79
    /* To long path, return original or NULL */
94
80
    size_t tmp_length;
95
81
    if (flag & MY_SAFE_PATH)
96
 
      return NULL;
97
 
    tmp_length= min(strlength(startpos), (size_t)(FN_REFLEN-1));
98
 
    strncpy(to,startpos,tmp_length);
99
 
    to[tmp_length]= '\0';
 
82
      return NullS;
 
83
    tmp_length= strlength(startpos);
 
84
    DBUG_PRINT("error",("dev: '%s'  ext: '%s'  length: %u",dev,ext,
 
85
                        (uint) length));
 
86
    (void) strmake(to,startpos,min(tmp_length,FN_REFLEN-1));
100
87
  }
101
88
  else
102
89
  {
103
90
    if (to == startpos)
104
91
    {
105
 
      memmove(buff, name, length); /* Save name for last copy */
 
92
      bmove(buff,(uchar*) name,length);         /* Save name for last copy */
106
93
      name=buff;
107
94
    }
108
 
    char *tmp= strcpy(to, dev) + strlen(dev);
109
 
    pos= strncpy(tmp,name,length) + length;
110
 
    (void) strcpy(pos,ext);                     /* Don't convert extension */
 
95
    pos=strmake(strmov(to,dev),name,length);
 
96
    (void) strmov(pos,ext);                     /* Don't convert extension */
111
97
  }
112
98
  /*
113
99
    If MY_RETURN_REAL_PATH and MY_RESOLVE_SYMLINK is given, only do
114
100
    realpath if the file is a symbolic link
115
101
  */
116
102
  if (flag & MY_RETURN_REAL_PATH)
117
 
  {
118
 
    struct stat stat_buff;
119
 
    char rp_buff[PATH_MAX];
120
 
    if ((!flag & MY_RESOLVE_SYMLINKS) || 
121
 
       (!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
 
  }
 
103
    (void) my_realpath(to, to, MYF(flag & MY_RESOLVE_SYMLINKS ?
 
104
                                   MY_RESOLVE_LINK: 0));
129
105
  else if (flag & MY_RESOLVE_SYMLINKS)
130
106
  {
131
 
    strcpy(buff,to);
132
 
    ssize_t sym_link_size= readlink(buff,to,FN_REFLEN-1);
133
 
    if (sym_link_size >= 0)
134
 
      to[sym_link_size]= '\0';
 
107
    strmov(buff,to);
 
108
    (void) my_readlink(to, buff, MYF(0));
135
109
  }
136
 
  return(to);
 
110
  DBUG_RETURN(to);
137
111
} /* fn_format */
138
112
 
139
113
 
146
120
{
147
121
  register const char * pos;
148
122
  register const char * found;
 
123
  DBUG_ENTER("strlength");
149
124
 
150
125
  pos= found= str;
151
126
 
163
138
    found=pos;
164
139
    while (*++pos == ' ') {};
165
140
  }
166
 
  return((size_t) (found - str));
 
141
  DBUG_RETURN((size_t) (found - str));
167
142
} /* strlength */
168
 
 
169
 
} /* namespace internal */
170
 
} /* namespace drizzled */