librnr  1.14.5
RoadNarrows Robotics Common Library 1
example_uri.c File Reference

Example of using the librnr URI utilities. More...

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include "rnr/rnrconfig.h"
#include "rnr/new.h"
#include "rnr/uri.h"

Go to the source code of this file.

Macros

#define SAFESTR(s)   ((s) != NULL? s: "")
 Make a safe string. More...
 

Functions

static char * strip (char *s)
 Strip leading and trailing white space. More...
 
int main (int argc, char *argv[])
 Example main. More...
 

Detailed Description

Example of using the librnr URI utilities.

LastChangedDate
2011-11-18 13:30:34 -0700 (Fri, 18 Nov 2011)
Rev
1577
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file example_uri.c.

Macro Definition Documentation

#define SAFESTR (   s)    ((s) != NULL? s: "")

Make a safe string.

Parameters
sString to safe.

Definition at line 58 of file example_uri.c.

Referenced by main().

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Example main.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Exit Status:
Program exits with 0 success, > 0 on failure.

Definition at line 109 of file example_uri.c.

References uri_struct_t::m_nPortNum, uri_struct_t::m_sHostName, uri_struct_t::m_sPath, uri_struct_t::m_sQuery, uri_struct_t::m_sScheme, uri_struct_t::m_sUserInfo, SAFESTR, strip(), UriDelete(), UriParseNew(), and UriStrNew().

110 {
111  char buf[256];
112  char *sCmd;
113  Uri_T *pUri;
114  char *sUri;
115 
116  printf("Uniform Resource Indentifier Example.\n");
117  printf(" (enter 'quit' to quit programe)\n\n");
118 
119  // process user commands
120  for(;;)
121  {
122  printf("enter uri> ");
123 
124  // read user input
125  if( !fgets(buf, (int)sizeof(buf), stdin) )
126  {
127  break;
128  }
129 
130  sCmd = strip(buf);
131 
132  // null command
133  if( (*sCmd == '\0') || (*sCmd == '\n') )
134  {
135  continue;
136  }
137 
138  else if( !strcmp(sCmd, "quit") )
139  {
140  break;
141  }
142 
143  else
144  {
145  pUri = UriParseNew(sCmd);
146  printf("UriParseNew() ->\n");
147  printf("{\n");
148  printf(" m_sScheme = \"%s\"\n", SAFESTR(pUri->m_sScheme));
149  printf(" m_sUserInfo = \"%s\"\n", SAFESTR(pUri->m_sUserInfo));
150  printf(" m_sHostName = \"%s\"\n", SAFESTR(pUri->m_sHostName));
151  printf(" m_nPortNum = %d\n", pUri->m_nPortNum);
152  printf(" m_sPath = \"%s\"\n", SAFESTR(pUri->m_sPath));
153  printf(" m_sQuery = \"%s\"\n", SAFESTR(pUri->m_sQuery));
154  printf("}\n");
155 
156  sUri = UriStrNew(pUri);
157  printf("UriStrNew() -> \"%s\"\n", SAFESTR(sUri));
158  delete(sUri);
159 
160  UriDelete(pUri);
161  printf("UriDelete() -> deleted\n");
162  }
163  }
164 
165  return 0;
166 }
char * m_sScheme
scheme
Definition: uri.h:60
static char * strip(char *s)
Strip leading and trailing white space.
Definition: example_uri.c:69
char * m_sQuery
query
Definition: uri.h:65
char * m_sPath
absolute file path
Definition: uri.h:64
void UriDelete(Uri_T *pUri)
Delete the URI compenent structure.
Definition: uri.c:397
char * m_sUserInfo
user info
Definition: uri.h:61
#define SAFESTR(s)
Make a safe string.
Definition: example_uri.c:58
char * UriStrNew(const Uri_T *pUri)
Construct a new URI string from the given URI components.
Definition: uri.c:321
char * m_sHostName
host name (domain or address)
Definition: uri.h:62
int m_nPortNum
port number
Definition: uri.h:63
Uri_T * UriParseNew(const char *sUri)
Parse a URI string.
Definition: uri.c:70
static char* strip ( char *  s)
static

Strip leading and trailing white space.

An 0 is insterted to strip the trailing white space.

Parameters
[out]sString to strip.
Returns
Returns pointer to start of non-white space token.

Definition at line 69 of file example_uri.c.

Referenced by main().

70 {
71  size_t n;
72  char *t;
73 
74  while( *s && isspace((int)*s))
75  {
76  s++;
77  }
78 
79  if( *s == '\0' )
80  {
81  return s;
82  }
83 
84  n = strlen(s);
85 
86  t = s + n - 1;
87 
88  while( (t > s) && isspace((int)*t))
89  {
90  t--;
91  }
92 
93  t++;
94 
95  *t = '\0';
96 
97  return s;
98 }