Advertisement
szabozoltan69

Modositott kod

Aug 22nd, 2011
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.65 KB | None | 0 0
  1. #include <sstream>
  2. using namespace std;
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <stdio.h>
  7. #include <cstdlib> //zol
  8. #include <string.h> //zol
  9.  
  10. #define PHP_NULL 0
  11. #define PHP_STRING 1
  12. #define PHP_INT 2
  13. #define PHP_BOOL 3
  14. #define PHP_ARRAY 4
  15. #define PHP_RESOURCE 5
  16. #define PHP_OBJECT 6
  17.  
  18. char* intstring(long i)
  19. {
  20.     char *buf = new char[11];
  21.     sprintf(buf, "%li", i); //zol
  22.     return buf;
  23. }
  24. char* doublestring(double i)
  25. {
  26.     char *buf = new char[16];
  27.     sprintf(buf, "%g", i); //zol
  28.     return buf;
  29. }
  30.  
  31. class php_var
  32. {
  33.     public:
  34.         php_var() // Constructor
  35.         {
  36.             type = PHP_NULL; // Make the var, but make it null.
  37.             container = "";
  38.         }
  39.         php_var(const char* str)
  40.         {
  41.             container = str;
  42.             type = PHP_STRING;
  43.         }
  44.         php_var(double i)
  45.         {
  46.             type = PHP_INT;
  47.             container = doublestring(i);
  48.         }
  49.         php_var(int i)
  50.         {
  51.             type = PHP_INT;
  52.             container = intstring(i);
  53.         }
  54.         php_var(unsigned int i)
  55.         {
  56.             container = intstring(i);
  57.             type = PHP_INT;
  58.         }
  59.         php_var(long i)
  60.         {
  61.             container = intstring(i);
  62.             type = PHP_INT;
  63.         }
  64.         php_var(const php_var &temp)
  65.         {
  66.             type = temp.type;
  67.             container = temp.container;
  68.             keys = temp.keys;
  69.             data = temp.data;
  70.             res = temp.res;
  71.         }
  72.         php_var(char * str)
  73.         {
  74.             container = str;
  75.             type = PHP_STRING;
  76.         }
  77.         php_var(string str)
  78.         {
  79.             container = str;
  80.             type = PHP_STRING;
  81.         }
  82.         php_var(bool b)
  83.         {
  84.             if(b)
  85.                 container = "1";
  86.             else
  87.                 container = "0";
  88.             type = PHP_BOOL;
  89.         }
  90.         operator const char*()
  91.         {
  92.             if(type == PHP_STRING || type == PHP_INT)
  93.                 return container.c_str();
  94.             else
  95.                 return "Array";
  96.         }
  97.         operator string()
  98.         {
  99.             if(type == PHP_STRING || type == PHP_INT)
  100.                 return container;
  101.             else
  102.                 return string("Array");
  103.         }
  104.         operator bool()
  105.         {
  106.             if(type != PHP_BOOL || (type == PHP_BOOL && container.compare("1") == 0))
  107.                 return true;
  108.             return false;
  109.         }
  110.         operator double()
  111.         {
  112.             return atof(container.c_str());
  113.         }
  114.         operator float()
  115.         {
  116.             return atof(container.c_str());
  117.         }
  118.         operator int()
  119.         {
  120.             return atoi(container.c_str());
  121.         }
  122.         operator unsigned int()
  123.         {
  124.             return atoi(container.c_str());
  125.         }
  126.         operator long()
  127.         {
  128.             return atol(container.c_str());
  129.         }
  130.         php_var &operator[](int subscript)
  131.         {
  132.             if(type == PHP_STRING)
  133.             {
  134.                 // return &container[subscript];
  135.             }
  136.             else if(type == PHP_ARRAY)
  137.             {
  138.                 php_var key = subscript;
  139.                 int i = 0;
  140.                 for(i = 0;i < keys.size(); ++i)
  141.                 {
  142.                     if(key.container.compare(keys[i].container) == 0)
  143.                         return data[i];
  144.                 }
  145.                 php_var temp;
  146.                 keys.push_back(key);
  147.                 data.push_back(temp);
  148.                 return data[i];
  149.             }
  150.         }
  151.         php_var &operator[](unsigned int subscript)
  152.         {
  153.             if(type == PHP_STRING)
  154.             {
  155.                 // return &container[subscript];
  156.             }
  157.             else if(type == PHP_ARRAY)
  158.             {
  159.                 php_var key = subscript;
  160.                 int i = 0;
  161.                 for(i = 0;i < keys.size(); ++i)
  162.                 {
  163.                     if(key.container.compare(keys[i].container) == 0)
  164.                         return data[i];
  165.                 }
  166.                 php_var temp;
  167.                 keys.push_back(key);
  168.                 data.push_back(temp);
  169.                 return data[i];
  170.             }
  171.         }
  172.         php_var &operator[](long subscript)
  173.         {
  174.             if(type == PHP_STRING)
  175.             {
  176.                 // return &container[subscript];
  177.             }
  178.             else if(type == PHP_ARRAY)
  179.             {
  180.                 php_var key = subscript;
  181.                 int i = 0;
  182.                 for(i = 0;i < keys.size(); ++i)
  183.                 {
  184.                     if(key.container.compare(keys[i].container) == 0)
  185.                         return data[i];
  186.                 }
  187.                 php_var temp;
  188.                 keys.push_back(key);
  189.                 data.push_back(temp);
  190.                 return data[i];
  191.             }
  192.         }
  193.         php_var &operator[](const char* subscript)
  194.         {
  195.             if(type == PHP_STRING)
  196.             {
  197.                 // return &container[subscript];
  198.             }
  199.             else if(type == PHP_ARRAY)
  200.             {
  201.                 php_var key = subscript;
  202.                 int i = 0;
  203.                 for(i = 0;i < keys.size(); ++i)
  204.                 {
  205.                     if(key.container.compare(keys[i].container) == 0)
  206.                         return data[i];
  207.                 }
  208.                 php_var temp;
  209.                 keys.push_back(key);
  210.                 data.push_back(temp);
  211.                 return data[i];
  212.             }
  213.         }
  214.         php_var &operator[](char* subscript)
  215.         {
  216.             if(type == PHP_STRING)
  217.             {
  218.                 // return &container[subscript];
  219.             }
  220.             else if(type == PHP_ARRAY)
  221.             {
  222.                 php_var key = subscript;
  223.                 int i = 0;
  224.                 for(i = 0;i < keys.size(); ++i)
  225.                 {
  226.                     if(key.container.compare(keys[i].container) == 0)
  227.                         return data[i];
  228.                 }
  229.                 php_var temp;
  230.                 keys.push_back(key);
  231.                 data.push_back(temp);
  232.                 return data[i];
  233.             }
  234.         }
  235.         php_var &operator[](string subscript)
  236.         {
  237.             if(type == PHP_STRING)
  238.             {
  239.                 // return &container[subscript];
  240.             }
  241.             else if(type == PHP_ARRAY)
  242.             {
  243.                 php_var key = subscript;
  244.                 int i = 0;
  245.                 for(i = 0;i < keys.size(); ++i)
  246.                 {
  247.                     if(key.container.compare(keys[i].container) == 0)
  248.                         return data[i];
  249.                 }
  250.                 php_var temp;
  251.                 keys.push_back(key);
  252.                 data.push_back(temp);
  253.                 return data[i];
  254.             }
  255.         }
  256.         php_var &operator[](php_var subscript)
  257.         {
  258.             if(type == PHP_STRING)
  259.             {
  260.                 // return (char *)&container[subscript];
  261.             }
  262.             else if(type == PHP_ARRAY)
  263.             {
  264.                 php_var key = subscript;
  265.                 int i = 0;
  266.                 for(i = 0;i < keys.size(); ++i)
  267.                 {
  268.                     if(key.container.compare(keys[i].container) == 0)
  269.                         return data[i];
  270.                 }
  271.                 php_var temp;
  272.                 keys.push_back(key);
  273.                 data.push_back(temp);
  274.                 return data[i];
  275.             }
  276.         }
  277.         bool operator<(int i)
  278.         {
  279.             if(atol(container.c_str()) < i)
  280.                 return true;
  281.             return false;
  282.         }
  283.         bool operator>(int i)
  284.         {
  285.             if(atol(container.c_str()) > i)
  286.                 return true;
  287.             return false;
  288.         }
  289.         bool operator<(php_var i)
  290.         {
  291.             if(atol(container.c_str()) < atol(i.container.c_str()))
  292.                 return true;
  293.             return false;
  294.         }
  295.         bool operator>(php_var i)
  296.         {
  297.             if(atol(container.c_str()) > atol(i.container.c_str()))
  298.                 return true;
  299.             return false;
  300.         }
  301.         bool operator!=(const char* cmp)
  302.         {
  303.             if(container.compare(cmp))
  304.                 return true;
  305.             return false;
  306.         }
  307.         bool operator!=(int i)
  308.         {
  309.             if(atol(container.c_str()) == i)
  310.                 return false;
  311.             return true;
  312.         }
  313.         bool operator!=(php_var var)
  314.         {
  315.             if(!container.compare(var.container))
  316.                 return false;
  317.             return true;
  318.         }
  319.         bool operator==(const char* cmp)
  320.         {
  321.             if(container.compare(cmp) == 0)
  322.                 return true;
  323.             return false;
  324.         }
  325.         bool operator==(int i)
  326.         {
  327.             if(atol(container.c_str()) == i)
  328.                 return true;
  329.             return false;
  330.         }
  331.         bool operator==(php_var var)
  332.         {
  333.             if(container.compare(var.container) == 0)
  334.                 return true;
  335.             return false;
  336.         }
  337.         int operator++(int i)
  338.         {
  339.             int ret = atol(container.c_str());
  340.             container = intstring(ret + i);
  341.             return ret;
  342.         }
  343.         int operator++()
  344.         {
  345.             int ret = atol(container.c_str()) + 1;
  346.             container = intstring(ret);
  347.             return ret;
  348.         }
  349.         int operator--(int i)
  350.         {
  351.             int ret = atol(container.c_str());
  352.             container = intstring(ret);
  353.             return ret;
  354.         }
  355.         int operator--()
  356.         {
  357.             int ret = atol(container.c_str()) + 1;
  358.             container = intstring(ret);
  359.             return ret;
  360.         }
  361.         void operator+=(int inc)
  362.         {
  363.             if(type == PHP_INT)
  364.             {
  365.                 container = intstring(atol(container.c_str()) + inc);
  366.             }
  367.         }
  368.         void operator+=(php_var str)
  369.         {
  370.             if(str.type == PHP_INT)
  371.             {
  372.                 container = intstring(atol(container.c_str()) + atoi(str.container.c_str()));
  373.                 if(type != PHP_INT && type != PHP_STRING)
  374.                     type = PHP_INT;
  375.             }
  376.             else
  377.             {
  378.                 container += str.container;
  379.                 if(type != PHP_STRING)
  380.                     type = PHP_STRING;
  381.             }
  382.         }
  383.         void operator+=(string str)
  384.         {
  385.             container += str;
  386.         }
  387.         void operator+=(const char* str)
  388.         {
  389.             container += str;
  390.         }
  391.         void operator+=(char* str)
  392.         {
  393.             container += str;
  394.         }
  395.         void operator-=(int dec)
  396.         {
  397.             if(type == PHP_INT)
  398.             {
  399.                 container = intstring(atol(container.c_str()) - dec);
  400.             }
  401.         }
  402.         void operator/(php_var i)
  403.         {
  404.             if(type == PHP_INT)
  405.                 container = doublestring(atof(container.c_str()) / atof(i.container.c_str()));
  406.         }
  407.         void operator-(php_var i)
  408.         {
  409.             if(type == PHP_INT)
  410.                 container = doublestring(atof(container.c_str()) - atof(i.container.c_str()));
  411.         }
  412.         friend ostream &operator<<( ostream &out, const php_var &var );
  413.         void to_array()
  414.         {
  415.             type = PHP_ARRAY;
  416.         }
  417.         string container; // Contains value.
  418.         vector<php_var> keys;
  419.         vector<php_var> data;
  420.         void *res;
  421.         void *obj_type;
  422.         int res_type;
  423.         int type; // Contains current type.
  424. };
  425. ostream &operator<<( ostream &out, const php_var &var )
  426. {
  427.     if(var.type == PHP_ARRAY)
  428.         out << "Array";
  429.     else
  430.         out << var.container;
  431. }
  432. bool operator<(int i,php_var v)
  433. {
  434.     return(v > i);
  435. }
  436. bool operator>(int i, php_var v)
  437. {
  438.     return(v < i);
  439. }
  440. php_var operator+(php_var l, php_var r)
  441. {
  442.     return (php_var)((int) l + (int) r);
  443. }
  444. php_var operator-(php_var l, php_var r)
  445. {
  446.     return (php_var)((int) l - (int) r);
  447. }
  448. #define is_identical(var, varb) (((var) == (varb) && (var).type == (varb).type) ? (php_var)true : (php_var)false)
  449.  
  450.  
  451. #include <stdarg.h>
  452. php_var array(int key = 0, ...)
  453. {
  454.     va_list ap;
  455.     va_start(ap, key);
  456.     php_var arr;
  457.     arr.to_array();
  458.     php_var key2;
  459.     int i = 0;
  460.     for(int i = 0; i < key / 2; ++i)
  461.     {
  462.         va_list aq;
  463.         va_start(aq, key2);
  464.         va_copy(ap, aq);
  465.         if(key2 == -1)
  466.         {
  467.             bool found = false;
  468.             for(;;)
  469.             {
  470.                 for(i = 0;i < arr.keys.size(); ++i)
  471.                 {
  472.                     if(arr.keys[i] == (php_var) i)
  473.                         found = true;
  474.                 }
  475.                 if(found)
  476.                     ++i;
  477.                 else
  478.                     break;
  479.             }
  480.             key2 = i;
  481.         }
  482.         php_var val = va_arg(ap, int);
  483.         arr.keys.push_back(key2);
  484.         arr.data.push_back(val);
  485.     va_end(aq);
  486.     };
  487.     va_end(ap);
  488.     return arr;
  489. }
  490.  
  491.  
  492. #define count(var) ((php_var) (var).keys.size())
  493.  
  494.  
  495.  
  496. int main(int _argc, char **_argv);
  497. int main(int _argc, char **_argv)
  498. {
  499.     php_var _array = (php_var)array(5, -1, (int)1, (int)2, (int)3, (int)4);
  500.     cout << (php_var)count(_array);
  501.     return 0;
  502. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement