Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_query.c File Reference

Implementation of the small boolean field-query language. More...

#include "nilorea/n_query.h"
#include "nilorea/n_log.h"
#include "nilorea/n_pcre.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
Include dependency graph for n_query.c:

Go to the source code of this file.

Data Structures

struct  N_QUERY
 Opaque compiled query (see n_query_compile / n_query_eval / n_query_free). More...
struct  PARSER
struct  QNODE

Enumerations

enum  {
  OP_EQ , OP_NE , OP_CONT , OP_NCONT ,
  OP_REGEX , OP_GT , OP_LT , OP_GE ,
  OP_LE
}
enum  { N_AND , N_OR , N_NOT , N_CMP }
enum  {
  TK_END , TK_WORD , TK_AND , TK_OR ,
  TK_NOT , TK_LP , TK_RP , TK_OP
}

Functions

static int ci_contains (const char *hay, const char *needle)
static char * dupn (const char *s, size_t n)
static int eval_node (const QNODE *n, N_QUERY_GET get, void *ud)
static int is_op_char (char c)
static void lex_next (PARSER *P)
N_QUERY * n_query_compile (const char *expr, char *errbuf, size_t errlen)
 Compile an expression into a query.
N_QUERY * n_query_compile_ex (const char *expr, const char *const *fields, size_t nfields, char *errbuf, size_t errlen)
 Compile expr, rejecting any field not in fields.
int n_query_eval (const N_QUERY *query, N_QUERY_GET get, void *user_data)
 Evaluate a compiled query against one record via get.
void n_query_free (N_QUERY **query)
 Free a compiled query and set the pointer to NULL.
static void node_free (QNODE *n)
static QNODE * node_new (int kind)
static QNODE * parse_and (PARSER *P)
static QNODE * parse_not (PARSER *P)
static QNODE * parse_or (PARSER *P)
static QNODE * parse_primary (PARSER *P)

Detailed Description

Implementation of the small boolean field-query language.

Definition in file n_query.c.


Data Structure Documentation

◆ N_QUERY

struct N_QUERY

Opaque compiled query (see n_query_compile / n_query_eval / n_query_free).

Examples
ex_query.c.

Definition at line 62 of file n_query.c.

Collaboration diagram for N_QUERY:
Data Fields
QNODE * root

◆ PARSER

struct PARSER

Definition at line 77 of file n_query.c.

Collaboration diagram for PARSER:
Data Fields
char err[160]
const char *const * fields
size_t nfields
const char * p
int tok
int top
char * tval

◆ QNODE

struct QNODE

Definition at line 52 of file n_query.c.

Collaboration diagram for QNODE:
Data Fields
struct QNODE * a
struct QNODE * b
char * field
int kind
int op
N_PCRE * re
char * value

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
OP_EQ 
OP_NE 
OP_CONT 
OP_NCONT 
OP_REGEX 
OP_GT 
OP_LT 
OP_GE 
OP_LE 

Definition at line 35 of file n_query.c.

◆ anonymous enum

anonymous enum
Enumerator
N_AND 
N_OR 
N_NOT 
N_CMP 

Definition at line 46 of file n_query.c.

◆ anonymous enum

anonymous enum
Enumerator
TK_END 
TK_WORD 
TK_AND 
TK_OR 
TK_NOT 
TK_LP 
TK_RP 
TK_OP 

Definition at line 67 of file n_query.c.

Function Documentation

◆ ci_contains()

int ci_contains ( const char * hay,
const char * needle )
static

Definition at line 103 of file n_query.c.

Referenced by eval_node().

Here is the caller graph for this function:

◆ dupn()

char * dupn ( const char * s,
size_t n )
static

Definition at line 88 of file n_query.c.

Referenced by lex_next(), and parse_primary().

Here is the caller graph for this function:

◆ eval_node()

int eval_node ( const QNODE * n,
N_QUERY_GET get,
void * ud )
static

Definition at line 446 of file n_query.c.

References QNODE::a, QNODE::b, ci_contains(), eval_node(), QNODE::field, QNODE::kind, N_AND, N_CMP, N_NOT, N_OR, npcre_match(), QNODE::op, OP_CONT, OP_EQ, OP_GE, OP_GT, OP_LE, OP_LT, OP_NCONT, OP_NE, OP_REGEX, QNODE::re, and QNODE::value.

Referenced by eval_node(), and n_query_eval().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_op_char()

int is_op_char ( char c)
static

Definition at line 98 of file n_query.c.

Referenced by lex_next().

Here is the caller graph for this function:

◆ lex_next()

void lex_next ( PARSER * P)
static

Definition at line 118 of file n_query.c.

References dupn(), is_op_char(), OP_CONT, OP_EQ, OP_GE, OP_GT, OP_LE, OP_LT, OP_NCONT, OP_NE, OP_REGEX, PARSER::p, TK_AND, TK_END, TK_LP, TK_NOT, TK_OP, TK_OR, TK_RP, TK_WORD, PARSER::tok, PARSER::top, and PARSER::tval.

Referenced by n_query_compile_ex(), parse_and(), parse_not(), parse_or(), and parse_primary().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ node_free()

void node_free ( QNODE * n)
static

Definition at line 231 of file n_query.c.

References QNODE::a, QNODE::b, QNODE::field, node_free(), npcre_delete(), QNODE::re, and QNODE::value.

Referenced by n_query_compile_ex(), n_query_free(), node_free(), parse_and(), parse_not(), parse_or(), and parse_primary().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ node_new()

QNODE * node_new ( int kind)
static

Definition at line 224 of file n_query.c.

References QNODE::kind.

Referenced by parse_and(), parse_not(), parse_or(), and parse_primary().

Here is the caller graph for this function:

◆ parse_and()

QNODE * parse_and ( PARSER * P)
static

Definition at line 347 of file n_query.c.

References QNODE::a, QNODE::b, lex_next(), N_AND, node_free(), node_new(), parse_not(), right(), TK_AND, and PARSER::tok.

Referenced by parse_or().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_not()

QNODE * parse_not ( PARSER * P)
static

Definition at line 328 of file n_query.c.

References QNODE::a, lex_next(), N_NOT, node_free(), node_new(), parse_not(), parse_primary(), TK_NOT, and PARSER::tok.

Referenced by parse_and(), and parse_not().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_or()

QNODE * parse_or ( PARSER * P)
static

Definition at line 373 of file n_query.c.

References QNODE::a, QNODE::b, lex_next(), N_OR, node_free(), node_new(), parse_and(), right(), TK_OR, and PARSER::tok.

Referenced by n_query_compile_ex(), and parse_primary().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_primary()

QNODE * parse_primary ( PARSER * P)
static

Definition at line 245 of file n_query.c.

References dupn(), PARSER::err, QNODE::field, PARSER::fields, lex_next(), N_CMP, PARSER::nfields, node_free(), node_new(), npcre_new(), QNODE::op, OP_REGEX, parse_or(), QNODE::re, TK_AND, TK_LP, TK_NOT, TK_OP, TK_OR, TK_RP, TK_WORD, PARSER::tok, PARSER::top, PARSER::tval, and QNODE::value.

Referenced by parse_not().

Here is the call graph for this function:
Here is the caller graph for this function: