Author: mturk Date: Wed May 4 08:06:27 2011 New Revision: 1099360 URL: http://svn.apache.org/viewvc?rev=1099360&view=rev Log: Add windows path helper
Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/path.c (with props) Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=1099360&r1=1099359&r2=1099360&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original) +++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Wed May 4 08:06:27 2011 @@ -81,6 +81,7 @@ WIN32_SOURCES=\ $(TOPDIR)\os\win32\execmem.c \ $(TOPDIR)\os\win32\init.c \ $(TOPDIR)\os\win32\os.c \ + $(TOPDIR)\os\win32\path.c \ $(TOPDIR)\os\win32\platform.c \ $(TOPDIR)\os\win32\posix.c \ $(TOPDIR)\os\win32\procmutex.c \ Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h?rev=1099360&r1=1099359&r2=1099360&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h Wed May 4 08:06:27 2011 @@ -37,6 +37,20 @@ #endif #define J2S(V) _s##V +#if defined(WINDOWS) +#define WITH_PATH(V) \ + do { \ + wchar_t _b##V[ACR_MBUFF_SIZ]; \ + wchar_t *_s##V = AcrGetJavaStringW(env, V, _b##V); \ + if (_s##V == 0 && V != 0) goto _e##V; +#else +#define WITH_PATH(V) \ + do { \ + char _b##V[ACR_PBUFF_SIZ]; \ + char *_s##V = AcrGetJavaStringA(env, V, _b##V); \ + if (_s##V == 0 && V != 0) goto _e##V; +#endif + #define WITH_WSTR(V) \ do { \ wchar_t _b##V[ACR_MBUFF_SIZ]; \ @@ -206,6 +220,11 @@ AcrGetJavaStringArrayW(JNI_STDENV, jobje jcharArray AcrNewJavaMszArrayW(JNI_STDENV, const wchar_t *s); +#if defined(WINDOWS) +wchar_t * +AcrGetNativePathW(JNI_STDENV, jstring str, wchar_t *b); +#endif + #ifdef __cplusplus } #endif Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/path.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/path.c?rev=1099360&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/path.c (added) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/path.c Wed May 4 08:06:27 2011 @@ -0,0 +1,169 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "acr/string.h" +#include "acr/jniapi.h" +#include "acr/port.h" +#include "acr/misc.h" +#include "arch_opts.h" + +#include <direct.h> /* For _getdrive() */ +/** + * Win32 file path functions + * + */ + +#define NON_UNC_PATH_LENGTH 248 +/* + * 1. Remove \\?\ prefix + * 2. Replace UNC\ with \\ + */ +ACR_INLINE(wchar_t *) NO2UNC(wchar_t *fname) +{ + if (fname[0] == L'/' && fname[1] == L'/' && + fname[2] == L'?' && fname[3] == L'/') { + fname += 4; + if (fname[0] == L'U' && fname[1] == L'N' && + fname[2] == L'C' && fname[3] == L'/') { + fname += 2; + *fname = L'/'; + } + else if (fname[0] == L'U' && fname[1] == L'N' && + fname[2] == L'/' && fname[3] == L'/') { + fname += 2; + /* Already modified in-place. + */ + fname += 2; + } + } + return fname; +} + +ACR_INLINE(void) FS2BS(wchar_t *path) +{ + while (*path) { + if (*path == L'/') + *path = L'\\'; + path++; + } +} + +ACR_INLINE(void) BS2FS(wchar_t *path) +{ + while (*path) { + if (*path == L'\\') + *path = L'/'; + path++; + } +} + +#define IS_PATH_SEP(C) ((C) == L'/' || (C) == L'\0') +#define IS_DRIVE_CHAR(C) (((C) >= L'A' && (C) <= L'Z') || \ + ((C) >= L'a' && (C) <= L'z')) + +wchar_t * +AcrGetNativePathW(JNI_STDENV, jstring str, wchar_t *b) +{ + size_t srclen; + size_t retlen; + const jchar *sr; + const wchar_t *srcstr; + wchar_t *rv = NULL; + wchar_t *retstr; + + if (IS_JOBJECT_NULL(str)) { + return 0; + } + retlen = srclen = (size_t)(*env)->GetStringLength(env, str); + if (b && srclen < (ACR_MBUFF_LEN - 8)) + rv = b; + else { + rv = ACR_MALLOC(wchar_t, srclen + 9); + if (!rv) { + /* Exception has already been throw from ACR_Malloc + */ + return NULL; + } + } + srcstr = sr = (*env)->GetStringCritical(env, str, 0); + if (srcstr == 0) { + if (rv != b) + AcrFree(rv); + return 0; + } + retstr = rv; + if (IS_DRIVE_CHAR(srcstr[0]) && srcstr[1] == L':' && + (srcstr[2] == L'/' || srcstr[2] == L'\\')) { + if (srclen > NON_UNC_PATH_LENGTH) { + wcscpy(retstr, L"\\\\?\\"); + retstr += 4; + retlen += 4; + } + } + else if ((srcstr[0] == L'/' || srcstr[0] == L'\\') + && (srcstr[1] == L'/' || srcstr[1] == L'\\') + && (srcstr[2] != L'?')) { + if (srcstr[2] == L'.' && (srcstr[3] == L'/' || + srcstr[3] == L'\\')) { + /* We have \\.\ sequence that can apear only + * if we have something like \\.\pipe\ + */ + wcscpy(retstr, L"\\\\.\\"); + retstr += 4; + srcstr += 4; + srclen -= 4; + } + else { + /* Skip the slashes and ? */ + srcstr += 2; + srclen -= 2; + wcscpy (retstr, L"\\\\?\\UNC\\"); + retlen += 6; + retstr += 8; + } + } + else if (srcstr[0] == '/' || srcstr[0] == '\\') { + int cd; + int ps = 0; + /* Addition to APR. Allow \FilePath + * and construct \\?\CurrenttDrive:\FilePath + * We use _getdrive CRT function + */ + if (srclen > NON_UNC_PATH_LENGTH) { + wcscpy(retstr, L"\\\\?\\"); + ps = 4; + } + if ((cd = _getdrive())) + retstr[ps] = L'A' + cd - 1; + else { + /* C:\ should always be there + * If not later open will fail anyhow + */ + retstr[ps] = L'C'; + } + retstr[ps + 1] = L':'; + retlen += (ps + 2); + retstr += (ps + 2); + } + + memcpy(retstr, srcstr, srclen * sizeof(wchar_t)); + (*env)->ReleaseStringCritical(env, str, sr); + rv[retlen] = L'\0'; + FS2BS(retstr); + + return rv; +} + Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/path.c ------------------------------------------------------------------------------ svn:eol-style = native