Annotation of hatari/src/str.c, revision 1.1.1.5

1.1       root        1: /*
                      2:   Hatari - str.c
                      3: 
                      4:   This file is distributed under the GNU Public License, version 2 or at
                      5:   your option any later version. Read the file gpl.txt for details.
                      6: 
                      7:   String functions.
                      8: */
1.1.1.3   root        9: const char Str_fileid[] = "Hatari str.c : " __DATE__ " " __TIME__;
1.1       root       10: 
1.1.1.3   root       11: #include <stdio.h>
1.1       root       12: #include <ctype.h>
                     13: #include <stdbool.h>
1.1.1.3   root       14: #include <SDL_types.h>
                     15: #include "configuration.h"
1.1       root       16: #include "str.h"
                     17: 
                     18: 
                     19: /**
                     20:  * Remove whitespace from beginning and end of a string.
                     21:  * Returns the trimmed string (string content is moved
                     22:  * so that it still starts from the same address)
                     23:  */
                     24: char *Str_Trim(char *buffer)
                     25: {
                     26:        int i, linelen;
                     27: 
                     28:        if (buffer == NULL)
                     29:                return NULL;
                     30: 
                     31:        linelen = strlen(buffer);
                     32: 
                     33:        for (i = 0; i < linelen; i++)
                     34:        {
                     35:                if (!isspace(buffer[i]))
                     36:                        break;
                     37:        }
                     38: 
                     39:        if (i > 0 && i < linelen)
                     40:        {
                     41:                linelen -= i;
                     42:                memmove(buffer, buffer + i, linelen);
                     43:        }
                     44: 
                     45:        for (i = linelen; i > 0; i--)
                     46:        {
                     47:                if (!isspace(buffer[i-1]))
                     48:                        break;
                     49:        }
                     50: 
                     51:        buffer[i] = '\0';
                     52: 
                     53:        return buffer;
                     54: }
                     55: 
                     56: 
                     57: /**
                     58:  * Convert a string to uppercase in place.
                     59:  */
                     60: char *Str_ToUpper(char *pString)
                     61: {
                     62:        char *str = pString;
                     63:        while (*str)
                     64:        {
                     65:                *str = toupper(*str);
                     66:                str++;
                     67:        }
                     68:        return pString;
                     69: }
                     70: 
                     71: 
                     72: /**
1.1.1.4   root       73:  * Convert string to lowercase in place.
1.1       root       74:  */
                     75: char *Str_ToLower(char *pString)
                     76: {
                     77:        char *str = pString;
                     78:        while (*str)
                     79:        {
                     80:                *str = tolower(*str);
                     81:                str++;
                     82:        }
                     83:        return pString;
                     84: }
                     85: 
                     86: 
                     87: /**
1.1.1.4   root       88:  * truncate string at first unprintable char (e.g. newline).
1.1       root       89:  */
                     90: char *Str_Trunc(char *pString)
                     91: {
                     92:        int i = 0;
                     93:        char *str = pString;
                     94:        while (str[i] != '\0')
                     95:        {
                     96:                if (!isprint((unsigned)str[i]))
                     97:                {
                     98:                        str[i] = '\0';
                     99:                        break;
                    100:                }
                    101:                i++;
                    102:        }
                    103:        return pString;
                    104: }
                    105: 
                    106: 
                    107: /**
                    108:  * check if string is valid hex number.
                    109:  */
                    110: bool Str_IsHex(const char *str)
                    111: {
                    112:        int i = 0;
                    113:        while (str[i] != '\0' && str[i] != ' ')
                    114:        {
                    115:                if (!isxdigit((unsigned)str[i]))
                    116:                        return false;
                    117:                i++;
                    118:        }
                    119:        return true;
                    120: }
1.1.1.5 ! root      121: 
        !           122: 
        !           123: /**
        !           124:  * Convert potentially too long host filenames to 8.3 TOS filenames
        !           125:  * by truncating extension and part before it, replacing invalid
        !           126:  * GEMDOS file name characters with INVALID_CHAR + upcasing the result.
        !           127:  * 
        !           128:  * Matching them from the host file system should first try exact
        !           129:  * case-insensitive match, and then with a pattern that takes into
        !           130:  * account the conversion done in here.
        !           131:  */
        !           132: void Str_Filename2TOSname(const char *source, char *dst)
        !           133: {
        !           134:        char *dot, *tmp, *src;
        !           135:        int len;
        !           136: 
        !           137:        src = strdup(source); /* dup so that it can be modified */
        !           138:        len = strlen(src);
        !           139: 
        !           140:        /* does filename have an extension? */
        !           141:        dot = strrchr(src, '.');
        !           142:        if (dot)
        !           143:        {
        !           144:                /* limit extension to 3 chars */
        !           145:                if (src + len - dot > 3)
        !           146:                        dot[4] = '\0';
        !           147: 
        !           148:                /* if there are extra dots, convert them */
        !           149:                for (tmp = src; tmp < dot; tmp++)
        !           150:                        if (*tmp == '.')
        !           151:                                *tmp = INVALID_CHAR;
        !           152:        }
        !           153: 
        !           154:        /* does name now fit to 8 (+3) chars? */
        !           155:        if (len <= 8 || (dot && len <= 12))
        !           156:                strcpy(dst, src);
        !           157:        else
        !           158:        {
        !           159:                /* name (still) too long, cut part before extension */
        !           160:                strncpy(dst, src, 8);
        !           161:                if (dot)
        !           162:                        strcpy(dst+8, dot);
        !           163:                else
        !           164:                        dst[8] = '\0';
        !           165:        }
        !           166:        free(src);
        !           167: 
        !           168:        /* replace other invalid chars than '.' in filename */
        !           169:        for (tmp = dst; *tmp; tmp++)
        !           170:        {
        !           171:                if (*tmp < 33 || *tmp > 126)
        !           172:                        *tmp = INVALID_CHAR;
        !           173:                else
        !           174:                {
        !           175:                        switch (*tmp)
        !           176:                        {
        !           177:                                case '*':
        !           178:                                case '/':
        !           179:                                case ':':
        !           180:                                case '?':
        !           181:                                case '\\':
        !           182:                                case '{':
        !           183:                                case '}':
        !           184:                                        *tmp = INVALID_CHAR;
        !           185:                        }
        !           186:                }
        !           187:        }
        !           188:        Str_ToUpper(dst);
        !           189: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.