Commit 3359c1f1 authored by lisj's avatar lisj
Browse files

增加GKLib

parent f2c80b44
/*!
\file strings.c
\brief Testing module for the string functions in GKlib
\date Started 3/5/2007
\author George
\version\verbatim $Id: strings.c 10711 2011-08-31 22:23:04Z karypis $ \endverbatim
*/
#include <GKlib.h>
/*************************************************************************/
/*! Testing module for gk_strstr_replace() */
/*************************************************************************/
void test_strstr_replace()
{
char *new_str;
int rc;
rc = gk_strstr_replace("This is a simple string", "s", "S", "", &new_str);
printf("%d, %s.\n", rc, new_str);
gk_free((void **)&new_str, LTERM);
rc = gk_strstr_replace("This is a simple string", "s", "S", "g", &new_str);
printf("%d, %s.\n", rc, new_str);
gk_free((void **)&new_str, LTERM);
rc = gk_strstr_replace("This is a simple SS & ss string", "s", "T", "g", &new_str);
printf("%d, %s.\n", rc, new_str);
gk_free((void **)&new_str, LTERM);
rc = gk_strstr_replace("This is a simple SS & ss string", "s", "T", "ig", &new_str);
printf("%d, %s.\n", rc, new_str);
gk_free((void **)&new_str, LTERM);
rc = gk_strstr_replace("This is a simple SS & ss string", "\\b\\w(\\w+)\\w\\b", "$1", "ig", &new_str);
printf("%d, %s.\n", rc, new_str);
gk_free((void **)&new_str, LTERM);
rc = gk_strstr_replace("This is a simple SS & ss string", "\\b\\w+\\b", "word", "ig", &new_str);
printf("%d, %s.\n", rc, new_str);
gk_free((void **)&new_str, LTERM);
rc = gk_strstr_replace("http://www.cs.umn.edu/This-is-something-T12323?pp=20&page=4",
"(http://www\\.cs\\.umn\\.edu/)(.*)-T(\\d+)", "$1$2-P$3", "g", &new_str);
printf("%d, %s.\n", rc, new_str);
gk_free((void **)&new_str, LTERM);
rc = gk_strstr_replace("http://www.cs.umn.edu/This-is-something-T12323?pp=20&page=4",
"(\\d+)", "number:$1", "ig", &new_str);
printf("%d, %s.\n", rc, new_str);
gk_free((void **)&new_str, LTERM);
rc = gk_strstr_replace("http://www.cs.umn.edu/This-is-something-T12323?pp=20&page=4",
"(http://www\\.cs\\.umn\\.edu/)", "[$1]", "g", &new_str);
printf("%d, %s.\n", rc, new_str);
gk_free((void **)&new_str, LTERM);
}
int main()
{
test_strstr_replace();
/*
{
int i;
for (i=0; i<1000; i++)
printf("%d\n", RandomInRange(3));
}
*/
}
/*!
\file timers.c
\brief Various timing functions
\date Started 4/12/2007
\author George
\version\verbatim $Id: timers.c 10711 2011-08-31 22:23:04Z karypis $ \endverbatim
*/
#include <GKlib.h>
/*************************************************************************
* This function returns the CPU seconds
**************************************************************************/
double gk_WClockSeconds(void)
{
#ifdef __GNUC__
struct timeval ctime;
gettimeofday(&ctime, NULL);
return (double)ctime.tv_sec + (double).000001*ctime.tv_usec;
#else
return (double)time(NULL);
#endif
}
/*************************************************************************
* This function returns the CPU seconds
**************************************************************************/
double gk_CPUSeconds(void)
{
//#ifdef __OPENMP__
#ifdef __OPENMPXXXX__
return omp_get_wtime();
#else
#if defined(WIN32) || defined(__MINGW32__)
return((double) clock()/CLOCKS_PER_SEC);
#else
struct rusage r;
getrusage(RUSAGE_SELF, &r);
return ((r.ru_utime.tv_sec + r.ru_stime.tv_sec) + 1.0e-6*(r.ru_utime.tv_usec + r.ru_stime.tv_usec));
#endif
#endif
}
/*!
\file tokenizer.c
\brief String tokenization routines
This file contains various routines for splitting an input string into
tokens and returning them in form of a list. The goal is to mimic perl's
split function.
\date Started 11/23/04
\author George
\version\verbatim $Id: tokenizer.c 10711 2011-08-31 22:23:04Z karypis $ \endverbatim
*/
#include <GKlib.h>
/************************************************************************
* This function tokenizes a string based on the user-supplied delimiters
* list. The resulting tokens are returned into an array of strings.
*************************************************************************/
void gk_strtokenize(char *str, char *delim, gk_Tokens_t *tokens)
{
int i, ntoks, slen;
tokens->strbuf = gk_strdup(str);
slen = strlen(str);
str = tokens->strbuf;
/* Scan once to determine the number of tokens */
for (ntoks=0, i=0; i<slen;) {
/* Consume all the consecutive characters from the delimiters list */
while (i<slen && strchr(delim, str[i]))
i++;
if (i == slen)
break;
ntoks++;
/* Consume all the consecutive characters from the token */
while (i<slen && !strchr(delim, str[i]))
i++;
}
tokens->ntoks = ntoks;
tokens->list = (char **)gk_malloc(ntoks*sizeof(char *), "strtokenize: tokens->list");
/* Scan a second time to mark and link the tokens */
for (ntoks=0, i=0; i<slen;) {
/* Consume all the consecutive characters from the delimiters list */
while (i<slen && strchr(delim, str[i]))
str[i++] = '\0';
if (i == slen)
break;
tokens->list[ntoks++] = str+i;
/* Consume all the consecutive characters from the token */
while (i<slen && !strchr(delim, str[i]))
i++;
}
}
/************************************************************************
* This function frees the memory associated with a gk_Tokens_t
*************************************************************************/
void gk_freetokenslist(gk_Tokens_t *tokens)
{
gk_free((void *)&tokens->list, &tokens->strbuf, LTERM);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment