Project

General

Profile

1 2596 stephane
/*! \file scol_gbase.c
2
*	\brief all functions of this API
3
*   \author Stephane Bisaro
4
*/
5 2581 stephane
/*
6
This source file is part of Scol
7
For the latest info, see http://www.scolring.org
8
9
Copyright (c) 2010 Stephane Bisaro, aka Iri <iri@irizone.net>
10
11
This program is free software; you can redistribute it and/or modify it under
12
the terms of the GNU Lesser General Public License as published by the Free Software
13
Foundation; either version 2 of the License, or (at your option) any later
14
version.
15
16
This program is distributed in the hope that it will be useful, but WITHOUT
17
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19
20
You should have received a copy of the GNU Lesser General Public License along with
21
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
23
http://www.gnu.org/copyleft/lesser.txt
24
25
For others informations, please contact us from http://www.scolring.org/
26 2836 stephane
*/
27
28 2596 stephane
#include "../include/scol_gbase.h"
29
30 2836 stephane
#ifdef __cplusplus
31
#error This source file is not C++ but rather C. Please use a C-compiler
32
#endif
33
34
#if ((defined _WIN32) || (defined __WIN32__))
35
cbmachine ww;
36
#endif
37
mmachine  mm;
38
39
40
/**
41
 * \brief _gbaseUserName : Returns the user (or real) name
42
 *
43
 * fun [] S
44
 *
45
 * \param void : nothing
46
 * \return S : this name
47
 */
48
int SCOL_gbaseUserName (mmachine m)
49
{
50
    const gchar * name;
51
52
    name = g_get_user_name ();
53
    if (name == NULL)
54
        name = g_get_real_name ();
55
56
    if (name == NULL)
57
    {
58
        Mpushstrbloc (m, "unknown");
59
        return 0;
60
    }
61
    Mpushstrbloc (m, (char *) name);
62
    return 0;
63
}
64
65
/**
66
 * \brief _gBaseHostName : Returns the host name
67
 *
68
 * fun [] S
69
 *
70
 * \param void : nothing
71
 * \return S : this name
72
 */
73
int SCOL_gBaseHostName (mmachine m)
74
{
75
    const gchar * name;
76
77
    name = g_get_host_name ();
78
    if (name == NULL)
79
    {
80
        Mpushstrbloc (m, "unknown");
81
        return 0;
82
    }
83
    Mpushstrbloc (m, (char *) name);
84
    return 0;
85
}
86
87
/**
88
 * \brief _gBaseDirCur : Returns the current directory (by default on MS Windows "C:\Program Files\Scol Voyager")
89
 *
90
 * fun [] S
91
 *
92
 * \param void : nothing
93
 * \return S : this directory or "unknown" if undefined
94
 */
95
int SCOL_gBaseDirCur (mmachine m)
96
{
97
    const gchar * name;
98
99
    name = g_get_current_dir ();
100
    if (name == NULL)
101
    {
102
        Mpushstrbloc (m, "unknown");
103
        return 0;
104
    }
105
    Mpushstrbloc (m, (char *) name);
106
    return 0;
107
}
108
109
/**
110
 * \brief _gBasePathAbsolute : Returns 1 if the given file_name is an absolute file name.
111
 *
112
 * fun [S] I
113
 *
114
 * \param S : any path
115
 * \return I : 1 if TRUE,otherwise 0
116
 */
117
 int SCOL_gBasePathAbsolute (mmachine m)
118
 {
119
    int mpath;
120
    gboolean value;
121
122
    mpath = MMpull (m);
123
    if (mpath == NIL)
124
    {
125
        MMpush (m, NIL);
126
        return 0;
127
    }
128
129
    value = g_path_is_absolute (MMstartstr (m, MTOP (mpath)));
130
    MMpush (m, ITOM (value));
131
    return 0;
132
 }
133
134
/**
135
 * \brief _gBasePathBase : Returns the last component of the filename
136
 *
137
 * fun [S] S
138
 *
139
 * \param S : any path
140
 * \return S : this last component
141
 */
142
int SCOL_gBasePathBase (mmachine m)
143
{
144
    int mpath;
145
    gchar *base;
146
147
    mpath = MMpull (m);
148
    if (mpath == NIL)
149
    {
150
        MMpush (m, NIL);
151
        return 0;
152
    }
153
    base = g_path_get_basename (MMstartstr (m, MTOP (mpath)));
154
    Mpushstrbloc (m, base);
155
    g_free (base);
156
    return 0;
157
}
158
159
/**
160
 * \brief _gBasePathDir : Returns the directory components of a file name.
161
 *
162
 * fun [S] S
163
 *
164
 * \param S : any path
165
 * \return S : this directory
166
 */
167
int SCOL_gBasePathDir (mmachine m)
168
{
169
    int mpath;
170
    gchar *dir;
171
172
    mpath = MMpull (m);
173
    if (mpath == NIL)
174
    {
175
        MMpush (m, NIL);
176
        return 0;
177
    }
178
    dir = g_path_get_dirname (MMstartstr (m, MTOP (mpath)));
179
    Mpushstrbloc (m, dir);
180
    g_free (dir);
181
    return 0;
182
}
183
184
/**
185
 * \brief _gBasePathBuild : Creates a filename from a list of elements using the correct separator for filenames.
186
 *
187
 * fun [[S r1] I] S
188
 *
189
 * \param [S r1] : a list of elements (typically, a list of directory = a :: b :: c :: file.ext)
190
 * \param I : 1 = OS separator (e.g. "\" on MS Windows, "/" on Linux), otherwise Scol separator ("/")
191
 * \return S : the filename
192
 */
193
int SCOL_gBasePathBuild (mmachine m)
194
{
195
    int mlist, mflag;
196
    int size = 0, i = 0;
197
    gchar **list;
198
    gchar *fn;
199
200
    mflag = MTOI (MMpull (m));
201
    mlist = MMget (m, 0);
202
    if (mlist == NIL)
203
    {
204
        MMpush (m, NIL);
205
        return 0;
206
    }
207
    mlist = MTOP (mlist);
208
209
    while (mlist != NIL)
210
    {
211
        size++;
212
        mlist = MMfetch (m, mlist, 1)>>1;
213
    }
214
215
    list = g_new0 (gchar*, size + 2);
216
    mlist = MTOP (MMpull (m));
217
    for (; i < size; i++)
218
    {
219
        list[i] = MMstartstr (m, MTOP (MMfetch (m, mlist, 0)));
220
        mlist = MTOP (MMfetch (m, mlist, 1));
221
    }
222
223
    if (mflag == 1)
224
        fn = g_build_filenamev (list);
225
    else
226
        fn = g_build_pathv ("/", list);
227
    Mpushstrbloc (m, fn);
228
    g_free (list);
229
    g_free (fn);
230
    return 0;
231
}
232
233
/**
234
 * \brief _gBaseStringUTF8 : Converts a string in the current locale into a UTF-8 string or reverse.
235
 *
236
 * fun [S I] S
237
 *
238
 * \param S : any string
239
 * \param I : 1 from locale to UTF8, 0 from UTF-8 to current locale
240
 * \return S : the new string
241
 */
242
int SCOL_gBaseStringUTF8 (mmachine m)
243
{
244
    int mstring, mflag;
245
    gchar *string;
246
247
    mflag = MMpull (m);
248
    mstring = MMpull (m);
249
250
    if (mstring == NIL)
251
    {
252
        MMpush (m, NIL);
253
        return 0;
254
    }
255
    if (mflag)
256
        string = SCOLUTF8 (MMstartstr (m, MTOP (mstring)), -1);
257
    else
258
        string = UTF8SCOL (MMstartstr (m, MTOP (mstring)), -1);
259
    Mpushstrbloc (m, string);
260
    g_free (string);
261
    return 0;
262
}
263
264
/**
265
 * \brief _gbaseChecksumS : Computes the checksum of a string.
266
 *
267
 * fun [S I] S
268
 *
269
 * \param S : any string
270
 * \param I : the hashing algorithm to use = 0 -> MD5 (default), 1 -> SHA-1, 2 -> SHA-256
271
 * \return S : the checksum as an hexadecimal string.
272
 */
273
int SCOL_gbaseChecksumS (mmachine m)
274
{
275
    int mstring, mtype;
276
    GChecksumType type = G_CHECKSUM_MD5;
277
    gchar *string;
278
279
    mtype = MTOI (MMpull (m));
280
    mstring = MMpull (m);
281
282
    if (mstring == NIL)
283
    {
284
        MMpush (m, NIL);
285
        return 0;
286
    }
287
    mstring = MTOP (mstring);
288
289
    if (mtype == 1)
290
        type = G_CHECKSUM_SHA1;
291
    else if (mtype == 2)
292
        type = G_CHECKSUM_SHA256;
293
294
    string = g_compute_checksum_for_string (type, MMstartstr (m, mstring), -1);
295
    Mpushstrbloc (m, string);
296
    g_free (string);
297
    return 0;
298
}
299
300
/**
301
 * \brief _gbaseChecksumP : Computes the checksum of a file.
302
 *
303
 * fun [P I] S
304
 *
305
 * \param S : any file (read referenced only)
306
 * \param I : the hashing algorithm to use = 0 -> MD5 (default), 1 -> SHA-1, 2 -> SHA-256
307
 * \return S : the checksum as an hexadecimal string.
308
 */
309
int SCOL_gbaseChecksumP (mmachine m)
310
{
311
    /* #define SCSP_MAX_SIZE G_MAXUINT-1 */ /* i'm stupid ! */
312
    #define SCSP_MAX_SIZE 1024*32   /* 32 ko / each loop */
313
    int mfile, mtype;
314
    guchar data[SCSP_MAX_SIZE];
315
    gsize size = 0;
316
    GChecksumType type = G_CHECKSUM_MD5;
317
    GChecksum *cs;
318
    const gchar *string;
319
    FILE *file;
320
321
    mtype = MTOI (MMpull (m));
322
    mfile = MMpull (m);
323
324
    if (mfile == NIL)
325
    {
326
        MMpush (m, NIL);
327
        return 0;
328
    }
329
    mfile = MTOP (mfile);
330
331
    file = fopen (MMstartstr (m, mfile), "rb");
332
    if (file == NULL)
333
    {
334
        MMpush (m, NIL);
335
        return 0;
336
    }
337
338
    if (mtype == 1)
339
        type = G_CHECKSUM_SHA1;
340
    else if (mtype == 2)
341
        type = G_CHECKSUM_SHA256;
342
343
    cs = g_checksum_new (type);
344
    do
345
    {
346
        size = fread((void *) data, sizeof (guchar), SCSP_MAX_SIZE, file);
347
        g_checksum_update (cs, data, size);
348
    }
349
    while (size == SCSP_MAX_SIZE);
350
    fclose (file);
351
352
    string = g_checksum_get_string (cs);
353
    Mpushstrbloc (m, (char *) string);
354
    g_checksum_free (cs);
355
    return 0;
356
}
357
358
/**
359
 * \brief _gbaseSleep : Pauses the current thread for the given number of milliseconds.
360
 *
361
 * fun [I] I
362
 *
363
 * \param I : the number of milliseconds
364
 * \return I : 0 if success, nil if error
365
 */
366
int SCOL_gbaseSleep (mmachine m)
367
{
368
    int msleep;
369
370
    msleep = MTOI (MMpull (m));
371
    if (msleep <= 0)
372
    {
373
        MMpush (m, NIL);
374
        return 0;
375
    }
376
377
    g_usleep (1000*msleep);
378
    MMpush (m, ITOM (0));
379
    return 0;
380
}
381
382
/**
383
 * \brief _gbaseFileStat : it should not be used, at this time
384
 *
385
 * fun [P] [I I I I]
386
 *
387
 * \param P : a filename (read reference only)
388
 * \return [I I I I] : size, last access, last modification, last state changed
389
 */
390
int SCOL_gbaseFileStat (mmachine m)
391
{
392
    int mfile;
393 2589 stephane
    int result;
394 2836 stephane
    #if ((defined _WIN32) || (defined __WIN32__))
395 2589 stephane
    GStatBuf *s = NULL;
396
    #else
397
    struct stat *s = NULL;
398 2836 stephane
    #endif
399
400
    mfile = MMpull(m);
401
    if (mfile == NIL)
402
    {
403
        MMpush (m, NIL);
404
        return 0;
405
    }
406
    mfile = MTOP (mfile);
407
408
    result = g_stat (MMstartstr (m, mfile), s);
409
    if (result != 0)
410
    {
411
        MMpush (m, NIL);
412
        return 0;
413
    }
414
    MMpush (m, ITOM (s->st_size));
415
    MMpush (m, ITOM (s->st_atime));
416
    MMpush (m, ITOM (s->st_mtime));
417
    MMpush (m, ITOM (s->st_ctime));
418
    MMpush (m, ITOM (4));
419
    MBdeftab (m);
420
421
    return 0;
422
}
423
424
/**
425
 * \brief _gbaseIsIp : Tests if a string is a form of an IPv4 or IPv6 address (like "123.210.012.231").
426
 * fun [S] I
427
 * \param S : any string
428
 * \return I : 1 if TRUE (the string is an IP address), 0 if FALSE
429
 */
430
int SCOL_gbaseIsIp (mmachine m)
431
{
432
    int ms;
433
434
    ms = MMpull (m);
435
    if (ms == NIL)
436
    {
437
        MMpush (m, NIL);
438
        return 0;
439
    }
440
    MMpush (m, ITOM (g_hostname_is_ip_address (MMstartstr (m, MTOP (ms)))));
441
    return 0;
442
}
443
444
445
446
447
448
449 2581 stephane
/* API d?finitions : */
450
451
char* gbase_name[GBASE_PKG_NB]=
452
{
453 2836 stephane
    "_gbaseUserName",
454
    "_gBaseHostName",
455
    "_gBaseDirCur",
456
    "_gBasePathAbsolute",
457
    "_gBasePathBase",
458
    "_gBasePathDir",
459
    "_gBasePathBuild",
460
    "_gBaseStringUTF8",
461
    "_gbaseChecksumS",
462
    "_gbaseChecksumP",
463
    "_gbaseSleep",
464
    "_gbaseFileStat",
465 2581 stephane
    "_gbaseIsIp"
466
};
467
468
int (*gbase_fun[GBASE_PKG_NB])(mmachine m)=
469
{
470 2836 stephane
    SCOL_gbaseUserName,
471
    SCOL_gBaseHostName,
472
    SCOL_gBaseDirCur,
473
    SCOL_gBasePathAbsolute,
474
    SCOL_gBasePathBase,
475
    SCOL_gBasePathDir,
476
    SCOL_gBasePathBuild,
477
    SCOL_gBaseStringUTF8,
478
    SCOL_gbaseChecksumS,
479
    SCOL_gbaseChecksumP,
480
    SCOL_gbaseSleep,
481
    SCOL_gbaseFileStat,
482 2581 stephane
    SCOL_gbaseIsIp
483
};
484
485
int gbase_narg[GBASE_PKG_NB]=
486
{
487 2836 stephane
    0,      /* SCOL_gbaseUserName */
488
    0,      /* SCOL_gBaseHostName */
489
    0,      /* SCOL_gBaseDirCur */
490
    1,      /* SCOL_gBasePathAbsolute */
491
    1,      /* SCOL_gBasePathBase */
492
    1,      /* SCOL_gBasePathDir */
493
    2,      /* SCOL_gBasePathBuild */
494
    2,       /* SCOL_gBaseStringUTF8 */
495
    2,       /* SCOL_gbaseChecksumS */
496
    2,       /* SCOL_gbaseChecksumP */
497
    1,       /* SCOL_gbaseSleep */
498
    1,       /* SCOL_gbaseFileStat */
499 2581 stephane
    1       /* SCOL_gbaseIsIp */
500
};
501
502
char* gbase_type[GBASE_PKG_NB]=
503
{
504 2836 stephane
    "fun [] S",                     /* SCOL_gbaseUserName */
505
    "fun [] S",                     /* SCOL_gBaseHostName */
506
    "fun [] S",                     /* SCOL_gBaseDirCur */
507
    "fun [S] I",                    /* SCOL_gBasePathAbsolute */
508
    "fun [S] S",                    /* SCOL_gBasePathBase */
509
    "fun [S] S",                    /* SCOL_gBasePathDir */
510
    "fun [[S r1] I] S",             /* SCOL_gBasePathBuild */
511
    "fun [S I] S",                  /* SCOL_gBaseStringUTF8 */
512
    "fun [S I] S",                  /* SCOL_gbaseChecksumS */
513
    "fun [P I] S",                  /* SCOL_gbaseChecksumS */
514
    "fun [I] I",                    /* SCOL_gbaseSleep */
515
    "fun [P] [I I I I]",            /* SCOL_gbaseFileStat */
516 2581 stephane
    "fun [S] I"                    /* SCOL_gbaseIsIp */
517
};
518
519
int SCOLinitGbaseClass (mmachine m)
520
{
521
    int k;
522
523
    MMechostr (0, "SCOLinitGbaseClass : entering\n");
524
525
    k = PKhardpak (m, "GBaseEngine", GBASE_PKG_NB, gbase_name, gbase_fun, gbase_narg, gbase_type);
526
    return k;
527 2836 stephane
}
528
529
530
531
532 3137 stephane
int GBaseRelease ()
533 2836 stephane
{
534 3137 stephane
    MMechostr (0, "\nGBASE library released !\n");
535 2836 stephane
    return 0;
536
}
537 3137 stephane
538
#if ((defined _WIN32) || (defined __WIN32__))
539
# define SCOL_GBASE_PLUGIN_EXPORT __declspec (dllexport)
540
#elif ((defined linux) || (defined __linux))
541
# define SCOL_GBASE_PLUGIN_EXPORT
542
#else
543
# error no platform supported
544
#endif
545
546
SCOL_GBASE_PLUGIN_EXPORT int SCOLloadGBASE(mmachine m, cbmachine w)
547
{
548
    ww = w;
549
    mm = m;
550
551
    SCOLinitplugin(w);
552
    return SCOLinitGbaseClass(m);
553
}
554
555
SCOL_GBASE_PLUGIN_EXPORT int SCOLfreeGBASE()
556
{
557
    return 0;
558
}