From 1d34f6293740f4f79f8b362c2d605fa2a527d538 Mon Sep 17 00:00:00 2001 From: Kyungwoo Lee Date: Thu, 3 Dec 2015 14:26:05 -0800 Subject: Refactoring resource string This pulls out resource string handling out of mscorrc into top level. The scripts (awk) are now parameterized to generate a unique string table for the given name. Added resourcestring.cpp to create an API "LoadResourceString". Added a few macros into resourcestring.h, which are used for definition/uses of the string table. --- src/CMakeLists.txt | 34 +++++++++++++ src/dlls/mscorrc/CMakeLists.txt | 35 +------------ src/dlls/mscorrc/full/CMakeLists.txt | 1 - src/dlls/mscorrc/processrc.awk | 65 ------------------------ src/dlls/mscorrc/rctocpp.awk | 80 ------------------------------ src/dlls/mscorrc/rctopo.awk | 21 -------- src/dlls/mscorrc/resourcestring.h | 23 --------- src/dlls/mscorrc/small/CMakeLists.txt | 23 +++------ src/nativeresources/.gitmirror | 1 + src/nativeresources/CMakeLists.txt | 9 ++++ src/nativeresources/processrc.awk | 64 ++++++++++++++++++++++++ src/nativeresources/rctocpp.awk | 84 ++++++++++++++++++++++++++++++++ src/nativeresources/rctopo.awk | 21 ++++++++ src/nativeresources/resourcestring.cpp | 63 ++++++++++++++++++++++++ src/nativeresources/resourcestring.h | 33 +++++++++++++ src/utilcode/ccomprc.cpp | 57 +++------------------- src/utilcode/crossgen/CMakeLists.txt | 3 ++ src/utilcode/dac/CMakeLists.txt | 3 +- src/utilcode/dyncrt/CMakeLists.txt | 3 +- src/utilcode/staticnohost/CMakeLists.txt | 5 ++ 20 files changed, 336 insertions(+), 292 deletions(-) delete mode 100644 src/dlls/mscorrc/processrc.awk delete mode 100644 src/dlls/mscorrc/rctocpp.awk delete mode 100644 src/dlls/mscorrc/rctopo.awk delete mode 100644 src/dlls/mscorrc/resourcestring.h create mode 100644 src/nativeresources/.gitmirror create mode 100644 src/nativeresources/CMakeLists.txt create mode 100644 src/nativeresources/processrc.awk create mode 100644 src/nativeresources/rctocpp.awk create mode 100644 src/nativeresources/rctopo.awk create mode 100644 src/nativeresources/resourcestring.cpp create mode 100644 src/nativeresources/resourcestring.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 31c37e5a39..7949679f8e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -34,6 +34,40 @@ if(CLR_CMAKE_PLATFORM_UNIX) # This prevents inclusion of standard C++ compiler headers set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdinc++") endif(NOT CLR_CMAKE_PLATFORM_DARWIN) + + set (NATIVE_RESOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/nativeresources) + include_directories(${NATIVE_RESOURCE_DIR}) + set (RC_TO_CPP ${NATIVE_RESOURCE_DIR}/rctocpp.awk) + set (PROCESS_RC ${NATIVE_RESOURCE_DIR}/processrc.awk) + set (RESOURCE_STRING_HEADER_DIR ${NATIVE_RESOURCE_DIR}) + + # Create a command to create a C++ source file containing an array of + # NativeStringResource structs which represent the information from a + # given Windows .rc file. The target C++ file path is returned in the + # variable specified by the TARGET_FILE parameter. + function(build_resources SOURCE TARGET_NAME TARGET_FILE) + + get_compile_definitions(PREPROCESS_DEFINITIONS) + get_include_directories(INCLUDE_DIRECTORIES) + + set(PREPROCESSED_SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.rc.i) + set(RESOURCE_ENTRY_ARRAY_CPP ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.cpp) + + add_custom_command( + OUTPUT ${RESOURCE_ENTRY_ARRAY_CPP} + # Preprocess the windows .rc file + COMMAND ${CMAKE_CXX_COMPILER} -E -P ${PREPROCESS_DEFINITIONS} ${INCLUDE_DIRECTORIES} -o ${PREPROCESSED_SOURCE} -x c ${SOURCE} + # Convert the preprocessed .rc file to a C++ file which will be used to make a static lib. + COMMAND ${AWK} -v name=${TARGET_NAME} -f ${RC_TO_CPP} -f ${PROCESS_RC} ${PREPROCESSED_SOURCE} >${RESOURCE_ENTRY_ARRAY_CPP} + DEPENDS ${SOURCE} ${RC_TO_CPP} ${PROCESS_RC} + ) + + include_directories(${RESOURCE_STRING_HEADER_DIR}) + set(${TARGET_FILE} ${RESOURCE_ENTRY_ARRAY_CPP} PARENT_SCOPE) + + endfunction() + + add_subdirectory(nativeresources) endif(CLR_CMAKE_PLATFORM_UNIX) add_subdirectory(utilcode) diff --git a/src/dlls/mscorrc/CMakeLists.txt b/src/dlls/mscorrc/CMakeLists.txt index e139bc3a30..f3288c97bc 100644 --- a/src/dlls/mscorrc/CMakeLists.txt +++ b/src/dlls/mscorrc/CMakeLists.txt @@ -4,38 +4,6 @@ if(WIN32) # remove /ltcg from resource-only libraries string(REPLACE "/LTCG" "" CMAKE_SHARED_LINKER_FLAGS_RELEASE ${CMAKE_SHARED_LINKER_FLAGS_RELEASE}) string(REPLACE "/LTCG" "" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO ${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}) -else() - - set (RC_TO_CPP ${CMAKE_CURRENT_SOURCE_DIR}/rctocpp.awk) - set (PROCESS_RC ${CMAKE_CURRENT_SOURCE_DIR}/processrc.awk) - set (RESOURCE_STRING_HEADER_DIR ${CMAKE_CURRENT_SOURCE_DIR}) - - # Create a command to create a C++ source file containing an array of - # NativeStringResource structs which represent the information from a - # given Windows .rc file. The target C++ file path is returned in the - # variable specified by the TARGET_FILE parameter. - function(build_resources SOURCE TARGET_NAME TARGET_FILE) - - get_compile_definitions(PREPROCESS_DEFINITIONS) - get_include_directories(INCLUDE_DIRECTORIES) - - set(PREPROCESSED_SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.rc.i) - set(RESOURCE_ENTRY_ARRAY_CPP ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.cpp) - - add_custom_command( - OUTPUT ${RESOURCE_ENTRY_ARRAY_CPP} - # Preprocess the windows .rc file - COMMAND ${CMAKE_CXX_COMPILER} -E -P ${PREPROCESS_DEFINITIONS} ${INCLUDE_DIRECTORIES} -o ${PREPROCESSED_SOURCE} -x c ${SOURCE} - # Convert the preprocessed .rc file to a C++ file which will be used to make a static lib. - COMMAND ${AWK} -f ${RC_TO_CPP} -f ${PROCESS_RC} ${PREPROCESSED_SOURCE} >${RESOURCE_ENTRY_ARRAY_CPP} - DEPENDS ${SOURCE} ${RC_TO_CPP} ${PROCESS_RC} - ) - - include_directories(${RESOURCE_STRING_HEADER_DIR}) - set(${TARGET_FILE} ${RESOURCE_ENTRY_ARRAY_CPP} PARENT_SCOPE) - - endfunction() - endif(WIN32) add_subdirectory(full) @@ -43,4 +11,5 @@ add_subdirectory(full) # Only add the small version of the resources if the platform is Windows. if(WIN32) add_subdirectory(small) -endif(WIN32) \ No newline at end of file +endif(WIN32) + diff --git a/src/dlls/mscorrc/full/CMakeLists.txt b/src/dlls/mscorrc/full/CMakeLists.txt index 1e9d2d149a..726ff0a47b 100644 --- a/src/dlls/mscorrc/full/CMakeLists.txt +++ b/src/dlls/mscorrc/full/CMakeLists.txt @@ -11,7 +11,6 @@ if(WIN32) install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$/mscorrc.debug.pdb DESTINATION PDB) else() - build_resources(${CMAKE_CURRENT_SOURCE_DIR}/../include.rc mscorrc_debug TARGET_CPP_FILE) add_library(mscorrc_debug STATIC diff --git a/src/dlls/mscorrc/processrc.awk b/src/dlls/mscorrc/processrc.awk deleted file mode 100644 index 71279ef7d2..0000000000 --- a/src/dlls/mscorrc/processrc.awk +++ /dev/null @@ -1,65 +0,0 @@ -# Parse string resources from Windows native resource file -# and pass them to the writestringentry function that -# is responsible for writing the resource id and string -# to a platform specific resource file. -# A script containing this function needs to be specified -# using the -f command line parameter before this script. - -BEGIN { - inStringTable = 0; - inBeginEnd = 0; - writeheader(); -} -{ - if ($1 == "STRINGTABLE" && $2 == "DISCARDABLE") - { - inStringTable = 1; - } - else if ($1 == "BEGIN") - { - inBeginEnd = inStringTable; - } - else if (inBeginEnd && $1 == "END") - { - inBeginEnd = 0; - inStringTable = 0; - } - else if (inBeginEnd && $1 != "") - { - # combine all items until the first string and remove them - # from the line - i = 1 - expression = "" - # string starts with either a quote or L followed by a quote - while (substr($i, 1, 1) != "\"" && substr($i, 1, 2) != "L\"") - { - # some of the resource IDs contain cast to HRESULT - gsub(/\(HRESULT\)/, "", $i); - # some of the resource IDs have trailing L - gsub(/L/, "", $i); - expression = expression $i; - $i=""; - i++; - } - # evaluate the resource ID expression - cmd = "echo $(("expression"))"; - cmd | getline var; - close(cmd); - # in case shell returned the result as a string, ensure the var has numeric type - var = var + 0; - - # remove the L prefix from strings - gsub(/L"/, "\"", $0); - # join strings "..." "..." into one - gsub(/" +"/, "", $0); - # remove all terminating newlines from the string - the msgfmt fails on those - # since it expects them to be at the end of the msgid as well - while(gsub(/\\n"/, "\"", $0)) {} - - # write the resource entry to the target file - writestringentry(var, $0); - } -} -END { - writefooter(); -} diff --git a/src/dlls/mscorrc/rctocpp.awk b/src/dlls/mscorrc/rctocpp.awk deleted file mode 100644 index 84b4b909fc..0000000000 --- a/src/dlls/mscorrc/rctocpp.awk +++ /dev/null @@ -1,80 +0,0 @@ -# Convert string resources from Windows native resource file to a C++ -# source file with an array of structs representing the resources. - -BEGIN { - numEntries = 0; -} - -# Takes a number and returns a string corresponding to its hex -# representation. A string representation that has fewer than 8 -# characters (not including the '0x' prefix) is padded with 0's -# to make it 8 characters. -# Example: an input of 49 yields "0x00000031". -function numberToHexString(number) -{ - quotient = number; - - hexString = ""; - for (digitCount = 0; digitCount < 8; digitCount++) - { - remainder = quotient % 16; - quotient = int(quotient / 16); - hexString = sprintf("%x"hexString, remainder); - } - - hexString = "0x" hexString; - return hexString; -} - -# Add each entry that is in our associative array of entries to the -# C++ array we are building. The C++ array will be ordered by the -# resourceId (lowest to highest) to facilitate quick lookups. -function writesortedentries() -{ - for (entry in resourceArray) - { - # Write the entries to the C++ array ordered by the ID. - printf " {%s,%s},\n", entry, resourceArray[entry] | "sort"; - } - - # Close the pipe to ensure that the data is written now. - close("sort"); -} - -# Write entry for a string resource -# This is called for each entry. Because we want to write them in -# sorted order once all the entries have come in, for now we just -# store each entry in an associative array. -function writestringentry(id, str) -{ - numEntries++; - - # Use the string representation of the ID as the array index - # because the precision of numeric indices can be lost during - # the number -> string -> number conversions that would occur - # if numeric indices are used. - resourceArray[numberToHexString(id)] = str; -} - -# Write file header and begin the array we will populate with the resources. -function writeheader() -{ - print "// This code was generated by rctocpp.awk and is not meant to be modified manually." - print "#include "; - print ""; - print "const NativeStringResource nativeStringResources[] = {"; -} - -# Write file footer -# This function is called after all of the entries have been given to -# writestringentry. Because we know there are no more entries, we can -# now write all the entries we received so far when this is called. -# After we have written all the entries, we close the array and add a -# constant for the size of the array for convenience. -function writefooter() -{ - writesortedentries(); - print "};"; - print ""; - print "const int NUMBER_OF_NATIVE_STRING_RESOURCES = " numEntries ";"; -} diff --git a/src/dlls/mscorrc/rctopo.awk b/src/dlls/mscorrc/rctopo.awk deleted file mode 100644 index 5229585ff5..0000000000 --- a/src/dlls/mscorrc/rctopo.awk +++ /dev/null @@ -1,21 +0,0 @@ -# Convert string resources from Windows native resource file to the -# input format of the gettext toolchain. - -# Write entry for a string resource -function writestringentry(id, str) -{ - idAsStr = sprintf("%X", id); - print "msgid \""idAsStr"\""; - print "msgstr" str; - print ""; -} - -# Write file header -function writeheader() -{ -} - -# Write file footer -function writefooter() -{ -} diff --git a/src/dlls/mscorrc/resourcestring.h b/src/dlls/mscorrc/resourcestring.h deleted file mode 100644 index 475705d015..0000000000 --- a/src/dlls/mscorrc/resourcestring.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. -// - -#ifndef __RESOURCE_STRING_H_ -#define __RESOURCE_STRING_H_ - -// Struct to contain a resource ID and its corresponding -// English language string. -struct NativeStringResource -{ - unsigned int resourceId; - const char* resourceString; -}; - -// Sorted array of all native string resources -extern const NativeStringResource nativeStringResources[]; - -// Number of entries in nativeStringResources -extern const int NUMBER_OF_NATIVE_STRING_RESOURCES; - -#endif // __RESOURCE_STRING_H_ diff --git a/src/dlls/mscorrc/small/CMakeLists.txt b/src/dlls/mscorrc/small/CMakeLists.txt index 3bd7fcd713..a86ae1a830 100644 --- a/src/dlls/mscorrc/small/CMakeLists.txt +++ b/src/dlls/mscorrc/small/CMakeLists.txt @@ -1,22 +1,11 @@ add_definitions(-DFX_VER_INTERNALNAME_STR=mscorrc.dll) -if(WIN32) +add_library(mscorrc SHARED + ../mscorrc.small.rc +) - add_library(mscorrc SHARED - ../mscorrc.small.rc - ) +# add the install targets +install (TARGETS mscorrc DESTINATION .) +install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$/mscorrc.pdb DESTINATION PDB) - # add the install targets - install (TARGETS mscorrc DESTINATION .) - install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$/mscorrc.pdb DESTINATION PDB) - -else() - - build_resources(${CMAKE_CURRENT_SOURCE_DIR}/../mscorrc.small.rc mscorrc TARGET_CPP_FILE) - - add_library(mscorrc STATIC - ${TARGET_CPP_FILE} - ) - -endif(WIN32) diff --git a/src/nativeresources/.gitmirror b/src/nativeresources/.gitmirror new file mode 100644 index 0000000000..f507630f94 --- /dev/null +++ b/src/nativeresources/.gitmirror @@ -0,0 +1 @@ +Only contents of this folder, excluding subfolders, will be mirrored by the Git-TFS Mirror. \ No newline at end of file diff --git a/src/nativeresources/CMakeLists.txt b/src/nativeresources/CMakeLists.txt new file mode 100644 index 0000000000..5bc326492a --- /dev/null +++ b/src/nativeresources/CMakeLists.txt @@ -0,0 +1,9 @@ +project(nativeresourcestring) + +add_compile_options(-fPIC) + +add_library(nativeresourcestring + STATIC + resourcestring.cpp +) + diff --git a/src/nativeresources/processrc.awk b/src/nativeresources/processrc.awk new file mode 100644 index 0000000000..1632753956 --- /dev/null +++ b/src/nativeresources/processrc.awk @@ -0,0 +1,64 @@ +# Parse string resources from Windows native resource file +# and pass them to the writestringentry function that +# is responsible for writing the resource id and string +# to a platform specific resource file. +# A script containing this function needs to be specified +# using the -f command line parameter before this script. + +BEGIN { + inStringTable = 0; + inBeginEnd = 0; + arrayName = "nativeStringResourceArray_" name; + tableName = "nativeStringResourceTable_" name; + writeheader(arrayName, tableName); +} +{ + if ($1 == "STRINGTABLE" && $2 == "DISCARDABLE") + { + inStringTable = 1; + } + else if ($1 == "BEGIN") + { + inBeginEnd = inStringTable; + } + else if (inBeginEnd && $1 == "END") + { + inBeginEnd = 0; + inStringTable = 0; + } + else if (inBeginEnd && $1 != "") + { + # combine all items until the first string and remove them + # from the line + i = 1 + expression = "" + # string starts with either a quote or L followed by a quote + while (substr($i, 1, 1) != "\"" && substr($i, 1, 2) != "L\"") + { + # some of the resource IDs contain cast to HRESULT + gsub(/\(HRESULT\)/, "", $i); + # some of the resource IDs have trailing L + gsub(/L/, "", $i); + expression = expression $i; + $i=""; + i++; + } + # evaluate the resource ID expression + cmd = "echo $(("expression"))"; + cmd | getline var; + close(cmd); + # in case shell returned the result as a string, ensure the var has numeric type + var = var + 0; + + # remove the L prefix from strings + gsub(/L"/, "\"", $0); + # join strings "..." "..." into one + gsub(/" +"/, "", $0); + + # write the resource entry to the target file + writestringentry(var, $0); + } +} +END { + writefooter(arrayName, tableName); +} diff --git a/src/nativeresources/rctocpp.awk b/src/nativeresources/rctocpp.awk new file mode 100644 index 0000000000..e631021b35 --- /dev/null +++ b/src/nativeresources/rctocpp.awk @@ -0,0 +1,84 @@ +# Convert string resources from Windows native resource file to a C++ +# source file with an array of structs representing the resources. + +BEGIN { + numEntries = 0; +} + +# Takes a number and returns a string corresponding to its hex +# representation. A string representation that has fewer than 8 +# characters (not including the '0x' prefix) is padded with 0's +# to make it 8 characters. +# Example: an input of 49 yields "0x00000031". +function numberToHexString(number) +{ + quotient = number; + + hexString = ""; + for (digitCount = 0; digitCount < 8; digitCount++) + { + remainder = quotient % 16; + quotient = int(quotient / 16); + hexString = sprintf("%x"hexString, remainder); + } + + hexString = "0x" hexString; + return hexString; +} + +# Add each entry that is in our associative array of entries to the +# C++ array we are building. The C++ array will be ordered by the +# resourceId (lowest to highest) to facilitate quick lookups. +function writesortedentries() +{ + for (entry in resourceArray) + { + # Write the entries to the C++ array ordered by the ID. + printf " {%s,%s},\n", entry, resourceArray[entry] | "sort"; + } + + # Close the pipe to ensure that the data is written now. + close("sort"); +} + +# Write entry for a string resource +# This is called for each entry. Because we want to write them in +# sorted order once all the entries have come in, for now we just +# store each entry in an associative array. +function writestringentry(id, str) +{ + numEntries++; + + # Use the string representation of the ID as the array index + # because the precision of numeric indices can be lost during + # the number -> string -> number conversions that would occur + # if numeric indices are used. + resourceArray[numberToHexString(id)] = str; +} + +# Write file header and begin the array we will populate with the resources. +function writeheader(arrayName, tableName) +{ + print "// This code was generated by rctocpp.awk and is not meant to be modified manually." + print "#include "; + print ""; + print "extern const NativeStringResourceTable " tableName ";"; + print "const NativeStringResource " arrayName "[] = {"; +} + +# Write file footer +# This function is called after all of the entries have been given to +# writestringentry. Because we know there are no more entries, we can +# now write all the entries we received so far when this is called. +# After we have written all the entries, we close the array and add a +# constant for the size of the array for convenience. +function writefooter(arrayName, tableName) +{ + writesortedentries(); + print "};"; + print ""; + + print "const NativeStringResourceTable " tableName " = {"; + print numEntries ","; + print arrayName "};"; +} diff --git a/src/nativeresources/rctopo.awk b/src/nativeresources/rctopo.awk new file mode 100644 index 0000000000..5229585ff5 --- /dev/null +++ b/src/nativeresources/rctopo.awk @@ -0,0 +1,21 @@ +# Convert string resources from Windows native resource file to the +# input format of the gettext toolchain. + +# Write entry for a string resource +function writestringentry(id, str) +{ + idAsStr = sprintf("%X", id); + print "msgid \""idAsStr"\""; + print "msgstr" str; + print ""; +} + +# Write file header +function writeheader() +{ +} + +# Write file footer +function writefooter() +{ +} diff --git a/src/nativeresources/resourcestring.cpp b/src/nativeresources/resourcestring.cpp new file mode 100644 index 0000000000..75fb1ececd --- /dev/null +++ b/src/nativeresources/resourcestring.cpp @@ -0,0 +1,63 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + + +#include +#include +#include "resourcestring.h" + +static int CompareNativeStringResources(const void *a, const void *b) +{ + unsigned int resourceIdA = ((NativeStringResource*)a)->resourceId; + unsigned int resourceIdB = ((NativeStringResource*)b)->resourceId; + + if (resourceIdA < resourceIdB) + return -1; + + if (resourceIdA == resourceIdB) + return 0; + + return 1; +} + +int LoadNativeStringResource(const NativeStringResourceTable &nativeStringResourceTable, unsigned int iResourceID, WCHAR* szBuffer, int iMax, int *pcwchUsed) +{ + int len = 0; + if (szBuffer && iMax) + { + // Search the sorted set of resources for the ID we're interested in. + NativeStringResource searchEntry = {iResourceID, NULL}; + NativeStringResource *resourceEntry = (NativeStringResource*)bsearch( + &searchEntry, + nativeStringResourceTable.table, + nativeStringResourceTable.size, + sizeof(NativeStringResource), + CompareNativeStringResources); + + if (resourceEntry != NULL) + { + len = PAL_GetResourceString(NULL, resourceEntry->resourceString, szBuffer, iMax); + } + else + { + // The resource ID wasn't found in our array. Fall back on returning the ID as a string. + len = _snwprintf(szBuffer, iMax - 1, W("[Undefined resource string ID:0x%X]"), iResourceID); + if ((len < 0) || (len == (iMax - 1))) + { + // Add string terminator if the result of _snwprintf didn't fit the buffer. + szBuffer[iMax - 1] = W('\0'); + len = iMax - 1; + } + } + } + + if (pcwchUsed) + { + *pcwchUsed = len; + } + + return S_OK; +} + diff --git a/src/nativeresources/resourcestring.h b/src/nativeresources/resourcestring.h new file mode 100644 index 0000000000..1545b3c48f --- /dev/null +++ b/src/nativeresources/resourcestring.h @@ -0,0 +1,33 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +#ifndef __RESOURCE_STRING_H_ +#define __RESOURCE_STRING_H_ + +// Struct to contain a resource ID and its corresponding +// English language string. +struct NativeStringResource +{ + unsigned int resourceId; + const char* resourceString; +}; + +struct NativeStringResourceTable +{ + const int size; + const NativeStringResource *table; +}; + +int LoadNativeStringResource(const NativeStringResourceTable &nativeStringResourceTable, unsigned int iResourceID, char16_t* szBuffer, int iMax, int *pcwchUsed); + +#define CONCAT(a, b) a ## b + +#define NATIVE_STRING_RESOURCE_TABLE(name) CONCAT(nativeStringResourceTable_, name) + +#define DECLARE_NATIVE_STRING_RESOURCE_TABLE(name) \ + extern const NativeStringResourceTable NATIVE_STRING_RESOURCE_TABLE(name) + +#endif // __RESOURCE_STRING_H_ + diff --git a/src/utilcode/ccomprc.cpp b/src/utilcode/ccomprc.cpp index 9e1cd8c1d1..da7d5c07e4 100644 --- a/src/utilcode/ccomprc.cpp +++ b/src/utilcode/ccomprc.cpp @@ -10,7 +10,11 @@ #include "ndpversion.h" #include "../dlls/mscorrc/resource.h" -#include "../dlls/mscorrc/resourcestring.h" +#ifdef FEATURE_PAL +#include "resourcestring.h" +#define NATIVE_STRING_RESOURCE_NAME mscorrc_debug +DECLARE_NATIVE_STRING_RESOURCE_TABLE(NATIVE_STRING_RESOURCE_NAME); +#endif #include "sstring.h" #include "stringarraylist.h" @@ -697,21 +701,6 @@ HRESULT CCompRC::LoadString(ResourceCategory eCategory, UINT iResourceID, __out_ return LoadString(eCategory, langId, iResourceID, szBuffer, iMax, pcwchUsed); } -// Used for comparing NativeStringResource elements by ID. -int CompareNativeStringResources(const void *a, const void *b) -{ - unsigned int resourceIdA = ((NativeStringResource*)a)->resourceId; - unsigned int resourceIdB = ((NativeStringResource*)b)->resourceId; - - if (resourceIdA < resourceIdB) - return -1; - - if (resourceIdA == resourceIdB) - return 0; - - return 1; -} - HRESULT CCompRC::LoadString(ResourceCategory eCategory, LocaleID langId, UINT iResourceID, __out_ecount(iMax) LPWSTR szBuffer, int iMax, int *pcwchUsed) { CONTRACTL @@ -875,40 +864,8 @@ HRESULT CCompRC::LoadString(ResourceCategory eCategory, LocaleID langId, UINT iR return hr; #else // !FEATURE_PAL - int len = 0; - if (szBuffer && iMax) - { - // Search the sorted set of resources for the ID we're interested in. - NativeStringResource searchEntry = {iResourceID, NULL}; - NativeStringResource *resourceEntry = (NativeStringResource*)bsearch( - &searchEntry, - nativeStringResources, - NUMBER_OF_NATIVE_STRING_RESOURCES, - sizeof(NativeStringResource), - CompareNativeStringResources); - - if (resourceEntry != NULL) - { - len = PAL_GetResourceString(m_pResourceDomain, resourceEntry->resourceString, szBuffer, iMax); - } - else - { - // The resource ID wasn't found in our array. Fall back on returning the ID as a string. - len = _snwprintf(szBuffer, iMax - 1, W("[Undefined resource string ID:0x%X]"), iResourceID); - if ((len < 0) || (len == (iMax - 1))) - { - // Add string terminator if the result of _snwprintf didn't fit the buffer. - szBuffer[iMax - 1] = W('\0'); - len = iMax - 1; - } - } - } - - if (pcwchUsed) - { - *pcwchUsed = len; - } - + LoadNativeStringResource(NATIVE_STRING_RESOURCE_TABLE(NATIVE_STRING_RESOURCE_NAME), iResourceID, + szBuffer, iMax, pcwchUsed); return S_OK; #endif // !FEATURE_PAL } diff --git a/src/utilcode/crossgen/CMakeLists.txt b/src/utilcode/crossgen/CMakeLists.txt index 1f427e1e63..70981e3214 100644 --- a/src/utilcode/crossgen/CMakeLists.txt +++ b/src/utilcode/crossgen/CMakeLists.txt @@ -6,3 +6,6 @@ list(APPEND UTILCODE_SOURCES add_precompiled_header(stdafx.h ../stdafx.cpp UTILCODE_SOURCES) add_library(utilcode_crossgen STATIC ${UTILCODE_SOURCES}) +if(CLR_CMAKE_PLATFORM_UNIX) + target_link_libraries(utilcode_crossgen nativeresourcestring) +endif(CLR_CMAKE_PLATFORM_UNIX) diff --git a/src/utilcode/dac/CMakeLists.txt b/src/utilcode/dac/CMakeLists.txt index de6157b31e..8a957be1ab 100644 --- a/src/utilcode/dac/CMakeLists.txt +++ b/src/utilcode/dac/CMakeLists.txt @@ -6,8 +6,9 @@ list(APPEND UTILCODE_SOURCES ../hostimpl.cpp) if(CLR_CMAKE_PLATFORM_UNIX) add_library(utilcode_dac STATIC ${UTILCODE_SOURCES}) + target_link_libraries(utilcode_dac nativeresourcestring) add_dependencies(utilcode_dac coreclrpal) else() add_precompiled_header(stdafx.h ../stdafx.cpp UTILCODE_SOURCES) add_library(utilcode_dac STATIC ${UTILCODE_SOURCES}) -endif(CLR_CMAKE_PLATFORM_UNIX) \ No newline at end of file +endif(CLR_CMAKE_PLATFORM_UNIX) diff --git a/src/utilcode/dyncrt/CMakeLists.txt b/src/utilcode/dyncrt/CMakeLists.txt index 59f29ab61b..c2164f327c 100644 --- a/src/utilcode/dyncrt/CMakeLists.txt +++ b/src/utilcode/dyncrt/CMakeLists.txt @@ -1,8 +1,9 @@ if(CLR_CMAKE_PLATFORM_UNIX) add_library(utilcode STATIC ${UTILCODE_SOURCES}) + target_link_libraries(utilcode nativeresourcestring) add_dependencies(utilcode coreclrpal) else() add_precompiled_header(stdafx.h ../stdafx.cpp UTILCODE_SOURCES) add_library(utilcode STATIC ${UTILCODE_SOURCES}) -endif(CLR_CMAKE_PLATFORM_UNIX) \ No newline at end of file +endif(CLR_CMAKE_PLATFORM_UNIX) diff --git a/src/utilcode/staticnohost/CMakeLists.txt b/src/utilcode/staticnohost/CMakeLists.txt index 6b63b39f7b..c9912502c2 100644 --- a/src/utilcode/staticnohost/CMakeLists.txt +++ b/src/utilcode/staticnohost/CMakeLists.txt @@ -9,3 +9,8 @@ if(WIN32) endif(WIN32) add_library(utilcodestaticnohost STATIC ${UTILCODE_SOURCES}) + +if(CLR_CMAKE_PLATFORM_UNIX) + target_link_libraries(utilcodestaticnohost nativeresourcestring) +endif(CLR_CMAKE_PLATFORM_UNIX) + -- cgit v1.2.3