summaryrefslogtreecommitdiff
path: root/modules/fileio/macros/mkdir.sci
blob: a1d60929c23bca2055151708a70602063f05ee20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2007 - INRIA - Allan CORNET
// Copyright (C) 2010 - DIGITEO - Allan CORNET
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function [status, msg]=mkdir(varargin)
    // mkdir Make a directory
    //------------------------------------------------------------------------

    lhs  = argn(1);
    rhs  = argn(2);

    DirName  = "";
    NewDirName = "";
    status   = 0 ;
    msg  = "";

    select rhs

    case 0
        error(msprintf(gettext("%s: Wrong number of input argument(s).\n"), "mkdir"));
        break

    case 1
        NewDirName   = varargin(1);
        if type(NewDirName) <> 10 then
            error(999, msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "mkdir", 1));
        end

        if size(NewDirName, "*") <> 1 then
            error(999, msprintf(gettext("%s: Wrong size for input argument #%d: A string expected.\n"), "mkdir", 1));
        end

        NewDirName   = stripblanks(NewDirName, %T);
        if is_absolute_path(NewDirName) then
            NewDirectory = NewDirName;
        else
            NewDirectory = fullfile(pwd(),NewDirName);
        end
        break

    case 2
        DirName  = varargin(1);
        if type(DirName) <> 10 then
            error(999, msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "mkdir", 1));
        end

        if size(DirName, "*") <> 1 then
            error(999, msprintf(gettext("%s: Wrong size for input argument #%d: A string expected.\n"), "mkdir", 1));
        end

        NewDirName   = varargin(2);
        if type(NewDirName) <> 10 then
            error(999, msprintf(gettext("%s: Wrong type for input argument #%d: A string expected.\n"), "mkdir", 2));
        end

        if size(NewDirName, "*") <> 1 then
            error(999, msprintf(gettext("%s: Wrong size for input argument #%d: A string expected.\n"), "mkdir", 2));
        end

        NewDirName   = stripblanks(NewDirName, %T);
        DirName  = stripblanks(DirName, %T);
        NewDirectory = fullfile(DirName, NewDirName);
        break

    else
        error(msprintf(gettext("%s: Wrong number of input argument(s).\n"), "mkdir"));
    end

    NewDirectory = fullpath(NewDirectory);

    if ~isdir(NewDirectory) & ~isfile(NewDirectory) then
        // The directory does not exist
        status = 1;
    else
        // The directory or file exists
        if isfile(NewDirectory) then
            // it is a file
            status = -2;
        else
            // it is a directory
            status = 2;
        end
    end

    select status

    case 0
        break

    case 1
        bAddFirstDirSep = %f;
        subdirs = strsplit(NewDirectory, ["/" "\"]);
        if subdirs(1) == "" then
            subdirs(1) = [];
            bAddFirstDirSep = %t;
        end
        for i = 1:size(subdirs, "*")
            currentsubdir = strcat(subdirs(1:i), filesep());
            if bAddFirstDirSep then
                currentsubdir = filesep() + currentsubdir;
            end
            if ~isfile(currentsubdir) & ~isdir(currentsubdir) then
                status = createdir(currentsubdir);
                if ~status then
                    break
                end
            end
        end

        if ~isdir(NewDirectory) & ~isfile(NewDirectory) then
            status = createdir(NewDirectory);
        end

        if (~status) then
            msg  =  msprintf(gettext("%s: An error occurred: %s\n"),"mkdir",gettext("Impossible to create directory ")) + NewDirectory;
            status = 0;
        else
            msg  = "";
            status = 1;
        end
        break

    case 2
        msg  =  msprintf(gettext("%s: An error occurred: %s\n"),"mkdir",gettext("This directory already exists in ")) + NewDirectory;
        break

    case -2
        msg  =  msprintf(gettext("%s: An error occurred: %s\n"),"mkdir",gettext("A file with the same name already exists in ")) + DirName;
        break

    else
        msg  =  msprintf(gettext("%s: An error occurred: %s\n"),"mkdir",gettext("Impossible to create directory ")) + NewDirectory;
    end

endfunction