How to include too long literals in a C ++ source?

I have a problem. Essentially, I need to keep a large list of whitelists inside my program, and I would like to include such a list directly - I donโ€™t want to distribute other libraries, etc., And I donโ€™t want to insert lines into the Win32 resource, for a number of reasons, which I donโ€™t want to do now.

I simply included my large whitelist in my .cpp file and was presented with this error:

1>ServicesWhitelist.cpp(2807): fatal error C1091: compiler limit: string exceeds 65535 bytes in length 

The string itself is approximately twice the permissible limit of VC ++. What is the best way to include such a large literal in a program?

EDIT:

I save the line as follows:

 const std::wstring servicesWhitelist ( L".NETFRAMEWORK|" L"_IOMEGA_ACTIVE_DISK_SERVICE_|" L"{6080A529-897E-4629-A488-ABA0C29B635E}|" L"{834170A7-AF3B-4D34-A757-E05EB29EE96D}|" L"{85CCB53B-23D8-4E73-B1B7-9DDB71827D9B}|" L"{95808DC4-FA4A-4C74-92FE-5B863F82066B}|" L"{A7447300-8075-4B0D-83F1-3D75C8EBC623}|" L"{D31A0762-0CEB-444E-ACFF-B049A1F6FE91}|" L"{E2B953A6-195A-44F9-9BA3-3D5F4E32BB55}|" L"{EDA5F5D3-9E0F-4F4D-8A13-1D1CF469C9CC}|" L"2WIREPCP|" //About 3800 more lines ); 

EDIT2 It was used at runtime in a manner similar to this:

 static const boost::wregex servicesWhitelistRegex(servicesWhitelist); std::wstring service; //code to populate service if (!boost::regex_match(service, servicesWhitelistRegex)) //Do something to print service 
+5
c ++ long-integer literals
source share
5 answers

How about an array? (you should put commas only after the legal limit for each element)

 const std::wstring servicesWhitelist[] = { L".NETFRAMEWORK|", L"_IOMEGA_ACTIVE_DISK_SERVICE_|", L"{6080A529-897E-4629-A488-ABA0C29B635E}|", L"{834170A7-AF3B-4D34-A757-E05EB29EE96D}|", L"{85CCB53B-23D8-4E73-B1B7-9DDB71827D9B}|", L"{95808DC4-FA4A-4C74-92FE-5B863F82066B}|", L"{A7447300-8075-4B0D-83F1-3D75C8EBC623}|", L"{D31A0762-0CEB-444E-ACFF-B049A1F6FE91}|", L"{E2B953A6-195A-44F9-9BA3-3D5F4E32BB55}|", L"{EDA5F5D3-9E0F-4F4D-8A13-1D1CF469C9CC}|", L"2WIREPCP|", ... }; 

You can use the statement below to get the concatenated string.

 accumulate(servicesWhitelist, servicesWhitelist+sizeof(servicesWhitelist)/sizeof(servicesWhitelist[0]), "") 
+8
source share

Suppose you really need to save a string> 64 thousand characters (that is, all of the above โ€œjust don'tโ€ solutions don't apply.)

To make MSVC happy, instead of saying:

 const char *foo = "abcd..."; 

You can convert a character string> 64k to single characters represented by integers:

 const char foo[] = { 97, 98, 99, 100, ..., 0 }; 

If each letter was converted to its ascii equivalent (97 == 'a', etc.), and at the end the NUL terminator was added.

MSVC2010 is at least pleased with this.

+3
source share

If this is about twice the limit, the obvious solution seems to be to save 2 (or 3) of these lines. :) I'm sure your code that reads them at runtime can handle this quite easily.

EDIT: Do you need to use regex for some reason? Could you split the large lines into a list of individual tokens and conduct a simple string comparison?

+2
source share

I do not pretend to:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/c573db8b-c9cd-43d7-9f89-202ba9417296/fatal-error-c1091

Use STL instead.

Code snippet

#include <sstream>

std::ostringstream oss;

oss << myString1 << myString2 << myString3 << myString4;

oss.str () will now return an instance of STL std :: string class and oss.str (). c_str () will return const char *

0
source share

The problem can be removed before (in Python):

 whitelist_services = { ".NETFRAMEWORK", "_IOMEGA_ACTIVE_DISK_SERVICE_" } if service in whitelist_services: print service, "is a whitelisted service" 

Direct C ++ translation will be:

 // g++ *.cc -std=c++0x && ./a.out #include <iostream> #include <unordered_set> namespace { typedef const wchar_t* str_t; // or ////typedef std::wstring str_t; str_t servicesWhitelist[] = { L".NETFRAMEWORK", L"_IOMEGA_ACTIVE_DISK_SERVICE_", }; const size_t N = sizeof(servicesWhitelist) / sizeof(*servicesWhitelist); // if you need to search for multiple services then a hash table // could speed searches up O(1). Otherwise std::find() on the array // might be sufficient O(N), or std::binary_search() on sorted array // O(log N) const std::unordered_set<str_t> services (servicesWhitelist, servicesWhitelist + N); } int main() { str_t service = L".NETFRAMEWORK"; if (services.find(service) != services.end()) std::wcout << service << " is a whitelisted service" << std::endl; } 
-one
source share

All Articles