summaryrefslogtreecommitdiff
path: root/modules/string/src/c/stringsstrrev.c
blob: 6d92debb30a6c5e7d27d3ac73799f8f9cc09792d (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

/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) INRIA - Allan CORNET
 * Copyright (C) DIGITEO - 2009 - 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 <string.h>
#include <stdio.h>
#include "stringsstrrev.h"
#include "freeArrayOfString.h"
#include "MALLOC.h"
#include "charEncoding.h"
#ifdef _MSC_VER
#include "strdup_windows.h"
#endif
/*----------------------------------------------------------------------------*/
char **strings_strrev(char **Input_strings, int Dim_Input_strings)
{
    char **Output_strings = NULL;
    if (Input_strings)
    {
        Output_strings = (char **)MALLOC(sizeof(char*) * Dim_Input_strings);
        if (Output_strings)
        {
            int i = 0;
            for (i = 0; i < Dim_Input_strings; i++)
            {
                Output_strings[i] = scistrrev(Input_strings[i]);
                if (Output_strings[i] == NULL)
                {
                    freeArrayOfString(Output_strings, i);
                    return NULL;
                }
            }
        }
    }
    return Output_strings;
}
/*----------------------------------------------------------------------------*/
char* scistrrev(char* str)
{
    char *revstr = NULL;
    if (str)
    {
        wchar_t *wcstr = to_wide_string(str);
#ifdef _MSC_VER
        wchar_t *wcrevstr = _wcsrev(wcstr);
        revstr = wide_string_to_UTF8(wcrevstr);
#else
        int i = 0;
        int t = 0;
        int j = 0, k = 0;

        if (wcstr)
        {
            i = (int)wcslen(wcstr);
        }
        t = !(i % 2) ? 1 : 0;   // check the length of the string .

        /* copy character by character to reverse string */
        k = 0;
        for (j = i - 1; j > (i / 2 - t) ; j-- )
        {
            /* j starts from end of string */
            /* k starts from beginning of string */
            wchar_t ch  = wcstr[j]; /* ch temp. character */
            wcstr[j]   = wcstr[k]; /* end and beginning characters are exchanged */
            wcstr[k++] = ch;
        }
        revstr = wide_string_to_UTF8(wcstr);
#endif
        FREE(wcstr);
    }
    return revstr;
}
/*----------------------------------------------------------------------------*/