Advertisement
Ollie920049

boost.cpp

May 3rd, 2012
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. /*
  2.  * =====================================================================================
  3.  *       Filename:  boost.cpp
  4.  *    Description:  Converts two K objects to unsigned chars.
  5.  *                  Compares a regex expression to a string
  6.  *                  Boost was selected as the highest performing regex library therefore
  7.  *                  further error catching is implemented
  8.  *
  9.  *        Version:  2.0
  10.  *        Created:  04/04/12 15:03:03
  11.  *       Compiler:  g++ -static -fPIC -shared -I/usr/local/boost_1_49_0 boost.cpp -o
  12.  *                  boost.so libs/libboost_regex.a
  13.  *
  14.  *         Author:  Oliver Fletcher (ttolf@lboro.ac.uk)
  15.  *     University:  Loughborough University
  16.  *
  17.  * =====================================================================================
  18.  */
  19.  
  20. #include <boost/regex.hpp>
  21. #include "../k.h"
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. int ERROR = 0;
  26.  
  27. /*
  28.  * ===  FUNCTION  ======================================================================
  29.  *         Name:  makeString
  30.  *  Description:  Converts a K object into an unsigned char
  31.  *                Checks that the K object is the correct type else return an error
  32.  *       Inputs:  item -> K object of type string (10h)
  33.  *      Returns:  str1 -> String of type unsigned char
  34.  * =====================================================================================
  35.  */
  36. string makeString(K item)
  37. {
  38.   int sz = item->n;
  39.   int tp = item->t;
  40.   unsigned char *str;
  41.   char tmp[2];
  42.   string str1;
  43.   switch (tp) {
  44.     case 10:                                    /* Convert array of chars */
  45.       str = (kC(item));
  46.       str[sz] = '\0';
  47.       str1 = reinterpret_cast<char*>(str);
  48.       break;
  49.     case -10:                                   /* Convert single char */
  50.       tmp[0] = item->g;
  51.       tmp[1] = '\0';
  52.       str1 = tmp;
  53.       break;
  54.     default:                                    /* Catch invalid input */
  55.       ERROR=1;
  56.       break;
  57.   }
  58.   return str1;
  59. }
  60.  
  61. /*
  62.  * ===  FUNCTION  ======================================================================
  63.  *         Name:  regexp
  64.  *  Description:  Takes two K strings, converts them to chars and uses boost to
  65.  *                test for regex match. Returns a K int indicating match
  66.  * =====================================================================================
  67.  */
  68. extern "C" K regexp(K input, K rexp)
  69. {
  70.   string in = makeString(input);                /* Convert input to string */
  71.   string ex = makeString(rexp);
  72.  
  73.   if(ERROR == 1){                               /* Deal with invalid input */
  74.     ERROR = 0;
  75.     return ki(-1);
  76.   }
  77.  
  78.   boost::regex re(ex);                          /* and away we go... */
  79.   bool out = regex_search(in, re);
  80.   if(out){
  81.     return ki(1);
  82.   }
  83.   else return ki(0);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement