Re: please verify the code



Roman Mashak wrote:
> I wrote function parsing the fully-quilified domain name and extracting
> 'host' and 'domain' parts seperately.

I am highly skeptical that the *functionality* of your code is correct.
I usually take URLs to be of the form:
(<subdomainname>.)*<host-domainname>.<top-level-domainname> where
certain domain names have exceptional rules (co.uk, for example).
Ripping off the top most part usually just gets your some
sub-assignment by the host themselves.

> [...] The important thing for me is to keep
> original string (pointed by 'fqdn' in function). Is my way of coding
> correct, is there a way to optimize function and make it not so clumsy (I
> like the code produced by K&R in their famous book: brief, clear,
> comprehensible, nothing superfluous :) ).

This newsgroup is poor place to come for "optimization" help, unless
you mean making it somehow look like K&R-style syntax or something like
that. But I happen to see your post so ...

First of all, why do you do the extra copying step through p? Its
unnecessary! Presumably, you have two destination buffers ready to
receive data, just do your final manipulations in there:

----------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

static int
parse_fqdn(char *fqdn, char *host, char *domain) {
char *s = strstr (fqdn, ".");
ptrdiff_t len = s - fqdn;

if (NULL == s) return -1;
memcpy (host, fqdn, len);
host[len] = '\0';
strcpy (domain, s + 1);
return 0;
}

#define BUFLEN 1024

int main(void) {
char dom[] = "www.my.example.dom.ain";
static char h[BUFLEN], d[BUFLEN];

if ( parse_fqdn(dom, h, d) == 0 )
printf("fqdn='%s'\nhostname='%s', domain name='%s'\n", dom, h,
d);
return EXIT_SUCCESS;
}

----------------------------------------

Ok, notice how I moved the BUFLEN definition below the parse_fqdn
definition? In this way you can make somewhat more enlightened
decisions about the target buffer length in the future, if necessary.
For example, in your main you could easily do this:

static char h[sizeof (dom)], d[sizeof (dom)];

and avoid the use of predefined fixed size buffers altogether. In more
dynamic situations, you would malloc char*'s with the strlen() of the
input strings or something to that effect.

Of course when you get tired of buffer overflows, performance problems,
and other buffer management issues, you can just use "The Better String
Library":

----------------------------------------

#include <stdlib.h>
#include <stdio.h>
#include "bstrlib.h"

int main(void) {
struct tagbstring dom = bsStatic ("www.my.example.dom.ain");
int i = bstrchr (&dom, '.'); /* I think bstrrchr is what you really
want here */
if (BSTR_ERR != i) {
bstring h = blk2bstr (dom.data, i);
bstring d = blk2bstr (dom.data + i + 1, dom.slen - (i + 1));
printf("fqdn='%s'\nhostname='%s', domain name='%s'\n",
dom.data,
bdatae (h, "<Out of memory>"),
bdatae (d, "<Out of memory>"));
bdestroy (h);
bdestroy (d);
}
return EXIT_SUCCESS;
}

----------------------------------------

Notice how there is no "#include <string.h>" here? As a good rule of
thumb -- there is a good chance you are introducing performance
problems if you have included that file.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

.



Relevant Pages

  • Re: Cannot return values of char variable
    ... - buffer = ... Since you seem to be trying to return a char pointer ... int id = random; ... content is interpreted as a string. ...
    (comp.lang.c)
  • Can anyone help me out?
    ... int main{ ... string fname; ... Packet *packet = new Packet; ... packet->package(filename, filesize, buffer); ...
    (comp.unix.programmer)
  • Re: How to split a compressed file programmatically?
    ... value is there in converting that to a string and then back to a long? ... And why allocate a new buffer for each chunk you want to write, ... void splitFile(string path, string path_parts, int size_part) ... just read chunks of the original file until you can't ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to split a compressed file programmatically?
    ... Personally, I would forget about the calculation altogether and just write a loop that keeps writing bytes in chunks as large as you want or however many bytes you have remaining, whichever is less, until you have no more bytes to write. ... The "size_part" variable is already a long; what possible value is there in converting that to a string and then back to a long? ... And why allocate a new buffer for each chunk you want to write, and why does that buffer have to be the length of the original file, and given that you're allocating a new buffer each time, why read the data anywhere other than the beginning of the buffer? ... void splitFile(string path, string path_parts, int size_part) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [RFC][PATCH] Escaping the arguments of kernel messages for sane user-space localisation
    ... escape certain characters in string arguments, ... We get rid of a 1K temporary buffer in printk. ... +static int printed_len; ... +static void printk_begin ...
    (Linux-Kernel)