Skip to content

Commit

Permalink
Format source code using included clang-format profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Chad Trabant committed Jan 17, 2017
1 parent 15ab425 commit 0893212
Show file tree
Hide file tree
Showing 4 changed files with 1,246 additions and 1,242 deletions.
5 changes: 5 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BasedOnStyle: LLVM
AlwaysBreakAfterDefinitionReturnType: TopLevel
SpaceBeforeParens: Always
BreakBeforeBraces: Allman
ColumnLimit: 0
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
2017.017:
- Update libmseed to 2.18.
- Update libdali to 1.7.
- Reformat all source code using included clang-format profile.

2011.080:
- Update libmseed to 2.6.
Expand Down
319 changes: 162 additions & 157 deletions src/edir.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* modified: 2008.087
***************************************************************************/

#include "edir.h"
#include <stdlib.h>
#include <string.h>
#include "edir.h"

static int sortedirentries (EDIR *edirp);

Expand All @@ -31,75 +31,74 @@ eopendir (const char *dirname)
struct dirent *de, *decopy;
struct edirent *ede;
struct edirent *prevede = 0;
if ( ! dirname )

if (!dirname)
return NULL;

dirp = opendir (dirname);
if ( ! dirp )

if (!dirp)
return NULL;

/* Allocate new EDIR */
if ( ! (edirp = (EDIR *) malloc (sizeof (EDIR))) )
if (!(edirp = (EDIR *)malloc (sizeof (EDIR))))
{
closedir (dirp);
return NULL;
}

/* Read all directory entries */
while ((de = readdir (dirp)))
{
/* Allocate space for directory entry copy */
if (!(decopy = (struct dirent *)malloc (de->d_reclen)))
{
closedir (dirp);
eclosedir (edirp);
return NULL;
}

/* Read all directory entries */
while ( (de = readdir (dirp)) )
{
/* Allocate space for directory entry copy */
if ( ! (decopy = (struct dirent *) malloc (de->d_reclen)) )
{
closedir (dirp);
eclosedir (edirp);
return NULL;
}

/* Copy directory entry */
memcpy (decopy, de, de->d_reclen);

/* Allocate space for enhanced directory entry */
if ( ! (ede = (struct edirent *) calloc (1, sizeof(struct edirent))) )
{
closedir (dirp);
eclosedir (edirp);
return NULL;
}

ede->de = decopy;
ede->prev = prevede;

/* Add new enhanced directory entry to the list */
if ( prevede == 0 )
{
edirp->ents = ede;
}
else
{
prevede->next = ede;
}

prevede = ede;
}

closedir (dirp);

/* Sort directory entries */
if ( sortedirentries (edirp) < 0 )

/* Copy directory entry */
memcpy (decopy, de, de->d_reclen);

/* Allocate space for enhanced directory entry */
if (!(ede = (struct edirent *)calloc (1, sizeof (struct edirent))))
{
closedir (dirp);
eclosedir (edirp);
return NULL;
}


ede->de = decopy;
ede->prev = prevede;

/* Add new enhanced directory entry to the list */
if (prevede == 0)
{
edirp->ents = ede;
}
else
{
prevede->next = ede;
}

prevede = ede;
}

closedir (dirp);

/* Sort directory entries */
if (sortedirentries (edirp) < 0)
{
eclosedir (edirp);
return NULL;
}

/* Set the current entry to the top of the list */
edirp->current = edirp->ents;

return edirp;
} /* End of ediropen() */

return edirp;
} /* End of ediropen() */

/***************************************************************************
* ereaddir:
Expand All @@ -111,18 +110,17 @@ struct dirent *
ereaddir (EDIR *edirp)
{
struct edirent *ede;
if ( ! edirp )

if (!edirp)
return NULL;

ede = edirp->current;
if ( edirp->current )

if (edirp->current)
edirp->current = ede->next;

return (ede) ? ede->de : NULL;
} /* End of ereaddir() */

return (ede) ? ede->de : NULL;
} /* End of ereaddir() */

/***************************************************************************
* eclosedir:
Expand All @@ -135,30 +133,29 @@ int
eclosedir (EDIR *edirp)
{
struct edirent *ede, *nede;
if ( ! edirp )

if (!edirp)
return -1;

ede = edirp->ents;

/* Loop through associated entries and free them */
while ( ede )
{
nede = ede->next;
if ( ede->de )
free (ede->de);
free (ede);
ede = nede;
}
while (ede)
{
nede = ede->next;

if (ede->de)
free (ede->de);

free (ede);

ede = nede;
}

free (edirp);

return 0;
} /* End of eclosedir() */

return 0;
} /* End of eclosedir() */

/***************************************************************************
* sortedirentries:
Expand All @@ -176,87 +173,95 @@ sortedirentries (EDIR *edirp)
struct edirent *p, *q, *e, *top, *tail;
int nmerges, totalmerges;
int insize, psize, qsize, i;
if ( ! edirp )

if (!edirp)
return -1;

top = edirp->ents;
totalmerges = 0;
insize = 1;

for (;;)
{
p = top;
top = NULL;
tail = NULL;

nmerges = 0; /* count number of merges we do in this pass */

while (p)
{
p = top;
top = NULL;
tail = NULL;

nmerges = 0; /* count number of merges we do in this pass */

while ( p )
{
nmerges++; /* there exists a merge to be done */
totalmerges++;

/* step `insize' places along from p */
q = p;
psize = 0;
for (i = 0; i < insize; i++)
{
psize++;
q = q->next;
if ( ! q )
break;
}

/* if q hasn't fallen off end, we have two lists to merge */
qsize = insize;

/* now we have two lists; merge them */
while ( psize > 0 || (qsize > 0 && q) )
{
/* decide whether next element of merge comes from p or q */
if ( psize == 0 )
{ /* p is empty; e must come from q. */
e = q; q = q->next; qsize--;
}
else if ( qsize == 0 || ! q )
{ /* q is empty; e must come from p. */
e = p; p = p->next; psize--;
}
else if ( strcmp (p->de->d_name, q->de->d_name) <= 0 )
{ /* First element of p is lower (or same), e must come from p. */
e = p; p = p->next; psize--;
}
else
{ /* First element of q is lower; e must come from q. */
e = q; q = q->next; qsize--;
}

/* add the next element to the merged list */
if ( tail )
tail->next = e;
else
top = e;

e->prev = tail;
tail = e;
}

/* now p has stepped `insize' places along, and q has too */
p = q;
nmerges++; /* there exists a merge to be done */
totalmerges++;

/* step `insize' places along from p */
q = p;
psize = 0;
for (i = 0; i < insize; i++)
{
psize++;
q = q->next;
if (!q)
break;
}

/* if q hasn't fallen off end, we have two lists to merge */
qsize = insize;

/* now we have two lists; merge them */
while (psize > 0 || (qsize > 0 && q))
{
/* decide whether next element of merge comes from p or q */
if (psize == 0)
{ /* p is empty; e must come from q. */
e = q;
q = q->next;
qsize--;
}
else if (qsize == 0 || !q)
{ /* q is empty; e must come from p. */
e = p;
p = p->next;
psize--;
}

tail->next = NULL;

/* If we have done only one merge, we're finished. */
if ( nmerges <= 1 ) /* allow for nmerges==0, the empty list case */
{
edirp->ents = top;

return totalmerges;
else if (strcmp (p->de->d_name, q->de->d_name) <= 0)
{ /* First element of p is lower (or same), e must come from p. */
e = p;
p = p->next;
psize--;
}

/* Otherwise repeat, merging lists twice the size */
insize *= 2;
else
{ /* First element of q is lower; e must come from q. */
e = q;
q = q->next;
qsize--;
}

/* add the next element to the merged list */
if (tail)
tail->next = e;
else
top = e;

e->prev = tail;
tail = e;
}

/* now p has stepped `insize' places along, and q has too */
p = q;
}

tail->next = NULL;

/* If we have done only one merge, we're finished. */
if (nmerges <= 1) /* allow for nmerges==0, the empty list case */
{
edirp->ents = top;

return totalmerges;
}
} /* End of sortedirentries() */

/* Otherwise repeat, merging lists twice the size */
insize *= 2;
}
} /* End of sortedirentries() */
Loading

0 comments on commit 0893212

Please sign in to comment.