mozartito

utils2.cpp

Dec 22nd, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <node.h>
  2. #include <v8.h>
  3. #include <string>
  4. #define NEW_ERROR(ar) ThrowException(Exception::TypeError(String::New(ar)))
  5. using namespace v8;
  6.  
  7. int escape_chars(std::string&,int);
  8.  
  9. Handle<Value> escape(const Arguments& args)
  10. {
  11.  
  12.   Local<String> arg = args[0]->ToString();
  13.   String::Utf8Value argc(arg);
  14.   std::string argcs(*argc);
  15.  
  16.   if (!args[1]->IsNumber())
  17.   {
  18.     NEW_ERROR("El segundo argumento ha de ser un número");
  19.   }
  20.  
  21.   // call
  22.   double rtv = escape_chars(argcs,args[1]->NumberValue());
  23.  
  24.   if (rtv == 0)
  25.   {
  26.     return String::New(argcs.c_str());
  27.   }
  28.   else
  29.   {
  30.     return False();
  31.   }
  32.  
  33. }
  34. int escape_chars(std::string& str, int flag)
  35. {
  36.  
  37.   /* flags 0: only ' and " | 1: HTML encode | 2: ', " and HTML*/
  38.  
  39. size_t f,flast;
  40.  
  41. if (str.length() < 1)
  42. {
  43.   return 1;
  44. }
  45.  
  46. if(flag == 0 || flag == 2)
  47. {
  48.     // single quote
  49.    
  50.     const char sq[2] = {0x27,0x00};
  51.     const char sqr[3] = {0x5c,0x27,0x00};
  52.    
  53.     f = str.find_first_of(sq);
  54.    
  55.     while(f != std::string::npos)
  56.     {
  57.         str.replace(f,1,sqr);
  58.        
  59.         if (str.length() > f+2)
  60.         {
  61.             f = str.find_first_of(sq,f+2);
  62.         }
  63.         else
  64.         {
  65.             break;
  66.         }
  67.        
  68.     }
  69.    
  70.     // double quote
  71.    
  72.     const char dq[2] = {0x22,0x00};
  73.     const char dqr[3] = {0x5c,0x22,0x00};
  74.    
  75.     f = str.find_first_of(dq);
  76.    
  77.     while(f != std::string::npos)
  78.     {
  79.         str.replace(f,1,dqr);
  80.         if (str.length() > f+2){
  81.             f = str.find_first_of(dq,f+2);
  82.         }
  83.         else
  84.         {
  85.             break;
  86.         }
  87.        
  88.     }
  89. }
  90.  
  91. if(flag == 1 || flag == 2)
  92. {
  93.     // HTML
  94.    
  95.     const char n[2] = {0x0A,0x00}; // \n
  96.     const char nr[2] = {0x0D,0x00}; // \r
  97.     const char nrr[5] = {0x3c,0x62,0x72,0x3e,0x00}; // <br>
  98.    
  99.     f = str.find_first_of(n);
  100.    
  101.     while(f != std::string::npos)
  102.     {
  103.        
  104.         flast = str.find_first_of( nr,f-1 );
  105.         if (flast != std::string::npos )
  106.         {
  107.             str.replace(flast,2,nrr);
  108.         }
  109.         else
  110.         {
  111.             str.replace(f,1,nrr);
  112.         }
  113.        
  114.         f = str.find_first_of(n);
  115.        
  116.     }
  117.    
  118.    
  119.     f = str.find_first_of(nr);
  120.    
  121.     while(f != std::string::npos)
  122.     {
  123.         str.replace(f,1,nrr);
  124.         f = str.find_first_of(nr);
  125.     }
  126.    
  127. }
  128.  
  129. return 0;
  130.  
  131. }
  132.  
  133. void init(Handle<Object> target) {
  134. target->Set(String::NewSymbol("escape"),
  135.     FunctionTemplate::New(escape)->GetFunction());
  136. }
  137. NODE_MODULE(utils2, init);
Advertisement
Add Comment
Please, Sign In to add comment