#include /* In the FAQ section 4.2 it says not to include this header file when writing user-defined functions #include "libpq-fe.h"*/ /* #include "libpq-fe.h"*/ #include "postgres.h" #include "utils/palloc.h" #include "utils/mcxt.h" typedef struct { char natcode[6]; /* one extra for the NUL character to fit*/ char number[8];} /* one extra for the NUL character to fit*/ phone; /* These prototypes declare the requirements that Postgres places on these user written functions. */ phone *phone_in(char *outphone); char *phone_out(phone *inphone); /***************************************************************************** * Input/Output functions *****************************************************************************/ phone * phone_in(char *outphone) { char nat[6],num[8]; /* one extra for the NUL character to fit*/ phone *result; int arguments,i; arguments=sscanf(outphone,"%[0-9]-%[0-9]",nat,num);/* the input string stops at white space or at the maximum field width, whichever occurs first*/ printf("The function returned %d arguments\n",arguments); printf("national code:%s\nnumber:%s\n",nat,num); if (arguments!=2) return NULL; result=(phone *) palloc(sizeof(phone)); i=0; while (nat[i]!='\0') { result->natcode[i]=nat[i]; i++; } i=0; while (num[i]!='\0') { result->number[i]=num[i]; i++; } return (result); } char * phone_out(phone *inphone) { char *result; if (inphone==NULL) return NULL; result=(char *)palloc(60); sprintf(result,"%s-%-s",inphone->natcode,inphone->number); return result; } main() { phone *a,*b,*c; a = phone_in("01483-827294"); printf("a = %s\n", phone_out(a)); b = phone_in("0161-2242394"); printf("b = %s\n", phone_out(b)); c = phone_in("01189-887762"); printf("c = %s\n", phone_out(c)); }