Subversion Repositories filter_foundry

Rev

Rev 495 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     This file is part of a common library
  3.     Copyright (C) 2002-6 Toby Thain, toby@telegraphics.com.au
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include <string.h>
  21. #include <stdlib.h>
  22.  
  23. #ifdef WIN_ENV
  24. #include <windows.h>
  25. #endif
  26.  
  27. #include "str.h"
  28. #include "sprintf_tiny.h"
  29.  
  30.  
  31. void strcpy_advance(TCHAR** str, TCHAR* append) {
  32.         xstrcpy(*str, append);
  33.         *str += xstrlen(append);
  34. }
  35.  
  36. void strcpy_advance_a(TCHAR** str, char* append) {
  37. #ifdef UNICODE
  38.         mbstowcs(*str, append, 1000);
  39.         *str += strlen(append);
  40. #else
  41.         strcpy_advance(str, append);
  42. #endif
  43. }
  44.  
  45. #ifdef UNICODE
  46. size_t xstrlen(wchar_t* s) {
  47.         return lstrlen(s);
  48. }
  49. wchar_t* xstrcpy(wchar_t* dst, wchar_t* src) {
  50.         return lstrcpy(dst, src);
  51. }
  52. wchar_t* xstrcat(wchar_t* dst, const wchar_t* src) {
  53.         return lstrcat(dst, src);
  54. }
  55. wchar_t* xstrrchr(wchar_t* const _Str, const int _Ch) {
  56.         return wcsrchr(_Str, _Ch);
  57. }
  58. int xstrcasecmp(const wchar_t* a, const wchar_t* b) {
  59.         return _wcsicmp(a, b);
  60. }
  61. int xstrcmp(const wchar_t* a, const wchar_t* b) {
  62.         return lstrcmpW(a, b);
  63. }
  64. #else
  65. size_t xstrlen(char* s) {
  66.         return strlen(s);
  67. }
  68. char* xstrcpy(char* dst, char* src) {
  69.         return strcpy(dst, src);
  70. }
  71. char* xstrcat(char* dst, const char* src) {
  72.         return strcat(dst, src);
  73. }
  74. char* xstrrchr(char* const _Str, const int _Ch) {
  75.         return strrchr(_Str, _Ch);
  76. }
  77. int xstrcasecmp(const char* a, const char* b) {
  78.         //return strcasecmp(a, b);
  79.         return _stricmp(a, b);
  80. }
  81. int xstrcmp(const char* a, const char* b) {
  82.         return strcmp(a, b);
  83. }
  84. #endif
  85.  
  86. // convert C (null-terminated) to Pascal (length byte) string
  87. // no bounds checking
  88. unsigned char *myc2pstr(char *s){
  89.         size_t n = strlen(s);
  90.         memmove(s+1,s,n);
  91.         *s = (unsigned char)n;
  92.         return (unsigned char*)s;
  93. }
  94.  
  95. // convert Pascal string to C string
  96. // no bounds checking
  97. char *myp2cstr(unsigned char *s){
  98.         int n = *s;
  99.         memmove(s,s+1,n);
  100.         s[n] = 0;
  101.         return (char*)s;
  102. }
  103.  
  104. // copy Pascal string to C string
  105. // no bounds checking
  106. char *myp2cstrcpy(char *dst,const unsigned char *src){
  107.         memcpy(dst,src+1,src[0]);
  108.         dst[src[0]] = 0;
  109.         return dst;
  110. }
  111.  
  112. // copy C string to Pascal string
  113. unsigned char *myc2pstrcpy(unsigned char *dst,const char *src){
  114.         size_t n = strlen(src);
  115.         *dst = n <= 255 ? (unsigned char)n : 255;
  116.         memcpy(dst+1,src,*dst);
  117.         return dst;
  118. }
  119.  
  120. // copy null-terminated string;
  121. // returns pointer after last character copied
  122. char *cat(char *d,char *s){
  123.         while( (*d = *s++) )
  124.                 d++;
  125.         return d;
  126. }
  127.  
  128. /*
  129. void *my_memset(void *dst, int val, size_t len){
  130.         char *p;
  131.         for(p=(char*)dst;len--;)
  132.                 *p++ = val;
  133.         return dst;
  134. }
  135. */
  136.  
  137. // my_strdup() is like _strdup(), with the difference that it accepts "char*" instead of "const char*" as argument
  138. char *my_strdup(char *s){
  139.         size_t n = strlen(s);
  140.         char *p = (char*)malloc(n+1);
  141.         if(p)
  142.                 memcpy(p,s,n+1);
  143.         return p;
  144. }
  145.  
  146. // DM 03.12.2021 removed, because it is not used in Filter Foundry
  147. /*
  148. unsigned char *PLcstrcpy(unsigned char *s1,const char *s2){
  149.         size_t n = strlen(s2);
  150.         if(n>255)
  151.                 n = 255;
  152.         memcpy(s1+1,s2,n);
  153.         *s1 = (unsigned char)n;
  154.         return s1;
  155. }
  156. */
  157.  
  158. // DM 03.12.2021 removed, because it is not used in Filter Foundry
  159. /*
  160. unsigned char *PLcstrcat(unsigned char * str1,const char * s2){
  161.         size_t n = strlen(s2);
  162.         if(str1[0]+n > 255)
  163.                 n = 255 - str1[0];
  164.         memcpy(str1+1+str1[0],s2,n);
  165.         str1[0] += (unsigned char)n;
  166.         return str1;
  167. }
  168. */
  169.  
  170. void strcpy_win_replace_ampersand(char* dst, char* src) {
  171.         size_t i;
  172.         for (i = 0; i < strlen(src); i++) {
  173. #ifdef WIN_ENV
  174.                 // & needs to be replaced to && in:
  175.                 // - Labels (SETCTLTEXT)
  176.                 // - Menu items (i.e. PIPL)
  177.                 // It is not required in:
  178.                 // - Filedialog FileName
  179.                 // - MessageBox title or content
  180.                 // - Window titles
  181.                 // - Input boxes, e.g. import+export of an existing filter
  182.                 if (src[i] == '&') {
  183.                         *dst++ = src[i];
  184.                 }
  185. #endif
  186.                 * dst++ = src[i];
  187.         }
  188.         *dst++ = '\0';
  189. }