librnr  1.14.5
RoadNarrows Robotics Common Library 1
example_uri.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Common Library 1
4 //
5 // File: example_hash.c
6 //
7 /*! \file
8  *
9  * $LastChangedDate: 2011-11-18 13:30:34 -0700 (Fri, 18 Nov 2011) $
10  * $Rev: 1577 $
11  *
12  * \brief Example of using the librnr URI utilities.
13  *
14  * \author Robin Knight (robin.knight@roadnarrows.com)
15  *
16  * \pkgcopyright{2011-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
17  */
18 // Permission is hereby granted, without written agreement and without
19 // license or royalty fees, to use, copy, modify, and distribute this
20 // software and its documentation for any purpose, provided that
21 // (1) The above copyright notice and the following two paragraphs
22 // appear in all copies of the source code and (2) redistributions
23 // including binaries reproduces these notices in the supporting
24 // documentation. Substantial modifications to this software may be
25 // copyrighted by their authors and need not follow the licensing terms
26 // described here, provided that the new terms are clearly indicated in
27 // all files where they apply.
28 //
29 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
30 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
31 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
32 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
33 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
34 // THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
37 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
38 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
39 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
40 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
41 //
42 ////////////////////////////////////////////////////////////////////////////////
43 
44 #include <stdio.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <stdarg.h>
48 
49 #include "rnr/rnrconfig.h"
50 #include "rnr/new.h"
51 #include "rnr/uri.h"
52 
53 /*!
54  * \brief Make a safe string.
55  *
56  * \param s String to safe.
57  */
58 #define SAFESTR(s) ((s) != NULL? s: "")
59 
60 /*!
61  * \brief Strip leading and trailing white space.
62  *
63  * An 0 is insterted to strip the trailing white space.
64  *
65  * \param [out] s String to strip.
66  *
67  * \return Returns pointer to start of non-white space token.
68  */
69 static char *strip(char *s)
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 }
99 
100 /*!
101  * \brief Example main.
102  *
103  * \param argc Command-line argument count.
104  * \param argv Command-line argument list.
105  *
106  * \par Exit Status:
107  * Program exits with 0 success, \h_gt 0 on failure.
108  */
109 int main(int argc, char *argv[])
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
Uniform Resource Identifier (URI) parsing utilities declarations.
Memory allocation and deallocation declarations.
char * m_sPath
absolute file path
Definition: uri.h:64
void UriDelete(Uri_T *pUri)
Delete the URI compenent structure.
Definition: uri.c:397
int main(int argc, char *argv[])
Example main.
Definition: example_uri.c:109
char * m_sUserInfo
user info
Definition: uri.h:61
#define SAFESTR(s)
Make a safe string.
Definition: example_uri.c:58
RoadNarrows Robotics common configuration file.
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