1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*******************************************************************************
3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Copyright (C) 1997-2006, International Business Machines
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Corporation and others.  All Rights Reserved.
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*******************************************************************************
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   file name:  ures_cnv.c
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   encoding:   US-ASCII
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   tab size:   8 (not used)
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   indentation:4
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   created on: 2004aug25
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   created by: Markus W. Scherer
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Character conversion functions moved here from uresbund.c
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h"
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/putil.h"
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/ustring.h"
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/ucnv.h"
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/ures.h"
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "uinvchar.h"
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "ustr_cnv.h"
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI UResourceBundle * U_EXPORT2
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruures_openU(const UChar *myPath,
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru           const char *localeID,
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru           UErrorCode *status)
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char pathBuffer[1024];
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t length;
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *path = pathBuffer;
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(status==NULL || U_FAILURE(*status)) {
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return NULL;
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(myPath==NULL) {
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        path = NULL;
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    else {
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        length=u_strlen(myPath);
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if(length>=sizeof(pathBuffer)) {
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            *status=U_ILLEGAL_ARGUMENT_ERROR;
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return NULL;
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        } else if(uprv_isInvariantUString(myPath, length)) {
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            /*
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru             * the invariant converter is sufficient for package and tree names
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru             * and is more efficient
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru             */
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            u_UCharsToChars(myPath, path, length+1); /* length+1 to include the NUL */
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        } else {
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if !UCONFIG_NO_CONVERSION
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            /* use the default converter to support variant-character paths */
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            UConverter *cnv=u_getDefaultConverter(status);
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            length=ucnv_fromUChars(cnv, path, (int32_t)sizeof(pathBuffer), myPath, length, status);
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            u_releaseDefaultConverter(cnv);
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(U_FAILURE(*status)) {
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                return NULL;
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(length>=sizeof(pathBuffer)) {
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                /* not NUL-terminated - path too long */
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                *status=U_ILLEGAL_ARGUMENT_ERROR;
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                return NULL;
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            /* the default converter is not available */
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            *status=U_UNSUPPORTED_ERROR;
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return NULL;
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return ures_open(path, localeID, status);
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
77