summaryrefslogtreecommitdiffstats
path: root/scilab/modules/windows_tools/src/c/scilab_windows/FilesAssociations.c
blob: 8caa862a0fd965202faef61b069d4ddc934176d1 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) INRIA - Allan CORNET
* Copyright (C) DIGITEO - 2009-2010 - 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
*
*/

/*--------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "TextToPrint.h"
#include <windows.h>
#include <Winuser.h>
#include <shlwapi.h>
#include "version.h"
#include "FilesAssociations.h"
#include "sci_malloc.h"
#include "FindScilab.h"
#include "wmcopydata.h"
#include "os_string.h"
#include "MutexClosingScilab.h"
#include "with_module.h"
#include "FileExist.h"
#include "getshortpathname.h"
/*--------------------------------------------------------------------------*/
static void ReplaceSlash(char *pathout, char *pathin);
static void ExtensionFileIntoLowerCase(char *fichier);
static BOOL isGoodExtension(char *chainefichier, char *ext);
static BOOL IsAScicosFileCOS(char *chainefichier);
static BOOL IsAScicosFileCOSF(char *chainefichier);
static BOOL IsAScicosFileXCOS(char *chainefichier);
static BOOL IsAScicosFileZCOS(char *chainefichier);
static BOOL IsASciNotesFileSCE(char *chainefichier);
static BOOL IsASciNotesFileSCI(char *chainefichier);
static BOOL IsASciNotesFileTST(char *chainefichier);
/*--------------------------------------------------------------------------*/
#define MSG_SCIMSG1 "%s -e load(getlongpathname('%s'));disp(getlongpathname('%s')+ascii(32)+'loaded');"
#define MSG_SCIMSG2_XCOS "%s -e xcos(getlongpathname('%s'));"
#define MSG_SCIMSG3_XCOS "execstr('xcos(getlongpathname(''%s''));','errcatch');"
#define MSG_SCIMSG4 "%s -e exec(getlongpathname('%s'));"
#define MSG_SCIMSG5_EDITOR "%s -e editor(getlongpathname('%s'));"
/* we try to launch scilab editor */
#define MSG_SCIMSG6_EDITOR "execstr('editor(getlongpathname(''%s''));','errcatch');"
#define MSG_SCIMSG7 "Scilab Communication"
/*--------------------------------------------------------------------------*/
/* Teste si le fichier a une extension .sav ou .bin*/
/* retourne TRUE si c'est le cas sinon FALSE */
BOOL IsABinOrSavFile(char *chainefichier)
{
    if (isGoodExtension(chainefichier, ".BIN") || isGoodExtension(chainefichier, ".SAV")
            || isGoodExtension(chainefichier, ".SOD"))
    {
        return TRUE;
    }
    return FALSE;
}
/*--------------------------------------------------------------------------*/
BOOL IsAScicosFile(char *chainefichier)
{
    if (IsAScicosFileCOS(chainefichier) ||
            IsAScicosFileCOSF(chainefichier) ||
            IsAScicosFileXCOS(chainefichier) ||
            IsAScicosFileZCOS(chainefichier))
    {
        return TRUE;
    }
    return FALSE;
}
/*--------------------------------------------------------------------------*/
BOOL IsAScicosFileCOS(char *chainefichier)
{
    return isGoodExtension(chainefichier, ".COS");
}
/*--------------------------------------------------------------------------*/
BOOL IsAScicosFileCOSF(char *chainefichier)
{
    return isGoodExtension(chainefichier, ".COSF");
}
/*--------------------------------------------------------------------------*/
BOOL IsAScicosFileXCOS(char *chainefichier)
{
    return isGoodExtension(chainefichier, ".XCOS");
}
/*--------------------------------------------------------------------------*/
BOOL IsAScicosFileZCOS(char *chainefichier)
{
    return isGoodExtension(chainefichier, ".ZCOS");
}
/*--------------------------------------------------------------------------*/
BOOL IsASciNotesFile(char *chainefichier)
{
    if (IsASciNotesFileSCE(chainefichier) ||
            IsASciNotesFileSCI(chainefichier) ||
            IsASciNotesFileTST(chainefichier))
    {
        return TRUE;
    }
    return FALSE;
}
/*--------------------------------------------------------------------------*/
BOOL IsASciNotesFileSCE(char *chainefichier)
{
    return isGoodExtension(chainefichier, ".SCE");
}
/*--------------------------------------------------------------------------*/
BOOL IsASciNotesFileSCI(char *chainefichier)
{
    return isGoodExtension(chainefichier, ".SCI");
}
/*--------------------------------------------------------------------------*/
BOOL IsASciNotesFileTST(char *chainefichier)
{
    return isGoodExtension(chainefichier, ".TST");
}
/*--------------------------------------------------------------------------*/
int CommandByFileExtension(char *fichier, int OpenCode, char *Cmd)
{
    int ReturnedValue = 0;
    if (FileExist(fichier))
    {
        BOOL bConverted = FALSE;
        char FinalFileName[(MAX_PATH * 2) + 1];
        char *ShortPath = NULL;
        char PathWScilex[(MAX_PATH * 2) + 1];


        /* Recuperation du nom du fichier au format 8.3 */
        ShortPath = getshortpathname(fichier, &bConverted);
        GetShortPathName(fichier, ShortPath, MAX_PATH);
        ReplaceSlash(FinalFileName, ShortPath);
        if (ShortPath)
        {
            FREE(ShortPath);
            ShortPath = NULL;
        }

        GetModuleFileName ((HINSTANCE)GetModuleHandle(NULL), PathWScilex, MAX_PATH);
        ReturnedValue = 1;

        switch (OpenCode)
        {
            case 0:
            default: /* -O Open file with editor */
            {
                if (!HaveAnotherWindowScilab() || haveMutexClosingScilab())
                {
                    if (with_module(L"scinotes"))
                    {
                        wsprintf(Cmd, MSG_SCIMSG5_EDITOR, PathWScilex, FinalFileName);
                    }
                    else
                    {
                        MessageBox(NULL, "Please install editor module.", "Error", MB_ICONSTOP);
                        exit(0);
                    }
                }
                else
                {
                    char *ScilabDestination = NULL;

                    if (with_module(L"scinotes"))
                    {
                        wsprintf(Cmd, MSG_SCIMSG6_EDITOR, FinalFileName);
                    }
                    else
                    {
                        MessageBox(NULL, "Please install editor module.", "Error", MB_ICONSTOP);
                        exit(0);
                    }

                    ScilabDestination = getLastScilabFound();
                    if (ScilabDestination)
                    {
                        SendCommandToAnotherScilab(MSG_SCIMSG7, ScilabDestination, Cmd);
                        FREE(ScilabDestination);
                        exit(0);
                    }
                    else
                    {
                        if (with_module(L"scinotes"))
                        {
                            wsprintf(Cmd, MSG_SCIMSG5_EDITOR, PathWScilex, FinalFileName);
                        }
                        else
                        {
                            MessageBox(NULL, "Please install editor module.", "Error", MB_ICONSTOP);
                            exit(0);
                        }
                    }
                }
            }
            break;

            case 1: /* -X eXecute file */
            {
                if (IsABinOrSavFile(FinalFileName) == TRUE)
                {
                    /* C'est un fichier .BIN ou .SAV d'ou load */
                    wsprintf(Cmd, MSG_SCIMSG1, PathWScilex, FinalFileName, FinalFileName);
                }
                else
                {
                    if (IsAScicosFile(fichier) == TRUE)
                    {
                        ExtensionFileIntoLowerCase(FinalFileName);
                        if (!HaveAnotherWindowScilab() || haveMutexClosingScilab())
                        {
                            if (with_module(L"xcos"))
                            {
                                wsprintf(Cmd, MSG_SCIMSG2_XCOS, PathWScilex, FinalFileName);
                            }
                            else
                            {
                                MessageBox(NULL, "Please install xcos module.", "Error", MB_ICONSTOP);
                                exit(0);
                            }
                        }
                        else
                        {
                            char *ScilabDestination = NULL;

                            if (with_module(L"xcos"))
                            {
                                wsprintf(Cmd, MSG_SCIMSG3_XCOS, FinalFileName);
                            }
                            else
                            {
                                MessageBox(NULL, "Please install xcos module.", "Error", MB_ICONSTOP);
                                exit(0);
                            }

                            ScilabDestination = getLastScilabFound();
                            if (ScilabDestination)
                            {
                                SendCommandToAnotherScilab(MSG_SCIMSG7, ScilabDestination, Cmd);
                                FREE(ScilabDestination);
                                exit(0);
                            }
                            else
                            {
                                if (with_module(L"xcos"))
                                {
                                    wsprintf(Cmd, MSG_SCIMSG2_XCOS, PathWScilex, FinalFileName);
                                }
                                else
                                {
                                    MessageBox(NULL, "Please install xcos module.", "Error", MB_ICONSTOP);
                                    exit(0);
                                }
                            }
                        }
                    }
                    else
                    {
                        wsprintf(Cmd, MSG_SCIMSG4, PathWScilex, FinalFileName);
                    }
                }
            }
            break;

            case 2: /* -P Print file */
            {
                PrintFile(fichier);
                strcpy(Cmd, " ");
                exit(0);
            }
            break;
        }
    }
    return ReturnedValue;
}
/*--------------------------------------------------------------------------*/
static void ExtensionFileIntoLowerCase(char *fichier)
{
    char *tmpfile = NULL;
    char *buffer = NULL;
    char *lastdot = NULL;
    char *ext = NULL;

    tmpfile = os_strdup(fichier);
    buffer = strtok(tmpfile, ".");
    while (buffer = strtok(NULL, "."))
    {
        lastdot = buffer;
    }
    /* le dernier . permet d'avoir l'extension */
    ext = _strlwr(lastdot); /* Fichier en Majuscule */

    strcpy(&fichier[strlen(fichier) - strlen(ext)], ext);

    FREE(tmpfile);
}
/*--------------------------------------------------------------------------*/
static void ReplaceSlash(char *pathout, char *pathin)
{
    int i = 0;
    int len_pathin = (int)strlen(pathin);
    for (i = 0; i < len_pathin; i++)
    {
        if (pathin[i] == '\\')
        {
            pathout[i] = '/';
        }
        else
        {
            pathout[i] = pathin[i];
        }
    }
    pathout[i] = '\0';
}
/*--------------------------------------------------------------------------*/
static BOOL isGoodExtension(char *chainefichier, char *ext)
{
    char *ExtensionFilename = PathFindExtension(chainefichier);
    if (ExtensionFilename)
    {
        if (_stricmp(ExtensionFilename, ext) == 0)
        {
            return TRUE;
        }
    }
    return FALSE;
}
/*--------------------------------------------------------------------------*/