utf: console 2
[scilab.git] / scilab / modules / console / src / c / dropFiles.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2005-2008 - INRIA - Allan CORNET
4  * Copyright (C) 2012 - DIGITEO - Allan CORNET
5  *
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at
10  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11  *
12  */
13
14 /*--------------------------------------------------------------------------*/
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include "dropFiles.h"
19 #include "sci_malloc.h"
20 #include "storeCommand.h" /* storecommand */
21 #include "FindFileExtension.h"
22 #include "URIFileToFilename.h"
23 #include "with_module.h"
24 #include "os_string.h"
25 #include "charEncoding.h"
26 /*--------------------------------------------------------------------------*/
27 #define BIN_EXTENSION_FILE ".bin"
28 #define SAV_EXTENSION_FILE ".sav"
29 #define COS_EXTENSION_FILE ".cos"
30 #define XCOS_EXTENSION_FILE ".xcos"
31 #define COSF_EXTENSION_FILE ".cosf"
32 #define SCI_EXTENSION_FILE ".sci"
33 #define SCE_EXTENSION_FILE ".sce"
34 #define TST_EXTENSION_FILE ".tst"
35 #define DEM_EXTENSION_FILE ".dem"
36 #define SCG_EXTENSION_FILE ".scg"
37 #define ZCOS_EXTENSION_FILE ".zcos"
38 #define SOD_EXTENSION_FILE ".sod"
39 /*--------------------------------------------------------------------------*/
40 #define FORMAT_BIN_SCE_EXTENSION_FILES "load('%s');"
41 #define FORMAT_COS_COSF_XCOS_EXTENSION_FILES "xcos('%s');"
42 #define FORMAT_SCI_EXTENSION_FILES "exec('%s');"
43 #define FORMAT_SCE_TST_EXTENSION_FILES "exec('%s');"
44 #define FORMAT_SCG_EXTENSION_FILES "load('%s');"
45 #define FORMAT_UNKNOW_EXTENSION_FILES "disp(gettext('Unknown file type : %s'));"
46 #define XCOS_NOT_INSTALLED "disp(gettext('Please install xcos module.'))"
47 /*--------------------------------------------------------------------------*/
48 static char *getCommandByFileExtension(char *File, char *FileExtension);
49 static char *buildCommand(char *format, char *filename);
50 static BOOL LaunchFilebyExtension(char *File);
51 /*--------------------------------------------------------------------------*/
52 BOOL dropFiles(char **files)
53 {
54     int len = 0;
55
56     while (files[len])
57     {
58         char *convertfile = URIFileToFilename(files[len]);
59
60         if (convertfile)
61         {
62             BOOL bCheck = LaunchFilebyExtension(convertfile);
63             if (convertfile)
64             {
65                 FREE(convertfile);
66                 convertfile = NULL;
67             }
68             if (!bCheck)
69             {
70                 return bCheck;
71             }
72         }
73         len++;
74     }
75     return TRUE;
76 }
77 /*--------------------------------------------------------------------------*/
78 BOOL LaunchFilebyExtension(char *File)
79 {
80     BOOL bOK = FALSE;
81
82     char *CommandLine = NULL;
83     char *FileExtension = NULL;
84
85     FileExtension = FindFileExtension(File);
86     CommandLine = getCommandByFileExtension(File, FileExtension);
87
88     if (CommandLine)
89     {
90         StoreCommand(CommandLine);
91         bOK = TRUE;
92
93         FREE(CommandLine);
94         CommandLine = NULL;
95     }
96
97     if (FileExtension)
98     {
99         FREE(CommandLine);
100         CommandLine = NULL;
101     }
102
103     return bOK;
104 }
105 /*--------------------------------------------------------------------------*/
106 static char *getCommandByFileExtension(char *File, char *FileExtension)
107 {
108     char *command = NULL;
109
110     if (FileExtension)
111     {
112         if ( (stricmp(FileExtension, BIN_EXTENSION_FILE) == 0) ||
113                 (stricmp(FileExtension, SAV_EXTENSION_FILE) == 0) ||
114                 (stricmp(FileExtension, SOD_EXTENSION_FILE) == 0))
115         {
116             command = buildCommand(FORMAT_BIN_SCE_EXTENSION_FILES, File);
117         }
118         else if ( (stricmp(FileExtension, COS_EXTENSION_FILE) == 0) ||
119                   (stricmp(FileExtension, COSF_EXTENSION_FILE) == 0) ||
120                   (stricmp(FileExtension, ZCOS_EXTENSION_FILE) == 0) ||
121                   (stricmp(FileExtension, XCOS_EXTENSION_FILE) == 0))
122         {
123             if (with_module("xcos"))
124             {
125                 command = buildCommand(FORMAT_COS_COSF_XCOS_EXTENSION_FILES, File);
126             }
127             else
128             {
129                 command = os_strdup(XCOS_NOT_INSTALLED);
130             }
131         }
132         else if (stricmp(FileExtension, SCI_EXTENSION_FILE) == 0)
133         {
134             command = buildCommand(FORMAT_SCI_EXTENSION_FILES, File);
135         }
136         else if ( (stricmp(FileExtension, SCE_EXTENSION_FILE) == 0) || (stricmp(FileExtension, TST_EXTENSION_FILE) == 0) || (stricmp(FileExtension, DEM_EXTENSION_FILE) == 0) )
137         {
138             command = buildCommand(FORMAT_SCE_TST_EXTENSION_FILES, File);
139         }
140         else if (stricmp(FileExtension, SCG_EXTENSION_FILE) == 0)
141         {
142             command = buildCommand(FORMAT_SCG_EXTENSION_FILES, File);
143         }
144         else
145         {
146             command = buildCommand(FORMAT_UNKNOW_EXTENSION_FILES, File);
147         }
148     }
149     return command;
150 }
151 /*--------------------------------------------------------------------------*/
152 static char *buildCommand(char *format, char *filename)
153 {
154     char *command = NULL;
155
156     if (format && filename)
157     {
158         command = (char*)MALLOC( (strlen(filename) + strlen(format) + 1) * sizeof(char) );
159         if (command)
160         {
161             sprintf(command, format, filename);
162         }
163     }
164
165     return command;
166 }
167 /*--------------------------------------------------------------------------*/