2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/timeb.h>
29 #include "SC_Win32Utils.h"
31 void win32_ReplaceCharInString(char* string
, int len
, char src
, char dst
)
33 for( int i
= 0; i
< len
; i
++)
38 void win32_ExtractContainingFolder(char* folder
,const char* pattern
,int maxChars
)
40 strcpy(folder
,pattern
);
41 bool backSlashFound
= false;
42 int pathLen
= strlen(pattern
);
43 for( int i
= pathLen
-2; i
>= 0; --i
) {
44 if( pattern
[i
] == '\\') {
46 backSlashFound
= true;
55 void win32_gettimeofday(timeval
* tv
, void*)
57 long unsigned secBetween1601and1970
= 11644473600ULL;
59 GetSystemTimeAsFileTime(&fileTime
);
60 tv
->tv_sec
= (* (unsigned __int64
*) &fileTime
/ (unsigned __int64
)10000000) - secBetween1601and1970
;
61 tv
->tv_usec
= (* (unsigned __int64
*) &fileTime
% (unsigned __int64
)10000000)/(unsigned __int64
)10;
65 void win32_GetHomeFolder(char* homeFolder
, int bufLen
)
67 char homeFolder_
[MAX_PATH
];
69 if (!(h
= ::getenv("home")))
73 strcpy(homeFolder_
,h
);
75 // for Windows NT HOME might be defined as either $(HOMESHARE)/$(HOMEPATH)
76 // or $(HOMEDRIVE)/$(HOMEPATH)
77 h
= ::getenv("HOMESHARE");
79 h
= ::getenv("HOMEDRIVE");
81 strcpy(homeFolder_
,h
);
82 h
= ::getenv("HOMEPATH");
84 strcat(homeFolder_
,h
);
87 size_t len
= strlen(homeFolder_
);
89 fprintf(stderr
, "the buffer given to win32_GetHomeFolder(...) is too small\n");
90 strncpy(homeFolder
,homeFolder_
,len
);
94 char* win32_basename(char* path
)
96 int pathLen
= strlen(path
);
97 int lastPathSepFoundPos
= -1;
99 while (path
[pathLen
-1] == '\\' || path
[pathLen
-1] == '/') {
100 path
[pathLen
-1]=0; pathLen
--;
102 while(path
[pos
] != 0) {
103 if (path
[pos
] == '\\' || path
[pos
] == '/') {
104 lastPathSepFoundPos
= pos
;
108 if (lastPathSepFoundPos
== -1)
111 return path
+ lastPathSepFoundPos
+ 1;
114 char* win32_dirname(char* path
)
116 int pathLen
= strlen(path
);
117 int lastPathSepFoundPos
= -1;
119 while (path
[pathLen
-1] == '\\' || path
[pathLen
-1] == '/') {
120 path
[pathLen
-1]=0; pathLen
--;
122 while(path
[pos
] != 0) {
123 if (path
[pos
] == '\\' || path
[pos
] == '/') {
124 lastPathSepFoundPos
= pos
;
128 if (lastPathSepFoundPos
!= -1)
129 path
[lastPathSepFoundPos
]=0;
134 int win32_nanosleep (const struct timespec
*requested_time
,
135 struct timespec
*remaining
)
137 DWORD milliseconds
= requested_time
->tv_sec
* 1000
138 + (requested_time
->tv_nsec
) / 1000000;
139 if (requested_time
->tv_nsec
< 0 || requested_time
->tv_nsec
> 1000000) {
143 Sleep (milliseconds
);
144 remaining
->tv_sec
= 0;
145 remaining
->tv_nsec
= 0;
149 /* Based on code from PostgreSQL (pgsql/src/port/pipe.c)
150 * This is a replacement version of pipe for Win32 which allows
151 * returned handles to be used in select(). Note that read/write calls
152 * must be replaced with recv/send.
154 int win32_pipe(int handles
[2])
157 struct sockaddr_in serv_addr
;
158 int len
= sizeof(serv_addr
);
159 handles
[0] = handles
[1] = INVALID_SOCKET
;
161 if ((s
= socket(AF_INET
, SOCK_STREAM
, 0)) == INVALID_SOCKET
)
163 printf("win32_pipe failed to create socket: %ui", WSAGetLastError());
166 memset((void *) &serv_addr
, 0, sizeof(serv_addr
));
167 serv_addr
.sin_family
= AF_INET
;
168 serv_addr
.sin_port
= htons(0);
169 serv_addr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
170 if (bind(s
, (SOCKADDR
*) & serv_addr
, len
) == SOCKET_ERROR
)
172 printf("win32_pipe failed to bind: %ui", WSAGetLastError());
175 if (listen(s
, 1) == SOCKET_ERROR
)
177 printf("win32_pipe failed to listen to socket: %ui", WSAGetLastError());
181 if (getsockname(s
, (SOCKADDR
*) & serv_addr
, &len
) == SOCKET_ERROR
)
183 printf("win32_pipe failed to getsockname: %ui", WSAGetLastError());
187 if ((handles
[1] = socket(PF_INET
, SOCK_STREAM
, 0)) == INVALID_SOCKET
)
189 printf("win32_pipe failed to create socket 2: %ui", WSAGetLastError());
193 if (connect(handles
[1], (SOCKADDR
*) & serv_addr
, len
) == SOCKET_ERROR
)
195 printf("win32_pipe failed to connect to socket: %ui", WSAGetLastError());
199 if ((handles
[0] = accept(s
, (SOCKADDR
*) & serv_addr
, &len
)) == INVALID_SOCKET
)
201 printf("win32_pipe failed to accept socket: %ui", WSAGetLastError());
202 closesocket(handles
[1]);
203 handles
[1] = INVALID_SOCKET
;
211 int win32_piperead(int s
, char *buf
, int len
)
213 int ret
= recv(s
, buf
, len
, 0);
215 if (ret
< 0 && WSAGetLastError() == WSAECONNRESET
)
216 /* EOF on the pipe! (win32 socket based implementation) */
221 int win32_pipewrite(int s
, char *buf
, int len
)
223 int ret
= send(s
, buf
, len
, 0);
225 if (ret
< 0 && WSAGetLastError() == WSAECONNRESET
)
226 /* EOF on the pipe! (win32 socket based implementation) */