2010-08-28 7 views
7

मुझे एक लाइब्रेरी की आवश्यकता है जो एक स्ट्रिंग/चार सरणी को URLencode कर सकता है।सी ++ यूआरएलएनकोड लाइब्रेरी (यूनिकोड सक्षम)?

अब, मैं हेक्स यहाँ की तरह एक ASCII सरणी सांकेतिक शब्दों में बदलना कर सकते हैं: http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4029

लेकिन मैं कुछ है कि यूनिकोड के साथ काम करता जरूरत है। नोट: पर लिनक्स और विंडोज़ पर! ,

char *encodedURL = curl_easy_escape(handle,WEBPAGE_URL, strlen(WEBPAGE_URL)); 

लेकिन पहले कि कर्ल की जरूरत है और यह भी यूनिकोड सक्षम, के रूप में एक के बाद strlen

उत्तर

8

अगर मैं सही ढंग से खोज पढ़ सकते हैं और आप कर्ल मुझे लगता है कि मैं एक समाधान है का उपयोग किए बिना अपने आप को ऐसा करने के लिए, चाहते हैं (UTF-8 sssuming) और मुझे लगता है कि इस URL एन्कोडिंग क्वेरी स्ट्रिंग के अनुरूप और पोर्टेबल तरीका है :

#include <boost/function_output_iterator.hpp> 
#include <boost/bind.hpp> 
#include <algorithm> 
#include <sstream> 
#include <iostream> 
#include <iterator> 
#include <iomanip> 

namespace { 
    std::string encimpl(std::string::value_type v) { 
    if (isalnum(v)) 
     return std::string()+v; 

    std::ostringstream enc; 
    enc << '%' << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << int(static_cast<unsigned char>(v)); 
    return enc.str(); 
    } 
} 

std::string urlencode(const std::string& url) { 
    // Find the start of the query string 
    const std::string::const_iterator start = std::find(url.begin(), url.end(), '?'); 

    // If there isn't one there's nothing to do! 
    if (start == url.end()) 
    return url; 

    // store the modified query string 
    std::string qstr; 

    std::transform(start+1, url.end(), 
       // Append the transform result to qstr 
       boost::make_function_output_iterator(boost::bind(static_cast<std::string& (std::string::*)(const std::string&)>(&std::string::append),&qstr,_1)), 
       encimpl); 
    return std::string(url.begin(), start+1) + qstr; 
} 

यह कोई अमानक बढ़ावा अलावा अन्य निर्भरता है और अगर आप को बढ़ावा देने पसंद नहीं है यह है कि दूर करने के लिए मुश्किल नहीं है निर्भरता।

int main() { 
    const char *testurls[] = {"http://foo.com/bar?abc<>de??90 210fg!\"$%", 
           "http://google.com", 
           "http://www.unicode.com/example?großpösna"}; 
    std::copy(testurls, &testurls[sizeof(testurls)/sizeof(*testurls)], 
       std::ostream_iterator<std::string>(std::cout,"\n")); 
    std::cout << "encode as: " << std::endl; 
    std::transform(testurls, &testurls[sizeof(testurls)/sizeof(*testurls)], 
        std::ostream_iterator<std::string>(std::cout,"\n"), 
        std::ptr_fun(urlencode)); 
} 

सब कौन सा लग रहा था काम करने के लिए:

http://foo.com/bar?abc<>de??90 210fg!"$% 
http://google.com 
http://www.unicode.com/example?großpösna 

बन जाता है:

http://foo.com/bar?abc%3C%3Ede%3F%3F90%20%20%20210fg%21%22%24%25 
http://google.com 
http://www.unicode.com/example?gro%C3%9Fp%C3%B6sna 

इन examples

3

आप UTF8 करने के लिए अपने यूनिकोड यूआरएल पहले परिवर्तित करने पर विचार कर सकते देखता नहीं है:

कर्ल एक काफी अच्छा है , यूटीएफ 8 डेटा आपके यूनिकोड डेटा को एएससीआईआई अक्षरों में ले जाएगा, एक बार जब आप यूटीएफ 8 में अपना यूआरएल प्राप्त कर लेंगे तो आप आसानी से एपीआई के साथ यूआरएल एन्कोड कर सकते हैं।

+0

साथ कौन सा वर्ग UTF8 नहीं है

मैं का उपयोग कर इसे परीक्षण किया यूनिकोड? – maxschlepzig

+0

यूटीएफ -8 यूनिकोड डेटा स्थानांतरित करने के लिए तार प्रोटोकॉल में से एक है। एएससीआईआई एन्कोडिंग के साथ पिछड़े संगत होने का इसका एक अतिरिक्त लाभ है। जीजे के सुझाव के लिए +1। – ivymike

+0

@maxschlepzig: मैंने भी ऐसा सोचा। लेकिन आज कुछ नया सीखा ... –

संबंधित मुद्दे