M
Mark Hobley
I am writing a C function that will be utilized by a pathwalker. I am fairly
new to C programming. The purpose of the function is to look at a path variable
and determine the longest element within it. This is for a Linux based system
and the function supports paths with a colon prefixed with an backslash escape
character. (Presumably I need to support this on Linux, because directory
names can contain a colon).
The code currently contains a rather ugly "goto treatasdefault" within the
switch constuct, and I am wondering if this can be rearranged to be more
presentable.
If I am in one branch of a switch, is there any mechanism that will allow me to
conditionally jump to the default branch, or is goto the only way? I know
that I can fallthrough, by eliminating break, but this will just fallthrough
to the next switch branch. I really want to jump to default.
The code is not yet complete and has not yet been tested.
sconst.h:
#ifndef SCONST_H
#define SCONST_H
#define DIRSEP '/'
#define PATHSEP ':'
#define ESCSEQ '\'
#endif
lonp.c
/* ***************************************************************************
Filename: lonp.c Version: 0.0.1 Date: 96/03/2010
Details: This function will return the length of the longest value within
a path variable. This is typically used within path walking applications.
Written by Mark Hobley.
(c) Copyright 2010 Mark Hobley.
This file can be redistributed under the terms of version 2 of the
GNU General Public Licence as published by the Free Software Foundation.
It is prohibited to include any part of this software into any other
software product or other work, not covered by version 2 of the
GNU General Public Licence.
*************************************************************************** */
/* ************************** Standard Includes *************************** */
#include <stddef.h> /* Required for NULL */
/* **************************** Other Includes **************************** */
#include "lonp.h"
/* ******************************* Functions ****************************** */
int longestpath(char *pathdata) {
char *element = pathdata;
char c;
int eoe = 0; /* False */
int lastescseq = 0; /* False */
int len = 0, longest = 0;
while (!eoe) {
c = element
switch (c) {
case NULL:
eoe = 1; /* True */
if (len > longest) {
longest = len;
}
break;
case ESCSEQ:
if (lastescseq != 0) {
lastescseq = 0
goto treatasdetault;
}
case PATHSEP:
if (lastescseq != 0) {
goto treatasdefault;
}
if (len > longest) {
longest = len;
}
len = 0; /* Reset Length */
break;
default:
treatasdefault:
++len;
break;
}
++element;
}
return(longest);
}
new to C programming. The purpose of the function is to look at a path variable
and determine the longest element within it. This is for a Linux based system
and the function supports paths with a colon prefixed with an backslash escape
character. (Presumably I need to support this on Linux, because directory
names can contain a colon).
The code currently contains a rather ugly "goto treatasdefault" within the
switch constuct, and I am wondering if this can be rearranged to be more
presentable.
If I am in one branch of a switch, is there any mechanism that will allow me to
conditionally jump to the default branch, or is goto the only way? I know
that I can fallthrough, by eliminating break, but this will just fallthrough
to the next switch branch. I really want to jump to default.
The code is not yet complete and has not yet been tested.
sconst.h:
#ifndef SCONST_H
#define SCONST_H
#define DIRSEP '/'
#define PATHSEP ':'
#define ESCSEQ '\'
#endif
lonp.c
/* ***************************************************************************
Filename: lonp.c Version: 0.0.1 Date: 96/03/2010
Details: This function will return the length of the longest value within
a path variable. This is typically used within path walking applications.
Written by Mark Hobley.
(c) Copyright 2010 Mark Hobley.
This file can be redistributed under the terms of version 2 of the
GNU General Public Licence as published by the Free Software Foundation.
It is prohibited to include any part of this software into any other
software product or other work, not covered by version 2 of the
GNU General Public Licence.
*************************************************************************** */
/* ************************** Standard Includes *************************** */
#include <stddef.h> /* Required for NULL */
/* **************************** Other Includes **************************** */
#include "lonp.h"
/* ******************************* Functions ****************************** */
int longestpath(char *pathdata) {
char *element = pathdata;
char c;
int eoe = 0; /* False */
int lastescseq = 0; /* False */
int len = 0, longest = 0;
while (!eoe) {
c = element
switch (c) {
case NULL:
eoe = 1; /* True */
if (len > longest) {
longest = len;
}
break;
case ESCSEQ:
if (lastescseq != 0) {
lastescseq = 0
goto treatasdetault;
}
case PATHSEP:
if (lastescseq != 0) {
goto treatasdefault;
}
if (len > longest) {
longest = len;
}
len = 0; /* Reset Length */
break;
default:
treatasdefault:
++len;
break;
}
++element;
}
return(longest);
}