summaryrefslogtreecommitdiff
path: root/plugins/sid/sidplay-libs/libsidplay/src/sidtune/SidTuneTools.cpp
blob: 6a10006243515f44e52cc88db949fe0481624f1d (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
/*
 * /home/ms/files/source/libsidtune/RCS/SidTuneTools.cpp,v
 *
 * Copyright (C) Michael Schwendt <mschwendt@yahoo.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "SidTuneTools.h"

#ifdef HAVE_EXCEPTIONS
#   include <new>
#endif
#include <ctype.h>
#include <string.h>

// Own version of strdup, which uses new instead of malloc.
char* SidTuneTools::myStrDup(const char *source)
{
    char *dest;
#ifdef HAVE_EXCEPTIONS
    if ( (dest = new(std::nothrow) char[strlen(source)+1]) != 0)
#else
    if ( (dest = new char[strlen(source)+1]) != 0)
#endif
    {
        strcpy(dest,source);
    }
    return dest;
}

// Return pointer to file name position in complete path.
char* SidTuneTools::fileNameWithoutPath(char* s)
{
    int last_slash_pos = -1;
    for ( uint_least32_t pos = 0; pos < strlen(s); pos++ )
    {
#if defined(SID_FS_IS_COLON_AND_BACKSLASH_AND_SLASH)
        if ( s[pos] == ':' || s[pos] == '\\' ||
             s[pos] == '/' )
#elif defined(SID_FS_IS_COLON_AND_SLASH)
        if ( s[pos] == ':' || s[pos] == '/' )
#elif defined(SID_FS_IS_SLASH)
        if ( s[pos] == '/' )
#elif defined(SID_FS_IS_BACKSLASH)
        if ( s[pos] == '\\' )
#elif defined(SID_FS_IS_COLON)
        if ( s[pos] == ':' )
#else
#error Missing file/path separator definition.
#endif
        {
            last_slash_pos = pos;
        }
    }
    return( &s[last_slash_pos +1] );
}

// Return pointer to file name position in complete path.
// Special version: file separator = forward slash.
char* SidTuneTools::slashedFileNameWithoutPath(char* s)
{
    int last_slash_pos = -1;
    for ( uint_least32_t pos = 0; pos < strlen(s); pos++ )
    {
        if ( s[pos] == '/' )
        {
            last_slash_pos = pos;
        }
    }
    return( &s[last_slash_pos +1] );
}

// Return pointer to file name extension in path.
// The backwards-version.
char* SidTuneTools::fileExtOfPath(char* s)
{
    uint_least32_t last_dot_pos = strlen(s);  // assume no dot and append
    for ( int pos = last_dot_pos; pos >= 0; --pos )
    {
        if ( s[pos] == '.' )
        {
            last_dot_pos = pos;
            break;
        }
    }
    return( &s[last_dot_pos] );
}

// Parse input string stream. Read and convert a hexa-decimal number up 
// to a ``,'' or ``:'' or ``\0'' or end of stream.
uint_least32_t SidTuneTools::readHex( const char *s, int size, int &pos)
{
    uint_least32_t hexLong = 0;
    char c;
    do
    {
        if (pos >= size) {
            break;
        }
        c = s[pos++];
        if (( c != ',') && ( c != ':' ) && ( c != 0 ))
        {
            // machine independed to_upper
            c &= 0xdf;
            ( c < 0x3a ) ? ( c &= 0x0f ) : ( c -= ( 0x41 - 0x0a ));
            hexLong <<= 4;
            hexLong |= (uint_least32_t)c;
        }
        else
        {
            if ( c == 0 ) {
                pos--;
            }
            break;
        }
    }  while ( pos < size );
    return hexLong;
}

// Parse input string stream. Read and convert a decimal number up 
// to a ``,'' or ``:'' or ``\0'' or end of stream.
uint_least32_t SidTuneTools::readDec( const char *s, int size, int &pos )
{
    uint_least32_t hexLong = 0;
    char c;
    do
    {
        if (pos >= size) {
            break;
        }
        c = s[pos++];
        if (( c != ',') && ( c != ':' ) && ( c != 0 ))
        {
            c &= 0x0f;
            hexLong *= 10;
            hexLong += (uint_least32_t)c;
        }
        else
        { 
            if ( c == 0 ) {
                pos--;
            }
            break;
        }
    }  while ( pos < size);
    return hexLong;
}

// Search terminated string for next newline sequence.
// Skip it and return pointer to start of next line.
const char* SidTuneTools::returnNextLine(const char* s)
{
    // Unix: LF = 0x0A
    // Windows, DOS: CR,LF = 0x0D,0x0A
    // Mac: CR = 0x0D
    char c;
    while ((c = *s) != 0)
    {
        s++;                            // skip read character
        if (c == 0x0A)
        {
            break;                      // LF found
        }
        else if (c == 0x0D)
        {
            if (*s == 0x0A)
            {
                s++;                    // CR,LF found, skip LF
            }
            break;                      // CR or CR,LF found
        }
    }
    if (*s == 0)                        // end of string ?
    {
        return 0;                       // no next line available
    }
    return s;                           // next line available
}

// Skip any characters in an input string stream up to '='.
void SidTuneTools::skipToEqu( const char *s, int size, int &pos )
{
    char c;
    do
    {
        if (pos >= size) {
            break;
        }
        c = s[pos++];
    }
    while ( c != '=' );
}

void SidTuneTools::copyStringValueToEOL(const char* pSourceStr, char* pDestStr, int DestMaxLen )
{
    // Start at first character behind '='.
    while ( *pSourceStr != '=' )
    {
        pSourceStr++;
    }
    pSourceStr++;  // Skip '='.
    while (( DestMaxLen > 0 ) && ( *pSourceStr != 0 )
           && ( *pSourceStr != '\n' ) && ( *pSourceStr != '\r' ))
    {
        *pDestStr++ = *pSourceStr++;
        DestMaxLen--;
    }
    *pDestStr++ = 0;
}