c++ string pattern matching buffer data

1.1k views Asked by At

I have inbound buffer data from a script that I need the key => 'value' to so that I can run a math equation against it (yes, I know I need to convert to int). Since I am sure the data is string, I trying to run a pattern match against it. I see the inbound data, but I never get a positive match.

code:

int getmyData()
{

        char key[] = "total";
        char buff[BUFSIZ];
        FILE *fp = popen("php getMyorders.php 155", "r");
        while (fgets( buff, BUFSIZ, fp)){
                printf("%s", buff);
                //if (strstr(key, buff) == buff) {
                if (!memcmp(key, buff, sizeof(key) - 1)) {
                        std::cout << "Match "<< std::endl;
                }

        }
}

data output from print_f():

array(2) {
  ["success"]=>
  string(1) "1"
  ["return"]=>
  array(3) {
    [0]=>
    array(7) {
      ["orderid"]=>
      string(9) "198397652"
      ["created"]=>
      string(19) "2014-11-14 15:10:10"
      ["ordertype"]=>
      string(3) "Buy"
      ["price"]=>
      string(10) "0.00517290"
      ["quantity"]=>
      string(10) "0.00100000"
      ["orig_quantity"]=>
      string(10) "0.00100000"
      ["total"]=>
      string(10) "0.00000517"
    }
    [1]=>
    array(7) {
      ["orderid"]=>
      string(9) "198397685"
      ["created"]=>
      string(19) "2014-11-14 15:10:13"
      ["ordertype"]=>
      string(3) "Buy"
      ["price"]=>
      string(10) "0.00517290"
      ["quantity"]=>
      string(10) "0.00100000"
      ["orig_quantity"]=>
      string(10) "0.00100000"
      ["total"]=>
      string(10) "0.00000517"
    }
    [2]=>
    array(7) {
      ["orderid"]=>
      string(9) "198398295"
      ["created"]=>
      string(19) "2014-11-14 15:11:14"
      ["ordertype"]=>
      string(3) "Buy"
      ["price"]=>
      string(10) "0.00517290"
      ["quantity"]=>
      string(10) "0.00100000"
      ["orig_quantity"]=>
      string(10) "0.00100000"
      ["total"]=>
      string(10) "0.00000517"
    }
  }   
}

How would I get to ["total"] and add the # 3 to it? ["total"]+3?

1

There are 1 answers

0
Barry On BEST ANSWER

You are matching just the first 5 bytes of buff for "total", instead of actually searching. If your buffer does not contain any nulls, the function you want to use is strstr:

while (fgets( buff, BUFSIZ, fp)) {
    const char* total = strstr(buff, key);
    if (total) {
        // found our total, which should point
        // ["total"] =>
        //   ^
        //   here
    }
}

If your buffer can contain nulls, then you need to write a function that would be called memstr, which is pretty simple: just try to find it at every point:

const char* memstr(const char* str, size_t str_size, 
                   const char* target, size_t target_size) {

    for (size_t i = 0; i != str_size - target_size; ++i) {
        if (!memcmp(str + i, target, target_size)) {
            return str + i;
        }
    }

    return NULL;
}

whose usage in your case would be:

const char* total = memstr(buff, BUFSIZ, key, sizeof(key) - 1);