]> git.lyx.org Git - lyx.git/blob - src/support/mkdir.cpp
some de-boostification
[lyx.git] / src / support / mkdir.cpp
1 /**
2  * \file mkdir.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/lyxlib.h"
14 #include "support/FileName.h"
15
16 #ifdef HAVE_SYS_STAT_H
17 # include <sys/stat.h>
18 #endif
19 #ifdef HAVE_SYS_TYPES_H
20 # include <sys/types.h>
21 #endif
22 #include <fcntl.h>
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26 #ifdef HAVE_DIRECT_H
27 # include <direct.h>
28 #endif
29 #ifdef _WIN32
30 # include <windows.h>
31 #endif
32
33 namespace lyx {
34 namespace support {
35
36
37 int mymkdir(char const * pathname, unsigned long int mode)
38 {
39         // FIXME: why don't we have mode_t in lyx::mkdir prototype ??
40 #if HAVE_MKDIR
41 # if MKDIR_TAKES_ONE_ARG
42         // MinGW32
43         return ::mkdir(pathname);
44         // FIXME: "Permissions of created directories are ignored on this system."
45 # else
46         // POSIX
47         return ::mkdir(pathname, mode_t(mode));
48 # endif
49 #elif defined(_WIN32)
50         // plain Windows 32
51         return CreateDirectory(pathname, 0) != 0 ? 0 : -1;
52         // FIXME: "Permissions of created directories are ignored on this system."
53 #elif HAVE__MKDIR
54         return ::_mkdir(pathname);
55         // FIXME: "Permissions of created directories are ignored on this system."
56 #else
57 #   error "Don't know how to create a directory on this system."
58 #endif
59
60 }
61
62 int mkdir(FileName const & pathname, unsigned long int mode)
63 {
64         return mymkdir(pathname.toFilesystemEncoding().c_str(), mode);
65 }
66
67
68 // adapted from zlib-1.2.3/contrib/minizip/miniunz.c
69 int makedir(char * newdir, unsigned long int mode)
70 {
71         char *buffer;
72         char *p;
73         int     len = (int)strlen(newdir);
74
75         if (len <= 0)
76                 return 1;
77
78         buffer = (char*)malloc(len+1);
79         strcpy(buffer,newdir);
80
81         if (buffer[len-1] == '/')
82                 buffer[len-1] = '\0';
83         if (mymkdir(buffer, mode) == 0) {
84                 free(buffer);
85                 return 0;
86         }
87
88         p = buffer + 1;
89         while (1) {
90                 char hold;
91
92                 while(*p && *p != '\\' && *p != '/')
93                         p++;
94                 hold = *p;
95                 *p = 0;
96                 if (mymkdir(buffer, mode) != 0) {
97                         free(buffer);
98                         return 1;
99                 }
100                 if (hold == 0)
101                         break;
102                 *p++ = hold;
103         }
104         free(buffer);
105         return 0;
106 }
107
108
109 } // namespace support
110 } // namespace lyx