File: ffsox_path3.c

package info (click to toggle)
bs1770gain 0.4.12-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,832 kB
  • sloc: ansic: 9,380; sh: 1,288; makefile: 212
file content (135 lines) | stat: -rwxr-xr-x 3,371 bytes parent folder | download | duplicates (2)
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
/*
 * ffsox_path3.c
 * Copyright (C) 2014 Peter Belkner <pbelkner@users.sf.net>
 *
 * 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.0 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301  USA
 */
#include <ffsox_priv.h>

#if defined (_WIN32) // {
wchar_t *ffsox_path3(const wchar_t *ws1, const char *s2, const char *s3)
{
  wchar_t *path,*pp;
  int size1,size2,size3,size;

  path=NULL;
  size1=NULL==ws1||0==*ws1?0:wcslen(ws1)+1;

  if (NULL==s2||0==*s2)
    size2=0;
  else {
    size2=MultiByteToWideChar(
      CP_UTF8,    // _In_       UINT CodePage,
      0,          // _In_       DWORD dwFlags,
      s2,         // _In_       LPCSTR lpMultiByteStr,
      -1,         // _In_       int cbMultiByte,
      NULL,       // _Out_opt_  LPWSTR lpWideCharStr,
      0           // _In_       int cchWideChar
    );
  }

  if (NULL==s3||0==*s3)
    size3=0;
  else {
    size3=MultiByteToWideChar(
      CP_UTF8,    // _In_       UINT CodePage,
      0,          // _In_       DWORD dwFlags,
      s3,         // _In_       LPCSTR lpMultiByteStr,
      -1,         // _In_       int cbMultiByte,
      NULL,       // _Out_opt_  LPWSTR lpWideCharStr,
      0           // _In_       int cchWideChar
    );
  }

  if (0==(size=size1+size2+size3))
    goto empty;

  if (NULL==(path=MALLOC(size*sizeof *path)))
    goto malloc;

  pp=path;

  if (0<size1) {
    wcscpy(pp,ws1);
    pp+=size1;
    pp[-1]=L'\\';
  }

  if (0<size2) {
    pp+=MultiByteToWideChar(
      CP_UTF8,  // _In_       UINT CodePage,
      0,        // _In_       DWORD dwFlags,
      s2,       // _In_       LPCSTR lpMultiByteStr,
      -1,       // _In_       int cbMultiByte,
      pp,       // _Out_opt_  LPWSTR lpWideCharStr,
      size2     // _In_       int cchWideChar
    );

    pp[-1]=L'\\';
  }

  if (0<size3) {
    pp+=MultiByteToWideChar(
      CP_UTF8,  // _In_       UINT CodePage,
      0,        // _In_       DWORD dwFlags,
      s3,       // _In_       LPCSTR lpMultiByteStr,
      -1,       // _In_       int cbMultiByte,
      pp,       // _Out_opt_  LPWSTR lpWideCharStr,
      size3     // _In_       int cchWideChar
    );
  }
malloc:
empty:
  return path;
}
#else // } {
char *ffsox_path3(const char *s1, const char *s2, const char *s3)
{
  char *path,*pp;
  int size1,size2,size3,size;

  path=NULL;
  size1=NULL==s1||0==*s1?0:strlen(s1)+1;
  size2=NULL==s2||0==*s2?0:strlen(s2)+1;
  size3=NULL==s3||0==*s3?0:strlen(s3)+1;
  
  if (0==(size=size1+size2+size3))
    goto empty;

  if (NULL==(path=MALLOC(size)))
    goto path;

  pp=path;

  if (0<size1) {
    strcpy(pp,s1);
    pp+=size1;
    pp[-1]='/';
  }

  if (0<size2) {
    strcpy(pp,s2);
    pp+=size2;
    pp[-1]='/';
  }

  if (0<size3)
    strcpy(pp,s3);
path:
empty:
  return path;
}
#endif // }