PageRenderTime 768ms CodeModel.GetById 594ms app.highlight 14ms RepoModel.GetById 157ms app.codeStats 0ms

/src/os/wince/ftk_wince.c

http://ftk.googlecode.com/
C | 200 lines | 158 code | 32 blank | 10 comment | 26 complexity | 5b09e5cd9aa1a9802192d9c2491e5797 MD5 | raw file
  1
  2#include <string.h>
  3#include <assert.h>
  4#include "ftk_wince.h"
  5#include "ftk_log.h"
  6#include <initguid.h>
  7
  8static char g_work_dir[MAX_PATH+1] = {0};
  9static char g_data_dir[MAX_PATH+1] = {0};
 10static char g_testdata_dir[MAX_PATH+1] = {0};
 11
 12char* ftk_get_root_dir(void)
 13{
 14	return g_work_dir;
 15}
 16
 17char* ftk_get_data_dir(void)
 18{
 19	return g_data_dir;
 20}
 21
 22char* ftk_get_testdata_dir(void)
 23{
 24	return g_testdata_dir;
 25}
 26
 27int ftk_platform_init(int argc, char** argv)
 28{
 29	int Ret = 0;
 30	char* p = NULL;
 31	HRESULT hr;
 32	WSADATA wsaData = {0};
 33
 34	hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
 35	if(FAILED(hr))
 36	{
 37		ftk_loge("CoInitializeEx() failed\n");
 38		return 1;
 39	}
 40
 41	if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
 42	{
 43		assert(!"WSAStartup failed with error %d\n");
 44		CoUninitialize();
 45		return 0;
 46	}
 47	
 48	if(_getcwd(g_work_dir, MAX_PATH) != NULL)
 49	{
 50		p = strstr(g_work_dir, "\\src");
 51		if(p != NULL)
 52		{
 53			*p = '\0';
 54			ftk_snprintf(g_data_dir, MAX_PATH, "%s\\data", g_work_dir);
 55			ftk_snprintf(g_testdata_dir, MAX_PATH, "%s\\testdata", g_work_dir);
 56		}
 57	}
 58
 59	return 0;
 60}
 61
 62void ftk_platform_deinit(void)
 63{
 64	if (WSACleanup() == SOCKET_ERROR)
 65	{
 66		ftk_logd("WSACleanup failed with error %d\n", WSAGetLastError());
 67	}
 68
 69	CoUninitialize();
 70}
 71
 72char* ftk_strncpy(char *dest, const char *src, size_t n)
 73{
 74	return strncpy(dest, src, n);
 75}
 76
 77int   ftk_snprintf(char *str, size_t size, const char *format, ...)
 78{
 79	int ret = 0;
 80	va_list args;
 81	va_start(args, format);
 82	ret = _vsnprintf(str, size-1, format, args);
 83	str[size-1] = '\0';
 84
 85	return ret;
 86}
 87
 88int   ftk_vsnprintf(char *str, size_t size, const char *format, va_list ap)
 89{
 90	return _vsnprintf(str, size-1, format, ap);
 91}
 92
 93size_t ftk_get_relative_time(void)
 94{
 95	FILETIME        ft;
 96	SYSTEMTIME      st;
 97	LARGE_INTEGER   li;
 98	__int64         t;
 99
100	GetSystemTime(&st);
101	SystemTimeToFileTime(&st, &ft);
102	li.LowPart  = ft.dwLowDateTime;
103	li.HighPart = ft.dwHighDateTime;
104	t  = li.QuadPart;       /* In 100-nanosecond intervals */
105	t /= 10;                /* In microseconds */
106
107	return (size_t) t/1000;
108}
109
110char *_getcwd(char *buf, size_t size)
111{
112	size_t len;
113	len = sizeof("\\Program Files\\ftk") - 1;
114	ftk_strncpy(buf, "\\Program Files\\ftk", len);
115	buf[len] = '\0';
116	return buf;
117}
118
119/*http://cantrip.org/socketpair.c*/
120/* socketpair.c
121 * Copyright 2007 by Nathan C. Myers <ncm@cantrip.org>; all rights reserved.
122 * This code is Free Software.  It may be copied freely, in original or 
123 * modified form, subject only to the restrictions that (1) the author is
124 * relieved from all responsibilities for any use for any purpose, and (2)
125 * this copyright notice must be retained, unchanged, in its entirety.  If
126 * for any reason the author might be held responsible for any consequences
127 * of copying or use, license is withheld.  
128 */
129int wince_socketpair(SOCKET socks[2])
130{
131    int e;
132    DWORD flags = 0;
133    SOCKET listener;
134    struct sockaddr_in addr;
135    int addrlen = sizeof(addr);
136
137    if (socks == 0) {
138      WSASetLastError(WSAEINVAL);
139      return SOCKET_ERROR;
140    }
141
142    socks[0] = socks[1] = INVALID_SOCKET;
143    if ((listener = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) 
144        return SOCKET_ERROR;
145
146    memset(&addr, 0, sizeof(addr));
147    addr.sin_family = AF_INET;
148    addr.sin_addr.s_addr = htonl(0x7f000001);
149    addr.sin_port = 0;
150
151    e = bind(listener, (const struct sockaddr*) &addr, sizeof(addr));
152    if (e == SOCKET_ERROR) {
153        e = WSAGetLastError();
154    	closesocket(listener);
155        WSASetLastError(e);
156        return SOCKET_ERROR;
157    }
158
159    e = getsockname(listener, (struct sockaddr*) &addr, &addrlen);
160    if (e == SOCKET_ERROR) {
161        e = WSAGetLastError();
162    	closesocket(listener);
163        WSASetLastError(e);
164        return SOCKET_ERROR;
165    }
166
167    do {
168        if (listen(listener, 1) == SOCKET_ERROR)                      break;
169        if ((socks[0] = socket(AF_INET, SOCK_STREAM, 0))
170                == INVALID_SOCKET)                                    break;
171        if (connect(socks[0], (const struct sockaddr*) &addr,
172                    sizeof(addr)) == SOCKET_ERROR)                    break;
173        if ((socks[1] = accept(listener, NULL, NULL))
174                == INVALID_SOCKET)                                    break;
175        closesocket(listener);
176        return 0;
177    } while (0);
178    e = WSAGetLastError();
179    closesocket(listener);
180    closesocket(socks[0]);
181    closesocket(socks[1]);
182    WSASetLastError(e);
183
184    return SOCKET_ERROR;
185}
186
187int FTK_MAIN(int argc, char* argv[]);
188
189int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev_inst, LPWSTR cmd_link, int cmd_show)
190{
191    int argc, rv;
192    char *argv[2];
193
194    argv[0] = "ftk";
195    argv[1] = NULL;
196    argc = 1;
197    rv = FTK_MAIN(argc, argv);
198
199    return rv;
200}