main.c 6.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * Main file used to generate the files specific to the project:
 *  - user model files:
 *     - UserModel.h
 *     - UserModel.c
 *  - id file 
 *
 * author: Nicolas Van der Noot, Aubain Verle, Nicolas Docquier
 * 
 * (c) Universite catholique de Louvain
 */        

#include "mbs_print_user_files.h"
    

16
17
18
19
20
21
22
 /**
  * Find the user folder corresponding to the given *.mbs file.
  * 
  * This function allocat a char* that must be freed by the user.
  * 
  */ 
char* find_user_path(char* mbsfile){
23
24
    int lastSep;
    char *ptr;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    char* userpath;
    char* c;
    
    // allocate space for the path to the user folder
    userpath = (char*)malloc(sizeof(char)*(strlen(mbsfile)+50));
    // copy path to the *.mbs file
    sprintf(userpath, "%s", mbsfile);
    
    // replace '\' by '/' if any (i.e. win compatibility)
    for(c=userpath ; *c !='\0' ; c++){
        if(*c == '\\'){
            *c = '/';
        }
    }
    
    // find the last occurence of '/'
    ptr = strrchr(userpath, '/');
    // mark the end of path to the last occurence
43
    *ptr = '\0';
44
45
46
47
48
49
50
    
    // find the previous occurence
    ptr = strrchr(userpath, '/');
    
    // check if 
    if(strcmp(ptr, "/dataR")==0){
        *ptr = '\0';
51
        sprintf(userpath, "%s/userfctR/", userpath);
52
53
54
55
56
57
58
59
        
    }
    else{
        printf("ERROR: the *.mbs file must be in the dataR folder. \n");
        exit(1);
    }
    
    return userpath;
60
61
62
63
64
65
66
67
68
}


/**
 * Generates 'UserModelStruct.c' and 'UserModelStruct.h'
 */
void generate_user_models(char* mbsfile){
    
    // variables declaration
69
    char *fileoutC, *fileoutH, *userpath;
70

71
72
73
    //struct stat mbs_stat, outC_stat, outH_stat;

    userpath = find_user_path(mbsfile);
74
75
76

    MDS_gen_strct* gen = NULL;

77
78
    // path to the file to be written;
    fileoutC     = (char*)malloc(sizeof(char)*(strlen(userpath)+50));
79
    sprintf(fileoutC, "%suser_model.c", userpath);
80
    fileoutH     = (char*)malloc(sizeof(char)*(strlen(userpath)+50));
81
    sprintf(fileoutH, "%suser_model.h", userpath);
82

83
84
    printf("user_model.c: %s \n", fileoutC);
    printf("user_model.h: %s \n", fileoutH);
85
86
87
88
89
90
91
92
93

    // print check empty file
    //print_empty_file(fileoutCheck);
/*
    if (stat(fileoutCheck, &mbs_stat))
    {
        printf("Problem: could not read the creation date of %s !\n", fileoutCheck);
        return;
*/
94
/*
95
96
97
98
99
100
101
102
103
104
105
    if (stat(fileoutC, &outC_stat))
    {
        printf("Problem: could not read the creation date of %s !\n", fileoutC);
        return;
    }

    if (stat(fileoutH, &outH_stat))
    {
        printf("Problem: could not read the creation date of %s !\n", fileoutH);
        return;
    }
106
107
108
*/
//    if ( (mbs_stat.st_mtime >= outC_stat.st_mtime) || (mbs_stat.st_mtime >= outH_stat.st_mtime) )
//    {
109
110
        gen =  MDS_mbs_reader(mbsfile);
        mbs_print_user_models(gen, fileoutC, fileoutH);
111
//    }
112

113
114
115
116
117
    free_MDS_gen_strct(gen);
    free(fileoutC);
    free(fileoutH);
    free(userpath);
    
118
119
120
121
122
123
124
125
}

/*
 * Generates 'user_all_id.h' 
 */
void generate_user_all_id(char* mbsfile)
{
    // variables declaration
126
127
128
129
130
    char *fileoutH, *userpath;

    //struct stat mbs_stat, outH_stat;

    userpath = find_user_path(mbsfile);
131
132
133
134
135
136


    MDS_gen_strct* gen;
    gen = NULL;

    // files initialization
137
138
139
    fileoutH     = (char*)malloc(sizeof(char)*(strlen(userpath)+50));
    sprintf(fileoutH, "%suser_all_id.h", userpath);
    
140
141
    // print check empty file
    //print_empty_file(fileoutCheck);
142
/*
143
144
145
146
147
148
149
150
151
152
153
    if (stat(fileoutCheck, &mbs_stat))
    {
        printf("Problem: could not read the creation date of %s !\n", fileoutCheck);
        return;
    }

    if (stat(fileoutH, &outH_stat)) 
    {
        printf("Problem: could not read the creation date of %s !\n", fileoutH);
        return;
    }
154
155
156
*/
//    if (mbs_stat.st_mtime >= outH_stat.st_mtime)
//    {
157
158
        gen =  MDS_mbs_reader(mbsfile);
        mbs_print_user_all_id(gen, fileoutH);
159
160
161
162
163
//    }
    free_MDS_gen_strct(gen);
    free(fileoutH);
    free(userpath);
    
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
/*! \brief Generates 'user_hardcoded.h'
 * 
 * \param[in] mbsfile f.mbs file (with path) to use
 */
void generate_user_hardcoded(char* mbsfile)
{
    // variables declaration
    char *fileoutH, *userpath;

    //struct stat mbs_stat, outH_stat;
    userpath = find_user_path(mbsfile);

    MDS_gen_strct* gen;
    gen = NULL;

    // files initialization
    fileoutH = (char*) malloc(sizeof(char)*(strlen(userpath)+50));
    sprintf(fileoutH, "%suser_hardcoded.h", userpath);

    gen = MDS_mbs_reader(mbsfile);
    mbs_print_user_hardcoded(gen, fileoutH);

    free_MDS_gen_strct(gen);
    free(fileoutH);
    free(userpath);
}
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

/**
 * 
 * Print the UserModel files and/or the ID header file for
 * the given mbs file.
 * 
 * argv[0]: name of the program
 * 
 * argv[1]: path to the *.mbs for which user files must be generated
 * 
 * argv[2] (optional): flag specifying which file must be generated:
 *   - "user_models": user model files
 *   - "user_all_id": id header file
 *   - "all": both of the previous option
 * if argv[2] is not given, the "all" option is used
 */ 
 
int main(int argc, char *argv[])
{

212
213
214
215
216
217
218
219
220
    char* mbsFile;
    
    if (argc==1){
        mbsFile = "/home/ndocquier/Documents/MBsysC/MBprojects/PendulumSpringC/dataR/PendulumSpringC.mbs";
    }
    else{
        mbsFile = argv[1];
    }
    //find_user_path(argv[1]);
221
222
223
224
225
226
227

    if (argc > 3)
    {
        printf("Error: only one argument required, but received %d arguments !\n", argc-1);
    }
    else
    {
228
        if ( argc<3 || !strcmp(argv[2], "all") )
229
        {
230
231
            generate_user_models(mbsFile);
            generate_user_all_id(mbsFile);
232
            generate_user_hardcoded(mbsFile);
233
234
235
        }
        else if ( !strcmp(argv[2], "user_models") )
        {
236
            generate_user_models(mbsFile);
237
238
239
        }
        else if ( !strcmp(argv[2], "user_all_id") )
        {
240
            generate_user_all_id(mbsFile);
241
        }
242
243
244
245
        else if ( !strcmp(argv[2], "user_hardcoded") )
        {
            generate_user_hardcoded(mbsFile);
        }
246
247
248
        else
        {
            printf("Error: %s is not a good argument !\n", argv[2]);
249
            printf("Arguments accepted: 'all', 'user_models', 'user_all_id' and 'user_hardcoded' (without '')\n");
250
        }
251
    }
252
253
    return 0;
}