Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * =====================================================================================
- * Filename: boost.cpp
- * Description: Converts two K objects to unsigned chars.
- * Compares a regex expression to a string
- * Boost was selected as the highest performing regex library therefore
- * further error catching is implemented
- *
- * Version: 2.0
- * Created: 04/04/12 15:03:03
- * Compiler: g++ -static -fPIC -shared -I/usr/local/boost_1_49_0 boost.cpp -o
- * boost.so libs/libboost_regex.a
- *
- * Author: Oliver Fletcher (ttolf@lboro.ac.uk)
- * University: Loughborough University
- *
- * =====================================================================================
- */
- #include <boost/regex.hpp>
- #include "../k.h"
- #include <iostream>
- using namespace std;
- int ERROR = 0;
- /*
- * === FUNCTION ======================================================================
- * Name: makeString
- * Description: Converts a K object into an unsigned char
- * Checks that the K object is the correct type else return an error
- * Inputs: item -> K object of type string (10h)
- * Returns: str1 -> String of type unsigned char
- * =====================================================================================
- */
- string makeString(K item)
- {
- int sz = item->n;
- int tp = item->t;
- unsigned char *str;
- char tmp[2];
- string str1;
- switch (tp) {
- case 10: /* Convert array of chars */
- str = (kC(item));
- str[sz] = '\0';
- str1 = reinterpret_cast<char*>(str);
- break;
- case -10: /* Convert single char */
- tmp[0] = item->g;
- tmp[1] = '\0';
- str1 = tmp;
- break;
- default: /* Catch invalid input */
- ERROR=1;
- break;
- }
- return str1;
- }
- /*
- * === FUNCTION ======================================================================
- * Name: regexp
- * Description: Takes two K strings, converts them to chars and uses boost to
- * test for regex match. Returns a K int indicating match
- * =====================================================================================
- */
- extern "C" K regexp(K input, K rexp)
- {
- string in = makeString(input); /* Convert input to string */
- string ex = makeString(rexp);
- if(ERROR == 1){ /* Deal with invalid input */
- ERROR = 0;
- return ki(-1);
- }
- boost::regex re(ex); /* and away we go... */
- bool out = regex_search(in, re);
- if(out){
- return ki(1);
- }
- else return ki(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement