qse/ase/utl/main.c

104 lines
1.6 KiB
C
Raw Normal View History

2006-12-17 12:33:31 +00:00
/*
2007-02-03 10:52:36 +00:00
* $Id: main.c,v 1.8 2007-02-03 10:52:36 bacon Exp $
*
* {License}
2006-12-17 12:33:31 +00:00
*/
#include <ase/types.h>
#include <ase/macros.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
2007-01-26 16:10:32 +00:00
#if defined(_WIN32)
#include <tchar.h>
#define ase_main _tmain
2007-01-28 11:28:27 +00:00
#elif defined(ASE_CHAR_IS_MCHAR)
2007-01-29 02:47:20 +00:00
#ifdef __cplusplus
extern "C" { int ase_main (...); }
#else
extern int ase_main ();
#endif
int main (int argc, char* argv[], char** envp)
{
setlocale (LC_ALL, "");
return ase_main (argc, argv, envp);
}
2007-01-28 11:28:27 +00:00
#else /* ASE_CHAR_IS_WCHAR */
2006-12-17 12:33:31 +00:00
2007-01-26 16:08:55 +00:00
#ifdef __cplusplus
extern "C" { int ase_main (...); }
#else
extern int ase_main ();
#endif
2006-12-17 12:33:31 +00:00
int main (int argc, char* argv[]/*, char** envp*/)
{
int i, ret;
ase_char_t** v;
setlocale (LC_ALL, "");
v = (ase_char_t**) malloc (argc * ASE_SIZEOF(ase_char_t*));
if (v == NULL) return -1;
for (i = 0; i < argc; i++) v[i] = NULL;
for (i = 0; i < argc; i++)
{
ase_size_t n, len, rem;
char* p = argv[i];
len = 0; rem = strlen (p);
while (*p != '\0')
{
int x = mblen (p, rem);
if (x == -1)
{
ret = -1;
goto exit_main;
}
if (x == 0) break;
p += x; rem -= x; len++;
}
v[i] = (ase_char_t*) malloc (
(len + 1) * ASE_SIZEOF(ase_char_t));
if (v[i] == NULL)
{
ret = -1;
goto exit_main;
}
n = mbstowcs (v[i], argv[i], len);
if (n == (size_t)-1)
{
/* error */
return -1;
}
if (n == len) v[i][len] = ASE_T('\0');
}
/* TODO: envp... */
2007-01-26 15:50:48 +00:00
ret = ase_main (argc, v, NULL);
2006-12-17 12:33:31 +00:00
exit_main:
for (i = 0; i < argc; i++)
{
if (v[i] != NULL) free (v[i]);
}
free (v);
return ret;
}
#endif