bl/path.c
author ali <ali@juiblex.co.uk>
Mon Feb 20 10:10:48 2012 +0000 (2012-02-20)
changeset 37 037942e1bc4f
permissions -rw-r--r--
Add end of word scannos to testcase
ali@9
     1
#ifdef WIN32
ali@9
     2
#include <windows.h>
ali@9
     3
#endif
ali@9
     4
#include <stdlib.h>
ali@9
     5
#include <glib.h>
ali@9
     6
#include <bl/bl.h>
ali@9
     7
ali@9
     8
/*
ali@9
     9
 * Return an absolute path to <path>.
ali@9
    10
 * Note that this function makes no attempt to return a unique path, or
ali@9
    11
 * to remove "." or ".." entries. It simply returns a path which will
ali@9
    12
 * be unaffected by subsequent calls to chdir().
ali@9
    13
 */
ali@9
    14
char *path_to_absolute(const char *path)
ali@9
    15
{
ali@9
    16
#ifdef WIN32
ali@9
    17
    long len;
ali@9
    18
    gunichar2 *path2;
ali@9
    19
    gunichar2 *abs2;
ali@9
    20
    char *abs;
ali@9
    21
    path2=g_utf8_to_utf16(path,-1,NULL,NULL,NULL);
ali@9
    22
    if (!path2)
ali@9
    23
	return NULL;
ali@9
    24
    len=GetFullPathNameW(path2,0,NULL,NULL);	/* len includes nul */
ali@9
    25
    if (!len)
ali@9
    26
    {
ali@9
    27
	g_free(path2);
ali@9
    28
	return NULL;
ali@9
    29
    }
ali@9
    30
    abs2=g_new(gunichar2,len);
ali@9
    31
    len=GetFullPathNameW(path2,len,abs2,NULL);	/* len excludes nul */
ali@9
    32
    g_free(path2);
ali@9
    33
    if (!len)
ali@9
    34
    {
ali@9
    35
	g_free(abs2);
ali@9
    36
	return NULL;
ali@9
    37
    }
ali@9
    38
    abs=g_utf16_to_utf8(abs2,len,NULL,NULL,NULL);
ali@9
    39
    g_free(abs2);
ali@9
    40
    return abs;
ali@9
    41
#else
ali@9
    42
    char *s,*abs;
ali@9
    43
    if (*path=='/')
ali@9
    44
	abs=g_strdup(path);
ali@9
    45
    else
ali@9
    46
    {
ali@9
    47
	s=g_get_current_dir();
ali@9
    48
	abs=g_build_filename(s,path,NULL);
ali@9
    49
	g_free(s);
ali@9
    50
    }
ali@9
    51
    return abs;
ali@9
    52
#endif
ali@9
    53
}