diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
commit | b6c1cf6de79035f58b512f4400db458c8401379a (patch) | |
tree | 68979db37c85b499bc384e4ac337ed1424baab51 /libs/host | |
download | build-b6c1cf6de79035f58b512f4400db458c8401379a.zip build-b6c1cf6de79035f58b512f4400db458c8401379a.tar.gz build-b6c1cf6de79035f58b512f4400db458c8401379a.tar.bz2 |
Initial Contribution
Diffstat (limited to 'libs/host')
-rw-r--r-- | libs/host/Android.mk | 26 | ||||
-rw-r--r-- | libs/host/CopyFile.c | 626 | ||||
-rw-r--r-- | libs/host/Directories.cpp | 42 | ||||
-rw-r--r-- | libs/host/include/host/CopyFile.h | 30 | ||||
-rw-r--r-- | libs/host/include/host/Directories.h | 10 | ||||
-rw-r--r-- | libs/host/include/host/pseudolocalize.h | 9 | ||||
-rw-r--r-- | libs/host/list.java | 35 | ||||
-rw-r--r-- | libs/host/pseudolocalize.cpp | 119 |
8 files changed, 897 insertions, 0 deletions
diff --git a/libs/host/Android.mk b/libs/host/Android.mk new file mode 100644 index 0000000..81f2cc5 --- /dev/null +++ b/libs/host/Android.mk @@ -0,0 +1,26 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + CopyFile.c \ + Directories.cpp \ + pseudolocalize.cpp + +ifeq ($(HOST_OS),cygwin) +LOCAL_CFLAGS += -DWIN32_EXE +endif +ifeq ($(HOST_OS),darwin) +LOCAL_CFLAGS += -DMACOSX_RSRC +endif +ifeq ($(HOST_OS),linux) +endif + +LOCAL_MODULE:= libhost +LOCAL_C_INCLUDES := $(LOCAL_PATH)/include + +# acp uses libhost, so we can't use +# acp to install libhost. +LOCAL_ACP_UNAVAILABLE:= true + +include $(BUILD_HOST_STATIC_LIBRARY) + diff --git a/libs/host/CopyFile.c b/libs/host/CopyFile.c new file mode 100644 index 0000000..a822b41 --- /dev/null +++ b/libs/host/CopyFile.c @@ -0,0 +1,626 @@ +/* + * Copyright 2005 The Android Open Source Project + * + * Android "cp" replacement. + * + * The GNU/Linux "cp" uses O_LARGEFILE in its open() calls, utimes() instead + * of utime(), and getxattr()/setxattr() instead of chmod(). These are + * probably "better", but are non-portable, and not necessary for our + * purposes. + */ +#include <host/CopyFile.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <getopt.h> +#include <dirent.h> +#include <fcntl.h> +#include <utime.h> +#include <limits.h> +#include <errno.h> +#include <assert.h> + +#ifdef HAVE_MS_C_RUNTIME +# define mkdir(path,mode) _mkdir(path) +#endif + +#ifndef HAVE_SYMLINKS +# define lstat stat +# ifndef EACCESS /* seems to be missing from the Mingw headers */ +# define EACCESS 13 +# endif +#endif + +#ifndef O_BINARY +# define O_BINARY 0 +#endif + +/*#define DEBUG_MSGS*/ +#ifdef DEBUG_MSGS +# define DBUG(x) printf x +#else +# define DBUG(x) ((void)0) +#endif + +#define FSSEP '/' /* filename separator char */ + +static int copyFileRecursive(const char* src, const char* dst, bool isCmdLine, unsigned int options); + +/* + * Returns true if the source file is newer than the destination file. + * + * The check is based on the modification date, whole seconds only. This + * also returns true if the file sizes don't match. + */ +static bool isSourceNewer(const struct stat* pSrcStat, const struct stat* pDstStat) +{ + return (pSrcStat->st_mtime > pDstStat->st_mtime) || + (pSrcStat->st_size != pDstStat->st_size); +} + +/* + * Returns true if the source and destination files are actually the + * same thing. We detect this by checking the inode numbers, which seems + * to work on Cygwin. + */ +static bool isSameFile(const struct stat* pSrcStat, const struct stat* pDstStat) +{ +#ifndef HAVE_VALID_STAT_ST_INO + /* with MSVCRT.DLL, stat always sets st_ino to 0, and there is no simple way to */ + /* get the equivalent information with Win32 (Cygwin does some weird stuff in */ + /* its winsup/cygwin/fhandler_disk_file.cc to emulate this, too complex for us) */ + return 0; +#else + return (pSrcStat->st_ino == pDstStat->st_ino); +#endif +} + +static void printCopyMsg(const char* src, const char* dst, unsigned int options) +{ + if ((options & COPY_VERBOSE_MASK) > 0) + printf(" '%s' --> '%s'\n", src, dst); +} + +static void printNotNewerMsg(const char* src, const char* dst, unsigned int options) +{ + if ((options & COPY_VERBOSE_MASK) > 1) + printf(" '%s' is up-to-date\n", dst); +} + +/* + * Copy the contents of one file to another. + * + * The files are assumed to be seeked to the start. + */ +static int copyFileContents(const char* dst, int dstFd, const char* src, int srcFd) +{ + unsigned char buf[8192]; + ssize_t readCount, writeCount; + + /* + * Read a chunk, write it, and repeat. + */ + while (1) { + readCount = read(srcFd, buf, sizeof(buf)); + if (readCount < 0) { + fprintf(stderr, + "acp: failed reading '%s': %s\n", src, strerror(errno)); + return -1; + } + + if (readCount > 0) { + writeCount = write(dstFd, buf, readCount); + if (writeCount < 0) { + fprintf(stderr, + "acp: failed writing '%s': %s\n", dst, strerror(errno)); + return -1; + } + if (writeCount != readCount) { + fprintf(stderr, "acp: partial write to '%s' (%d of %d)\n", + dst, writeCount, readCount); + return -1; + } + } + + if (readCount < (ssize_t) sizeof(buf)) + break; + } + + return 0; +} + +/* + * Set the permissions, owner, and timestamps on the destination file + * equal to those of the source file. + * + * Failures here are "soft"; they don't produce warning messages and don't + * cause the cp command to report a failure. + */ +static int setPermissions(const char* dst, const struct stat* pSrcStat, unsigned int options) +{ + struct utimbuf ut; + + if (options & COPY_TIMESTAMPS) { + /* + * Start with timestamps. The access and mod dates are not affected + * by the next operations. + */ + ut.actime = pSrcStat->st_atime; + ut.modtime = pSrcStat->st_mtime; + if (utime(dst, &ut) != 0) { + DBUG(("--- unable to set timestamps on '%s': %s\n", + dst, strerror(errno))); + } + } + + if (options & COPY_PERMISSIONS) { + /* + * Set the permissions. + */ + if (chmod(dst, pSrcStat->st_mode & ~(S_IFMT)) != 0) { + DBUG(("--- unable to set perms on '%s' to 0%o: %s\n", + dst, pSrcStat->st_mode & ~(S_IFMT), strerror(errno))); + } +#ifndef HAVE_MS_C_RUNTIME + /* + * Set the owner. + */ + if (chown(dst, pSrcStat->st_uid, pSrcStat->st_gid) != 0) { + DBUG(("--- unable to set owner of '%s' to %d/%d: %s\n", + dst, pSrcStat->st_uid, pSrcStat->st_gid, strerror(errno))); + } +#endif + } + + return 0; +} + +/* + * Copy a regular file. If the destination file exists and is not a + * regular file, we fail. However, we use stat() rather than lstat(), + * because it's okay to write through a symlink (the noDereference stuff + * only applies to the source file). + * + * If the file doesn't exist, create it. If it does exist, truncate it. + */ +static int copyRegular(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options) +{ + struct stat dstStat; + int srcFd, dstFd, statResult, copyResult; + + DBUG(("--- copying regular '%s' to '%s'\n", src, dst)); + + statResult = stat(dst, &dstStat); + if (statResult == 0 && !S_ISREG(dstStat.st_mode)) { + fprintf(stderr, + "acp: destination '%s' exists and is not regular file\n", + dst); + return -1; + } else if (statResult != 0 && errno != ENOENT) { + fprintf(stderr, "acp: unable to stat destination '%s'\n", dst); + return -1; + } + + if (statResult == 0) { + if (isSameFile(pSrcStat, &dstStat)) { + fprintf(stderr, "acp: '%s' and '%s' are the same file\n", + src, dst); + return -1; + } + if (options & COPY_UPDATE_ONLY) { + if (!isSourceNewer(pSrcStat, &dstStat)) { + DBUG(("--- source is not newer: '%s'\n", src)); + printNotNewerMsg(src, dst, options); + return 0; + } + } + } + + /* open src */ + srcFd = open(src, O_RDONLY | O_BINARY, 0); + if (srcFd < 0) { + fprintf(stderr, "acp: unable to open '%s': %s\n", src, strerror(errno)); + return -1; + } + + /* open dest with O_CREAT | O_TRUNC */ + DBUG(("--- opening '%s'\n", dst)); + dstFd = open(dst, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644); + + if (dstFd < 0) { + if (errno == ENOENT) { + /* this happens if the target directory doesn't exist */ + fprintf(stderr, + "acp: cannot create '%s': %s\n", dst, strerror(errno)); + (void) close(srcFd); + return -1; + } + + /* if "force" is set, try removing the destination file and retry */ + if (options & COPY_FORCE) { + if (unlink(dst) != 0) { +#ifdef HAVE_MS_C_RUNTIME + /* MSVCRT.DLL unlink will fail with EACCESS if the file is set read-only */ + /* so try to change its mode, and unlink again */ + if (errno == EACCESS) { + if (chmod(dst, S_IWRITE|S_IREAD) == 0 && unlink(dst) == 0) + goto Open_File; + } +#endif + fprintf(stderr, "acp: unable to remove '%s': %s\n", + dst, strerror(errno)); + (void) close(srcFd); + return -1; + } +#ifdef HAVE_MS_C_RUNTIME + Open_File: +#endif + dstFd = open(dst, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644); + } + } + if (dstFd < 0) { + fprintf(stderr, "acp: unable to open '%s': %s\n", + dst, strerror(errno)); + (void) close(srcFd); + return -1; + } + + copyResult = copyFileContents(dst, dstFd, src, srcFd); + + (void) close(srcFd); + (void) close(dstFd); + if (copyResult != 0) + return -1; + +#ifdef MACOSX_RSRC + { + char* srcRsrcName = NULL; + char* dstRsrcName = NULL; + struct stat rsrcStat; + + srcRsrcName = malloc(strlen(src) + 5 + 1); + strcpy(srcRsrcName, src); + strcat(srcRsrcName, "/rsrc"); + + dstRsrcName = malloc(strlen(dst) + 5 + 1); + strcpy(dstRsrcName, dst); + strcat(dstRsrcName, "/rsrc"); + + if (stat(srcRsrcName, &rsrcStat) == 0 && rsrcStat.st_size > 0) { + DBUG(("--- RSRC: %s --> %s\n", srcRsrcName, dstRsrcName)); + + srcFd = open(srcRsrcName, O_RDONLY); + dstFd = open(dstRsrcName, O_TRUNC | O_WRONLY, 0); + copyResult = -1; + if (srcFd >= 0 && dstFd >= 0) { + copyResult = copyFileContents(dstRsrcName, dstFd, + srcRsrcName, srcFd); + (void) close(srcFd); + (void) close(dstFd); + } + + if (copyResult != 0) + return -1; + } + + free(srcRsrcName); + free(dstRsrcName); + } +#endif + + setPermissions(dst, pSrcStat, options); + + printCopyMsg(src, dst, options); + + return 0; +} + + +#ifdef HAVE_SYMLINKS +/* + * Copy a symlink. This only happens if we're in "no derefence" mode, + * in which we copy the links rather than the files that are pointed at. + * + * We always discard the destination file. If it's a symlink already, + * we want to throw it out and replace it. If it's not a symlink, we + * need to trash it so we can create one. + */ +static int copySymlink(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options) +{ + struct stat dstStat; + char linkBuf[PATH_MAX+1]; + int statResult, nameLen; + + assert(options & COPY_NO_DEREFERENCE); + DBUG(("--- copying symlink '%s' to '%s'\n", src, dst)); + + /* NOTE: we use lstat() here */ + statResult = lstat(dst, &dstStat); + if (statResult == 0 && !S_ISREG(dstStat.st_mode) + && !S_ISLNK(dstStat.st_mode) + ) + { + fprintf(stderr, + "acp: destination '%s' exists and is not regular or symlink\n", + dst); + return -1; + } + + if (statResult == 0) { + if (isSameFile(pSrcStat, &dstStat)) { + fprintf(stderr, "acp: '%s' and '%s' are the same file\n", + src, dst); + return -1; + } + if (options & COPY_UPDATE_ONLY) { + if (!isSourceNewer(pSrcStat, &dstStat)) { + DBUG(("--- source is not newer: '%s'\n", src)); + printNotNewerMsg(src, dst, options); + return 0; + } + } + } + + /* extract the symlink contents */ + nameLen = readlink(src, linkBuf, sizeof(linkBuf)-1); + if (nameLen <= 0) { + fprintf(stderr, "acp: unable to read symlink '%s': %s\n", + src, strerror(errno)); + return -1; + } + linkBuf[nameLen] = '\0'; + DBUG(("--- creating symlink file '%s' (--> %s)\n", dst, linkBuf)); + + if (statResult == 0) { + DBUG(("--- removing '%s'\n", dst)); + if (unlink(dst) != 0) { + fprintf(stderr, "acp: unable to remove '%s': %s\n", + dst, strerror(errno)); + return -1; + } + } + + if (symlink(linkBuf, dst) != 0) { + fprintf(stderr, "acp: unable to create symlink '%s' [%s]: %s\n", + dst, linkBuf, strerror(errno)); + return -1; + } + + /* + * There's no way to set the file date or access permissions, but + * it is possible to set the owner. + */ + if (options & COPY_PERMISSIONS) { + if (lchown(dst, pSrcStat->st_uid, pSrcStat->st_gid) != 0) + DBUG(("--- lchown failed: %s\n", strerror(errno))); + } + + printCopyMsg(src, dst, options); + + return 0; +} +#endif /* HAVE_SYMLINKS */ + +/* + * Copy the contents of one directory to another. Both "src" and "dst" + * must be directories. We will create "dst" if it does not exist. + */ +int copyDirectory(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options) +{ + int retVal = 0; + struct stat dstStat; + DIR* dir; + int cc, statResult; + + DBUG(("--- copy dir '%s' to '%s'\n", src, dst)); + + statResult = stat(dst, &dstStat); + if (statResult == 0 && !S_ISDIR(dstStat.st_mode)) { + fprintf(stderr, + "acp: destination '%s' exists and is not a directory\n", dst); + return -1; + } else if (statResult != 0 && errno != ENOENT) { + fprintf(stderr, "acp: unable to stat destination '%s'\n", dst); + return -1; + } + + if (statResult == 0) { + if (isSameFile(pSrcStat, &dstStat)) { + fprintf(stderr, + "acp: cannot copy directory into itself ('%s' and '%s')\n", + src, dst); + return -1; + } + } else { + DBUG(("--- creating dir '%s'\n", dst)); + cc = mkdir(dst, 0755); + if (cc != 0) { + fprintf(stderr, "acp: unable to create directory '%s': %s\n", + dst, strerror(errno)); + return -1; + } + + /* only print on mkdir */ + printCopyMsg(src, dst, options); + } + + /* + * Open the directory, and plow through its contents. + */ + dir = opendir(src); + if (dir == NULL) { + fprintf(stderr, "acp: unable to open directory '%s': %s\n", + src, strerror(errno)); + return -1; + } + + while (1) { + struct dirent* ent; + char* srcFile; + char* dstFile; + int srcLen, dstLen, nameLen; + + ent = readdir(dir); + if (ent == NULL) + break; + + if (strcmp(ent->d_name, ".") == 0 || + strcmp(ent->d_name, "..") == 0) + { + continue; + } + + nameLen = strlen(ent->d_name); + srcLen = strlen(src); + dstLen = strlen(dst); + + srcFile = malloc(srcLen +1 + nameLen +1); + memcpy(srcFile, src, srcLen); + srcFile[srcLen] = FSSEP; + memcpy(srcFile + srcLen+1, ent->d_name, nameLen +1); + + dstFile = malloc(dstLen +1 + nameLen +1); + memcpy(dstFile, dst, dstLen); + dstFile[dstLen] = FSSEP; + memcpy(dstFile + dstLen+1, ent->d_name, nameLen +1); + + if (copyFileRecursive(srcFile, dstFile, false, options) != 0) + retVal = -1; /* note failure and keep going */ + + free(srcFile); + free(dstFile); + } + closedir(dir); + + setPermissions(dst, pSrcStat, options); + + return retVal; +} + +/* + * Do the actual copy. This is called recursively from copyDirectory(). + * + * "dst" should only be a directory if "src" is also a directory. + * + * Returns 0 on success. + */ +static int copyFileRecursive(const char* src, const char* dst, bool isCmdLine, unsigned int options) +{ + char* srcExe = NULL; + char* dstExe = NULL; + char* dstDir = NULL; + struct stat srcStat; + int retVal = 0; + int statResult, statErrno; + + /* + * Stat the source file. If it doesn't exist, fail. + */ + if (options & COPY_NO_DEREFERENCE) + statResult = lstat(src, &srcStat); + else + statResult = stat(src, &srcStat); + statErrno = errno; /* preserve across .exe attempt */ + +#ifdef WIN32_EXE + /* + * Here's the interesting part. Under Cygwin, if you have a file + * called "foo.exe", stat("foo", ...) will succeed, but open("foo", ...) + * will fail. We need to figure out what its name is supposed to be + * so we can create the correct destination file. + * + * If we don't have the "-e" flag set, we want "acp foo bar" to fail, + * not automatically find "foo.exe". That way, if we really were + * trying to copy "foo", it doesn't grab something we don't want. + */ + if (isCmdLine && statResult == 0) { + int tmpFd; + tmpFd = open(src, O_RDONLY | O_BINARY, 0); + if (tmpFd < 0) { + statResult = -1; + statErrno = ENOENT; + } else { + (void) close(tmpFd); + } + } + + /* + * If we didn't find the file, try it again with ".exe". + */ + if (isCmdLine && statResult < 0 && statErrno == ENOENT && (options & COPY_TRY_EXE)) { + srcExe = malloc(strlen(src) + 4 +1); + strcpy(srcExe, src); + strcat(srcExe, ".exe"); + + if (options & COPY_NO_DEREFERENCE) + statResult = lstat(srcExe, &srcStat); + else + statResult = stat(srcExe, &srcStat); + + if (statResult == 0 && !S_ISREG(srcStat.st_mode)) + statResult = -1; /* fail, use original statErrno below */ + + if (statResult == 0) { + /* found a .exe, copy that instead */ + dstExe = malloc(strlen(dst) + 4 +1); + strcpy(dstExe, dst); + strcat(dstExe, ".exe"); + + src = srcExe; + dst = dstExe; + } else { + DBUG(("--- couldn't find '%s' either\n", srcExe)); + } + } +#endif + if (statResult < 0) { + if (statErrno == ENOENT) + fprintf(stderr, "acp: file '%s' does not exist\n", src); + else + fprintf(stderr, "acp: unable to stat '%s': %s\n", + src, strerror(statErrno)); + retVal = -1; + goto bail; + } + + /* + * If "src" is a directory, ignore it if "recursive" isn't set. + * + * We want to create "dst" as a directory (or verify that it already + * exists as a directory), and then copy its contents. + */ + if (S_ISDIR(srcStat.st_mode)) { + if (!(options & COPY_RECURSIVE)) { + fprintf(stderr, "acp: omitting directory '%s'\n", src); + } else { + retVal = copyDirectory(src, dst, &srcStat, options); + } +#ifdef HAVE_SYMLINKS + } else if (S_ISLNK(srcStat.st_mode)) { + retVal = copySymlink(src, dst, &srcStat, options); +#endif + } else if (S_ISREG(srcStat.st_mode)) { + retVal = copyRegular(src, dst, &srcStat, options); + } else { + fprintf(stderr, "acp: skipping unusual file '%s' (mode=0%o)\n", + src, srcStat.st_mode); + retVal = -1; + } + +bail: + free(srcExe); + free(dstExe); + free(dstDir); + return retVal; +} + +int copyFile(const char* src, const char* dst, unsigned int options) +{ + return copyFileRecursive(src, dst, true, options); +} + + diff --git a/libs/host/Directories.cpp b/libs/host/Directories.cpp new file mode 100644 index 0000000..a34f5b7 --- /dev/null +++ b/libs/host/Directories.cpp @@ -0,0 +1,42 @@ +#include <host/Directories.h> +#include <utils/String8.h> +#include <sys/types.h> +#include <sys/stat.h> + +#ifdef HAVE_MS_C_RUNTIME +#include <direct.h> +#endif + +using namespace android; +using namespace std; + +string +parent_dir(const string& path) +{ + return string(String8(path.c_str()).getPathDir().string()); +} + +int +mkdirs(const char* last) +{ + String8 dest; + const char* s = last-1; + int err; + do { + s++; + if (s > last && (*s == '.' || *s == 0)) { + String8 part(last, s-last); + dest.appendPath(part); +#ifdef HAVE_MS_C_RUNTIME + err = _mkdir(dest.string()); +#else + err = mkdir(dest.string(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); +#endif + if (err != 0) { + return err; + } + last = s+1; + } + } while (*s); + return 0; +} diff --git a/libs/host/include/host/CopyFile.h b/libs/host/include/host/CopyFile.h new file mode 100644 index 0000000..e65712b --- /dev/null +++ b/libs/host/include/host/CopyFile.h @@ -0,0 +1,30 @@ +#ifndef _HOST_COPYFILE_H +#define _HOST_COPYFILE_H + +#include <stdbool.h> +#include <sys/stat.h> + +#if __cplusplus +extern "C" { +#endif + +// command line options +enum { + COPY_NO_DEREFERENCE = 0x00010000, // copy symlink link instead of target + COPY_TRY_EXE = 0x00020000, // on Win32, try adding '.exe' to filename + COPY_FORCE = 0x00040000, // override access permissions + COPY_PERMISSIONS = 0x00080000, // preserve mode, ownership, timestamps + COPY_TIMESTAMPS = 0x00100000, // preserve mode, ownership, timestamps + COPY_RECURSIVE = 0x00200000, // copy directories + COPY_UPDATE_ONLY = 0x00400000, // only copy if source file is newer + COPY_VERBOSE_MASK = 0x000000ff // talk lots +}; + +int copyFile(const char* src, const char* dst, unsigned int options); + +#if __cplusplus +} // extern "C" +#endif + +#endif // _HOST_COPYFILE_H + diff --git a/libs/host/include/host/Directories.h b/libs/host/include/host/Directories.h new file mode 100644 index 0000000..fccce46 --- /dev/null +++ b/libs/host/include/host/Directories.h @@ -0,0 +1,10 @@ +#ifndef HOST_MKDIRS_H +#define HOST_MKDIRS_H + +#include <string> + +std::string parent_dir(const std::string& path); + +extern "C" int mkdirs(const char* path); + +#endif // HOST_MKDIRS_H diff --git a/libs/host/include/host/pseudolocalize.h b/libs/host/include/host/pseudolocalize.h new file mode 100644 index 0000000..94cb034 --- /dev/null +++ b/libs/host/include/host/pseudolocalize.h @@ -0,0 +1,9 @@ +#ifndef HOST_PSEUDOLOCALIZE_H +#define HOST_PSEUDOLOCALIZE_H + +#include <string> + +std::string pseudolocalize_string(const std::string& source); + +#endif // HOST_PSEUDOLOCALIZE_H + diff --git a/libs/host/list.java b/libs/host/list.java new file mode 100644 index 0000000..30546e3 --- /dev/null +++ b/libs/host/list.java @@ -0,0 +1,35 @@ +import java.io.*; + +public class list { + private static char nibble(int c) { + return (char)(c < 10 ? ('0' + c) : ('a' + (c-10))); + } + public static void main(String[] argv) + { + ByteArrayOutputStream stream = new ByteArrayOutputStream(100); + OutputStreamWriter writer = null; + try { + writer = new OutputStreamWriter(stream, "utf-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(System.err); + } + + int n = Integer.parseInt(argv[1], 16); + try { + writer.write(n); + writer.close(); + } catch (IOException e) { + e.printStackTrace(System.err); + } + + byte[] array = stream.toByteArray(); + + System.out.print(" case '" + argv[0] + "': return \""); + for (int i=0; i<array.length; i++) { + int b = array[i]; + System.out.print("\\x" + nibble((b >> 4) & 0x0f) + nibble(b & 0xf)); + } + System.out.println("\";"); + } +} + diff --git a/libs/host/pseudolocalize.cpp b/libs/host/pseudolocalize.cpp new file mode 100644 index 0000000..a2b3c2f --- /dev/null +++ b/libs/host/pseudolocalize.cpp @@ -0,0 +1,119 @@ +#include <host/pseudolocalize.h> + +using namespace std; + +static const char* +pseudolocalize_char(char c) +{ + switch (c) { + case 'a': return "\xc4\x83"; + case 'b': return "\xcf\x84"; + case 'c': return "\xc4\x8b"; + case 'd': return "\xc4\x8f"; + case 'e': return "\xc4\x99"; + case 'f': return "\xc6\x92"; + case 'g': return "\xc4\x9d"; + case 'h': return "\xd1\x9b"; + case 'i': return "\xcf\x8a"; + case 'j': return "\xc4\xb5"; + case 'k': return "\xc4\xb8"; + case 'l': return "\xc4\xba"; + case 'm': return "\xe1\xb8\xbf"; + case 'n': return "\xd0\xb8"; + case 'o': return "\xcf\x8c"; + case 'p': return "\xcf\x81"; + case 'q': return "\x51"; + case 'r': return "\xd2\x91"; + case 's': return "\xc5\xa1"; + case 't': return "\xd1\x82"; + case 'u': return "\xce\xb0"; + case 'v': return "\x56"; + case 'w': return "\xe1\xba\x85"; + case 'x': return "\xd1\x85"; + case 'y': return "\xe1\xbb\xb3"; + case 'z': return "\xc5\xba"; + case 'A': return "\xc3\x85"; + case 'B': return "\xce\xb2"; + case 'C': return "\xc4\x88"; + case 'D': return "\xc4\x90"; + case 'E': return "\xd0\x84"; + case 'F': return "\xce\x93"; + case 'G': return "\xc4\x9e"; + case 'H': return "\xc4\xa6"; + case 'I': return "\xd0\x87"; + case 'J': return "\xc4\xb5"; + case 'K': return "\xc4\xb6"; + case 'L': return "\xc5\x81"; + case 'M': return "\xe1\xb8\xbe"; + case 'N': return "\xc5\x83"; + case 'O': return "\xce\x98"; + case 'P': return "\xcf\x81"; + case 'Q': return "\x71"; + case 'R': return "\xd0\xaf"; + case 'S': return "\xc8\x98"; + case 'T': return "\xc5\xa6"; + case 'U': return "\xc5\xa8"; + case 'V': return "\xce\xbd"; + case 'W': return "\xe1\xba\x84"; + case 'X': return "\xc3\x97"; + case 'Y': return "\xc2\xa5"; + case 'Z': return "\xc5\xbd"; + default: return NULL; + } +} + +/** + * Converts characters so they look like they've been localized. + * + * Note: This leaves escape sequences untouched so they can later be + * processed by ResTable::collectString in the normal way. + */ +string +pseudolocalize_string(const string& source) +{ + const char* s = source.c_str(); + string result; + const size_t I = source.length(); + for (size_t i=0; i<I; i++) { + char c = s[i]; + if (c == '\\') { + if (i<I-1) { + result += '\\'; + i++; + c = s[i]; + switch (c) { + case 'u': + // this one takes up 5 chars + result += string(s+i, 5); + i += 4; + break; + case 't': + case 'n': + case '#': + case '@': + case '?': + case '"': + case '\'': + case '\\': + default: + result += c; + break; + } + } else { + result += c; + } + } else { + const char* p = pseudolocalize_char(c); + if (p != NULL) { + result += p; + } else { + result += c; + } + } + } + + //printf("result=\'%s\'\n", result.c_str()); + return result; +} + + |