translator.c File Reference

Source token substituter. More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pcre.h>
#include "pair.h"
#include "lexer.h"
#include "serializer.h"
#include "translator.h"

Include dependency graph for translator.c:

Include dependency graph

Go to the source code of this file.

Defines

#define EVAL(expr, err)   if (expr) { errno = err; perror(pcerr); iret = EXIT_FAILURE; goto cleanup; }
 Evaluates given expression and sets adherent internal failure state; goes to cleanup.
#define ALLOC(str, vector)   EVAL((str = (char *) realloc(str, vector + sizeof('\0'))) == NULL, ENOMEM)
 Sends string to realloc (initialized with NONE ); calls adherent error in case thereof.
#define STRCAT(stra, strb)   ALLOC(stra, strlen(stra) + strlen(strb)); strcat(stra, strb)
 Concatenates two strings; allocating and setting error states.
#define STRSUB(stra, strb, offset)   ALLOC(stra, offset); sprintf(stra, "%.*s", offset, strb)
 Copies a given string, strb, starting at offset to a new string, stra.
#define STRCPY(to, from)   ALLOC(to, strlen(from)) strcpy(to, from);
 Copy from to to, setting error states.
#define NONE(a)   a = (char *) malloc(0)
 Prepares string for work, Vollendung.

Functions

int translate (char *pcin, pair_t *ppain)
 Translates template's source according to {key, value} pairs.
void translatetoken (char *pc, char **ppc)
 Translates source token according to {key, value} pairs, prompted of lexbuf.


Detailed Description

Source token substituter.

Definition in file translator.c.


Define Documentation

#define ALLOC str,
vector   )     EVAL((str = (char *) realloc(str, vector + sizeof('\0'))) == NULL, ENOMEM)
 

Sends string to realloc (initialized with NONE ); calls adherent error in case thereof.

Parameters:
str allocate
vector allocable quantum

Definition at line 20 of file translator.c.

#define EVAL expr,
err   )     if (expr) { errno = err; perror(pcerr); iret = EXIT_FAILURE; goto cleanup; }
 

Evaluates given expression and sets adherent internal failure state; goes to cleanup.

Parameters:
expr evaluable
err errno

Definition at line 16 of file translator.c.

#define NONE  )     a = (char *) malloc(0)
 

Prepares string for work, Vollendung.

Parameters:
a preparate

Definition at line 36 of file translator.c.

#define STRCAT stra,
strb   )     ALLOC(stra, strlen(stra) + strlen(strb)); strcat(stra, strb)
 

Concatenates two strings; allocating and setting error states.

Parameters:
stra whither (concatenate)
strb whence (concatenand)

Definition at line 24 of file translator.c.

#define STRCPY to,
from   )     ALLOC(to, strlen(from)) strcpy(to, from);
 

Copy from to to, setting error states.

Parameters:
to duplicate
from duplicand

Definition at line 33 of file translator.c.

Referenced by translatetoken().

#define STRSUB stra,
strb,
offset   )     ALLOC(stra, offset); sprintf(stra, "%.*s", offset, strb)
 

Copies a given string, strb, starting at offset to a new string, stra.

Parameters:
stra duplicate
strb duplicand
offset duplicable quantum

Definition at line 29 of file translator.c.


Function Documentation

int translate char *  pcin,
pair_t ppain
 

Translates template's source according to {key, value} pairs.

Parameters:
pcin untranslate [sic] source
ppain {key, value} pairs
Returns:
state eserialerr.

Definition at line 42 of file translator.c.

References lexbuf(), NONE, ppa, serialize(), and translatetoken().

Referenced by parsetag().

00043 {
00044   int iret = EXIT_SUCCESS;
00045   const char *pcmask = "%%__[[:alnum:]]+?__%%";
00046   char NONE(*pcbuf);
00047   ppa = ppain;
00048 
00049   lexbuf(pcin, pcmask, translatetoken, &pcbuf);
00050   /* Buck stops on FILE *. */
00051   serialize(pcbuf, ppa);
00052 
00053   return iret;
00054 }

Here is the call graph for this function:

void translatetoken char *  pc,
char **  ppc
 

Translates source token according to {key, value} pairs, prompted of lexbuf.

Parameters:
pc lexate text
ppc lexand

Definition at line 59 of file translator.c.

References EVAL, pair_t::i, NONE, ppa, pair_t::ppan, STRCPY, STRSUB, TRBEGIN, TRCONST, TRCONT, TREND, TRSUB, and TRVECT.

Referenced by translate().

00060 {
00061   pcre *pre;
00062   int i, ire, iret, aisub[TRSUB * TRCONST];                     /* iret: macro placeholder */
00063   const char *pcmask = "^%%__([[:alnum:]]+?)__%%$";
00064   const char *pcre;
00065   const char *pcerr = "translator.c translatetoken()";
00066   char NONE(*pctoken);
00067   STRCPY(*ppc, "");         /* Guard against uninitialized garbage in case of unaccounted-for keys. */
00068 
00069   pre = pcre_compile(pcmask, PCRE_CASELESS | PCRE_DOTALL, &pcre, &ire, NULL);
00070   EVAL(pcre_exec(pre, NULL, pc, strlen(pc), 0, 0, aisub, TRSUB * TRCONST) < 0, EINVAL);
00071   STRSUB(pctoken, pc + aisub[TRCONT * TRVECT + TRBEGIN], aisub[TRCONT * TRVECT + TREND] - aisub[TRCONT * TRVECT + TRBEGIN]);
00072   for (i = 0; i < ppa->i; i++) {
00073     if (strcmp((ppa->ppan + i)->pckey, pctoken) == 0) {
00074       STRCPY(*ppc, (ppa->ppan + i)->pcvalue);
00075     }
00076   }
00077 
00078   cleanup:
00079     free(pctoken);
00080 }


Generated on Tue Dec 7 06:38:25 2004 for CSCI101:ProjectLatex by  doxygen 1.3.9.1