diff src/xs_stil.c @ 660:b0743dc9165d

Change tabs to 4 spaces, everywhere.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 02 Apr 2008 22:10:05 +0300
parents acaba070cf49
children 81b9a81b99f2
line wrap: on
line diff
--- a/src/xs_stil.c	Wed Apr 02 21:29:26 2008 +0300
+++ b/src/xs_stil.c	Wed Apr 02 22:10:05 2008 +0300
@@ -32,90 +32,90 @@
  */
 static gboolean xs_stildb_node_realloc(stil_node_t *node, gint nsubTunes)
 {
-	if (!node) return FALSE;
+    if (!node) return FALSE;
 
-	/* Re-allocate subTune structure if needed */
-	if (nsubTunes > node->nsubTunes) {
-		gint clearIndex, clearLength;
-		
-		node->subTunes =
-			(stil_subnode_t **) g_realloc(node->subTunes,
-			(nsubTunes + 1) * sizeof(stil_subnode_t **));
+    /* Re-allocate subTune structure if needed */
+    if (nsubTunes > node->nsubTunes) {
+        gint clearIndex, clearLength;
+        
+        node->subTunes =
+            (stil_subnode_t **) g_realloc(node->subTunes,
+            (nsubTunes + 1) * sizeof(stil_subnode_t **));
 
-		if (!node->subTunes) {
-			xs_error("SubTune pointer structure realloc failed.\n");
-			return FALSE;
-		}
-		
-		/* Clear the newly allocated memory */
-		if (node->nsubTunes == 0) {
-			clearIndex = 0;
-			clearLength = nsubTunes + 1;
-		} else {
-			clearIndex = node->nsubTunes + 1;
-			clearLength = (nsubTunes - clearIndex + 1);
-		}
-		memset(&(node->subTunes[clearIndex]), 0, clearLength * sizeof(stil_subnode_t **));
-		
-		node->nsubTunes = nsubTunes;
-	}
+        if (!node->subTunes) {
+            xs_error("SubTune pointer structure realloc failed.\n");
+            return FALSE;
+        }
+        
+        /* Clear the newly allocated memory */
+        if (node->nsubTunes == 0) {
+            clearIndex = 0;
+            clearLength = nsubTunes + 1;
+        } else {
+            clearIndex = node->nsubTunes + 1;
+            clearLength = (nsubTunes - clearIndex + 1);
+        }
+        memset(&(node->subTunes[clearIndex]), 0, clearLength * sizeof(stil_subnode_t **));
+        
+        node->nsubTunes = nsubTunes;
+    }
 
-	/* Allocate memory for subTune */
-	if (!node->subTunes[nsubTunes]) {
-		node->subTunes[nsubTunes] = (stil_subnode_t *)
-			g_malloc0(sizeof(stil_subnode_t));
-		
-		if (!node->subTunes[nsubTunes]) {
-			xs_error("SubTune structure malloc failed!\n");
-			return FALSE;
-		}
-	}
+    /* Allocate memory for subTune */
+    if (!node->subTunes[nsubTunes]) {
+        node->subTunes[nsubTunes] = (stil_subnode_t *)
+            g_malloc0(sizeof(stil_subnode_t));
+        
+        if (!node->subTunes[nsubTunes]) {
+            xs_error("SubTune structure malloc failed!\n");
+            return FALSE;
+        }
+    }
 
-	return TRUE;
+    return TRUE;
 }
 
 
 static void xs_stildb_node_free(stil_node_t *node)
 {
-	gint i;
-	stil_subnode_t *subnode;
+    gint i;
+    stil_subnode_t *subnode;
 
-	if (!node) return;
+    if (!node) return;
 
-	/* Free subtune information */
-	for (i = 0; i <= node->nsubTunes; i++) {
-		subnode = node->subTunes[i];
-		if (subnode) {
-			g_free(subnode->name);
-			g_free(subnode->author);
-			g_free(subnode->info);
-			g_free(subnode->title);
-			g_free(subnode);
-		}
-	}
-	g_free(node->subTunes);
-	g_free(node->filename);
-	g_free(node);
+    /* Free subtune information */
+    for (i = 0; i <= node->nsubTunes; i++) {
+        subnode = node->subTunes[i];
+        if (subnode) {
+            g_free(subnode->name);
+            g_free(subnode->author);
+            g_free(subnode->info);
+            g_free(subnode->title);
+            g_free(subnode);
+        }
+    }
+    g_free(node->subTunes);
+    g_free(node->filename);
+    g_free(node);
 }
 
 
 static stil_node_t *xs_stildb_node_new(gchar *filename)
 {
-	stil_node_t *result;
+    stil_node_t *result;
 
-	/* Allocate memory for new node */
-	result = (stil_node_t *) g_malloc0(sizeof(stil_node_t));
-	if (!result)
-		return NULL;
+    /* Allocate memory for new node */
+    result = (stil_node_t *) g_malloc0(sizeof(stil_node_t));
+    if (!result)
+        return NULL;
 
-	/* Allocate filename and initial space for one subtune */
-	result->filename = g_strdup(filename);
-	if (!result->filename || !xs_stildb_node_realloc(result, 1)) {
-		xs_stildb_node_free(result);
-		return NULL;
-	}
-	
-	return result;
+    /* Allocate filename and initial space for one subtune */
+    result->filename = g_strdup(filename);
+    if (!result->filename || !xs_stildb_node_realloc(result, 1)) {
+        xs_stildb_node_free(result);
+        return NULL;
+    }
+    
+    return result;
 }
 
 
@@ -123,204 +123,204 @@
  */
 static void xs_stildb_node_insert(xs_stildb_t *db, stil_node_t *node)
 {
-	assert(db != NULL);
+    assert(db != NULL);
 
-	if (db->nodes) {
-		/* The first node's pPrev points to last node */
-		LPREV = db->nodes->prev;	/* New node's prev = Previous last node */
-		db->nodes->prev->next = node;	/* Previous last node's next = New node */
-		db->nodes->prev = node;	/* New last node = New node */
-		LNEXT = NULL;	/* But next is NULL! */
-	} else {
-		db->nodes = node;	/* First node ... */
-		LPREV = node;	/* ... it's also last */
-		LNEXT = NULL;	/* But next is NULL! */
-	}
+    if (db->nodes) {
+        /* The first node's pPrev points to last node */
+        LPREV = db->nodes->prev;    /* New node's prev = Previous last node */
+        db->nodes->prev->next = node;    /* Previous last node's next = New node */
+        db->nodes->prev = node;    /* New last node = New node */
+        LNEXT = NULL;    /* But next is NULL! */
+    } else {
+        db->nodes = node;    /* First node ... */
+        LPREV = node;    /* ... it's also last */
+        LNEXT = NULL;    /* But next is NULL! */
+    }
 }
 
 
 /* Read database (additively) to given db-structure
  */
-#define XS_STILDB_MULTI							\
-	if (isMulti) {							\
-		isMulti = FALSE;					\
-		xs_pstrcat(&(tmnode->subTunes[subEntry]->info), "\n");\
-	}
+#define XS_STILDB_MULTI                            \
+    if (isMulti) {                            \
+        isMulti = FALSE;                    \
+        xs_pstrcat(&(tmnode->subTunes[subEntry]->info), "\n");\
+    }
 
 static void XS_STILDB_ERR(gint lineNum, gchar *inLine, const char *fmt, ...)
 {
-	va_list ap;
+    va_list ap;
 
-	va_start(ap, fmt);
-	xs_error(fmt, ap);
-	va_end(ap);
-	
-	fprintf(stderr, "#%d: '%s'\n", lineNum, inLine);
+    va_start(ap, fmt);
+    xs_error(fmt, ap);
+    va_end(ap);
+    
+    fprintf(stderr, "#%d: '%s'\n", lineNum, inLine);
 }
 
 gint xs_stildb_read(xs_stildb_t *db, gchar *dbFilename)
 {
-	FILE *inFile;
-	gchar inLine[XS_BUF_SIZE + 16];	/* Since we add some chars here and there */
-	size_t lineNum;
-	stil_node_t *tmnode;
-	gboolean isError, isMulti;
-	gint subEntry;
-	gchar *tmpLine = inLine;
-	assert(db != NULL);
+    FILE *inFile;
+    gchar inLine[XS_BUF_SIZE + 16];    /* Since we add some chars here and there */
+    size_t lineNum;
+    stil_node_t *tmnode;
+    gboolean isError, isMulti;
+    gint subEntry;
+    gchar *tmpLine = inLine;
+    assert(db != NULL);
 
-	/* Try to open the file */
-	if ((inFile = fopen(dbFilename, "ra")) == NULL) {
-		xs_error("Could not open STILDB '%s'\n", dbFilename);
-		return -1;
-	}
+    /* Try to open the file */
+    if ((inFile = fopen(dbFilename, "ra")) == NULL) {
+        xs_error("Could not open STILDB '%s'\n", dbFilename);
+        return -1;
+    }
 
-	/* Read and parse the data */
-	lineNum = 0;
-	isError = FALSE;
-	isMulti = FALSE;
-	tmnode = NULL;
-	subEntry = 0;
+    /* Read and parse the data */
+    lineNum = 0;
+    isError = FALSE;
+    isMulti = FALSE;
+    tmnode = NULL;
+    subEntry = 0;
 
-	while (!isError && fgets(inLine, XS_BUF_SIZE, inFile) != NULL) {
-		size_t linePos = 0, eolPos = 0;
-		xs_findeol(inLine, &eolPos);
-		inLine[eolPos] = 0;
-		lineNum++;
-		
-		tmpLine = XS_CS_STIL(inLine);
+    while (!isError && fgets(inLine, XS_BUF_SIZE, inFile) != NULL) {
+        size_t linePos = 0, eolPos = 0;
+        xs_findeol(inLine, &eolPos);
+        inLine[eolPos] = 0;
+        lineNum++;
+        
+        tmpLine = XS_CS_STIL(inLine);
 
-		switch (tmpLine[0]) {
-		case '/':
-			/* Check if we are already parsing entry */
-			isMulti = FALSE;
-			if (tmnode) {
-				XS_STILDB_ERR(lineNum, tmpLine,
-					"New entry found before end of current ('%s')!\n",
-					tmnode->filename);
-				xs_stildb_node_free(tmnode);
-			}
+        switch (tmpLine[0]) {
+        case '/':
+            /* Check if we are already parsing entry */
+            isMulti = FALSE;
+            if (tmnode) {
+                XS_STILDB_ERR(lineNum, tmpLine,
+                    "New entry found before end of current ('%s')!\n",
+                    tmnode->filename);
+                xs_stildb_node_free(tmnode);
+            }
 
-			/* A new node */
-			subEntry = 0;
-			tmnode = xs_stildb_node_new(tmpLine);
-			if (!tmnode) {
-				/* Allocation failed */
-				XS_STILDB_ERR(lineNum, tmpLine,
-					"Could not allocate new STILdb-node!\n");
-				isError = TRUE;
-			}
-			break;
+            /* A new node */
+            subEntry = 0;
+            tmnode = xs_stildb_node_new(tmpLine);
+            if (!tmnode) {
+                /* Allocation failed */
+                XS_STILDB_ERR(lineNum, tmpLine,
+                    "Could not allocate new STILdb-node!\n");
+                isError = TRUE;
+            }
+            break;
 
-		case '(':
-			/* A new sub-entry */
-			isMulti = FALSE;
-			linePos++;
-			if (tmpLine[linePos] == '#') {
-				linePos++;
-				if (isdigit(tmpLine[linePos])) {
-					size_t savePos = linePos;
-					xs_findnum(tmpLine, &linePos);
-					tmpLine[linePos] = 0;
-					subEntry = atol(&tmpLine[savePos]);
+        case '(':
+            /* A new sub-entry */
+            isMulti = FALSE;
+            linePos++;
+            if (tmpLine[linePos] == '#') {
+                linePos++;
+                if (isdigit(tmpLine[linePos])) {
+                    size_t savePos = linePos;
+                    xs_findnum(tmpLine, &linePos);
+                    tmpLine[linePos] = 0;
+                    subEntry = atol(&tmpLine[savePos]);
 
-					/* Sanity check */
-					if (subEntry < 1) {
-						XS_STILDB_ERR(lineNum, tmpLine,
-							"Number of subEntry (%i) for '%s' is invalid\n",
-							subEntry, tmnode->filename);
-						subEntry = 0;
-					}
-				} else {
-					XS_STILDB_ERR(lineNum, tmpLine,
-						"Syntax error, expected subEntry number.\n");
-					subEntry = 0;
-				}
-			} else {
-				XS_STILDB_ERR(lineNum, tmpLine,
-					"Syntax error, expected '#' before subEntry number.\n");
-				subEntry = 0;
-			}
+                    /* Sanity check */
+                    if (subEntry < 1) {
+                        XS_STILDB_ERR(lineNum, tmpLine,
+                            "Number of subEntry (%i) for '%s' is invalid\n",
+                            subEntry, tmnode->filename);
+                        subEntry = 0;
+                    }
+                } else {
+                    XS_STILDB_ERR(lineNum, tmpLine,
+                        "Syntax error, expected subEntry number.\n");
+                    subEntry = 0;
+                }
+            } else {
+                XS_STILDB_ERR(lineNum, tmpLine,
+                    "Syntax error, expected '#' before subEntry number.\n");
+                subEntry = 0;
+            }
 
-			break;
+            break;
 
-		case 0:
-		case '#':
-		case '\n':
-		case '\r':
-			/* End of entry/field */
-			isMulti = FALSE;
-			if (tmnode) {
-				/* Insert to database */
-				xs_stildb_node_insert(db, tmnode);
-				tmnode = NULL;
-			}
-			break;
+        case 0:
+        case '#':
+        case '\n':
+        case '\r':
+            /* End of entry/field */
+            isMulti = FALSE;
+            if (tmnode) {
+                /* Insert to database */
+                xs_stildb_node_insert(db, tmnode);
+                tmnode = NULL;
+            }
+            break;
 
-		default:
-			/* Check if we are parsing an entry */
-			xs_findnext(tmpLine, &linePos);
-			
-			if (!tmnode) {
-				XS_STILDB_ERR(lineNum, tmpLine,
-					"Entry data encountered outside of entry or syntax error!\n");
-				break;
-			}
+        default:
+            /* Check if we are parsing an entry */
+            xs_findnext(tmpLine, &linePos);
+            
+            if (!tmnode) {
+                XS_STILDB_ERR(lineNum, tmpLine,
+                    "Entry data encountered outside of entry or syntax error!\n");
+                break;
+            }
 
-			if (!xs_stildb_node_realloc(tmnode, subEntry)) {
-				XS_STILDB_ERR(lineNum, tmpLine,
-					"Could not (re)allocate memory for subEntries!\n");
-				isError = TRUE;
-				break;
-			}
-			
-			/* Some other type */
-			if (strncmp(tmpLine, "   NAME:", 8) == 0) {
-				XS_STILDB_MULTI;
-				g_free(tmnode->subTunes[subEntry]->name);
-				tmnode->subTunes[subEntry]->name = g_strdup(&tmpLine[9]);
-			} else if (strncmp(tmpLine, "  TITLE:", 8) == 0) {
-				XS_STILDB_MULTI;
-				isMulti = TRUE;
-				if (!tmnode->subTunes[subEntry]->title)
-					tmnode->subTunes[subEntry]->title = g_strdup(&tmpLine[9]);
-				xs_pstrcat(&(tmnode->subTunes[subEntry]->info), &tmpLine[2]);
-			} else if (strncmp(tmpLine, " AUTHOR:", 8) == 0) {
-				XS_STILDB_MULTI;
-				g_free(tmnode->subTunes[subEntry]->author);
-				tmnode->subTunes[subEntry]->author = g_strdup(&tmpLine[9]);
-			} else if (strncmp(tmpLine, " ARTIST:", 8) == 0) {
-				XS_STILDB_MULTI;
-				isMulti = TRUE;
-				xs_pstrcat(&(tmnode->subTunes[subEntry]->info), &tmpLine[1]);
-			} else if (strncmp(tmpLine, "COMMENT:", 8) == 0) {
-				XS_STILDB_MULTI;
-				isMulti = TRUE;
-				xs_pstrcat(&(tmnode->subTunes[subEntry]->info), tmpLine);
-			} else {
-				if (isMulti) {
-					xs_pstrcat(&(tmnode->subTunes[subEntry]->info), " ");
-					xs_pstrcat(&(tmnode->subTunes[subEntry]->info), &tmpLine[linePos]);
-				} else {
-					XS_STILDB_ERR(lineNum, tmpLine,
-					"Entry continuation found when isMulti == FALSE.\n");
-				}
-			}
-			break;
-		}
-		
-		XS_CS_FREE(tmpLine);
+            if (!xs_stildb_node_realloc(tmnode, subEntry)) {
+                XS_STILDB_ERR(lineNum, tmpLine,
+                    "Could not (re)allocate memory for subEntries!\n");
+                isError = TRUE;
+                break;
+            }
+            
+            /* Some other type */
+            if (strncmp(tmpLine, "   NAME:", 8) == 0) {
+                XS_STILDB_MULTI;
+                g_free(tmnode->subTunes[subEntry]->name);
+                tmnode->subTunes[subEntry]->name = g_strdup(&tmpLine[9]);
+            } else if (strncmp(tmpLine, "  TITLE:", 8) == 0) {
+                XS_STILDB_MULTI;
+                isMulti = TRUE;
+                if (!tmnode->subTunes[subEntry]->title)
+                    tmnode->subTunes[subEntry]->title = g_strdup(&tmpLine[9]);
+                xs_pstrcat(&(tmnode->subTunes[subEntry]->info), &tmpLine[2]);
+            } else if (strncmp(tmpLine, " AUTHOR:", 8) == 0) {
+                XS_STILDB_MULTI;
+                g_free(tmnode->subTunes[subEntry]->author);
+                tmnode->subTunes[subEntry]->author = g_strdup(&tmpLine[9]);
+            } else if (strncmp(tmpLine, " ARTIST:", 8) == 0) {
+                XS_STILDB_MULTI;
+                isMulti = TRUE;
+                xs_pstrcat(&(tmnode->subTunes[subEntry]->info), &tmpLine[1]);
+            } else if (strncmp(tmpLine, "COMMENT:", 8) == 0) {
+                XS_STILDB_MULTI;
+                isMulti = TRUE;
+                xs_pstrcat(&(tmnode->subTunes[subEntry]->info), tmpLine);
+            } else {
+                if (isMulti) {
+                    xs_pstrcat(&(tmnode->subTunes[subEntry]->info), " ");
+                    xs_pstrcat(&(tmnode->subTunes[subEntry]->info), &tmpLine[linePos]);
+                } else {
+                    XS_STILDB_ERR(lineNum, tmpLine,
+                    "Entry continuation found when isMulti == FALSE.\n");
+                }
+            }
+            break;
+        }
+        
+        XS_CS_FREE(tmpLine);
 
-	} /* while */
+    } /* while */
 
-	/* Check if there is one remaining node */
-	if (tmnode)
-		xs_stildb_node_insert(db, tmnode);
+    /* Check if there is one remaining node */
+    if (tmnode)
+        xs_stildb_node_insert(db, tmnode);
 
-	/* Close the file */
-	fclose(inFile);
+    /* Close the file */
+    fclose(inFile);
 
-	return 0;
+    return 0;
 }
 
 
@@ -328,10 +328,10 @@
  */
 static gint xs_stildb_cmp(const void *node1, const void *node2)
 {
-	/* We assume here that we never ever get NULL-pointers or similar */
-	return strcmp(
-		(*(stil_node_t **) node1)->filename,
-		(*(stil_node_t **) node2)->filename);
+    /* We assume here that we never ever get NULL-pointers or similar */
+    return strcmp(
+        (*(stil_node_t **) node1)->filename,
+        (*(stil_node_t **) node2)->filename);
 }
 
 
@@ -339,43 +339,43 @@
  */
 gint xs_stildb_index(xs_stildb_t *db)
 {
-	stil_node_t *curr;
-	size_t i;
+    stil_node_t *curr;
+    size_t i;
 
-	/* Free old index */
-	if (db->pindex) {
-		g_free(db->pindex);
-		db->pindex = NULL;
-	}
+    /* Free old index */
+    if (db->pindex) {
+        g_free(db->pindex);
+        db->pindex = NULL;
+    }
 
-	/* Get size of db */
-	curr = db->nodes;
-	db->n = 0;
-	while (curr) {
-		db->n++;
-		curr = curr->next;
-	}
+    /* Get size of db */
+    curr = db->nodes;
+    db->n = 0;
+    while (curr) {
+        db->n++;
+        curr = curr->next;
+    }
 
-	/* Check number of nodes */
-	if (db->n > 0) {
-		/* Allocate memory for index-table */
-		db->pindex = (stil_node_t **) g_malloc(sizeof(stil_node_t *) * db->n);
-		if (!db->pindex)
-			return -1;
+    /* Check number of nodes */
+    if (db->n > 0) {
+        /* Allocate memory for index-table */
+        db->pindex = (stil_node_t **) g_malloc(sizeof(stil_node_t *) * db->n);
+        if (!db->pindex)
+            return -1;
 
-		/* Get node-pointers to table */
-		i = 0;
-		curr = db->nodes;
-		while (curr && (i < db->n)) {
-			db->pindex[i++] = curr;
-			curr = curr->next;
-		}
+        /* Get node-pointers to table */
+        i = 0;
+        curr = db->nodes;
+        while (curr && (i < db->n)) {
+            db->pindex[i++] = curr;
+            curr = curr->next;
+        }
 
-		/* Sort the indexes */
-		qsort(db->pindex, db->n, sizeof(stil_node_t *), xs_stildb_cmp);
-	}
+        /* Sort the indexes */
+        qsort(db->pindex, db->n, sizeof(stil_node_t *), xs_stildb_cmp);
+    }
 
-	return 0;
+    return 0;
 }
 
 
@@ -383,30 +383,30 @@
  */
 void xs_stildb_free(xs_stildb_t *db)
 {
-	stil_node_t *curr, *next;
+    stil_node_t *curr, *next;
 
-	if (!db)
-		return;
+    if (!db)
+        return;
 
-	/* Free the memory allocated for nodes */
-	curr = db->nodes;
-	while (curr) {
-		next = curr->next;
-		xs_stildb_node_free(curr);
-		curr = next;
-	}
+    /* Free the memory allocated for nodes */
+    curr = db->nodes;
+    while (curr) {
+        next = curr->next;
+        xs_stildb_node_free(curr);
+        curr = next;
+    }
 
-	db->nodes = NULL;
+    db->nodes = NULL;
 
-	/* Free memory allocated for index */
-	if (db->pindex) {
-		g_free(db->pindex);
-		db->pindex = NULL;
-	}
+    /* Free memory allocated for index */
+    if (db->pindex) {
+        g_free(db->pindex);
+        db->pindex = NULL;
+    }
 
-	/* Free structure */
-	db->n = 0;
-	g_free(db);
+    /* Free structure */
+    db->n = 0;
+    g_free(db);
 }
 
 
@@ -414,18 +414,18 @@
  */
 stil_node_t *xs_stildb_get_node(xs_stildb_t *db, gchar *filename)
 {
-	stil_node_t keyItem, *key, **item;
+    stil_node_t keyItem, *key, **item;
 
-	/* Check the database pointers */
-	if (!db || !db->nodes || !db->pindex)
-		return NULL;
+    /* Check the database pointers */
+    if (!db || !db->nodes || !db->pindex)
+        return NULL;
 
-	/* Look-up index using binary search */
-	keyItem.filename = filename;
-	key = &keyItem;
-	item = bsearch(&key, db->pindex, db->n, sizeof(stil_node_t *), xs_stildb_cmp);
-	if (item)
-		return *item;
-	else
-		return NULL;
+    /* Look-up index using binary search */
+    keyItem.filename = filename;
+    key = &keyItem;
+    item = bsearch(&key, db->pindex, db->n, sizeof(stil_node_t *), xs_stildb_cmp);
+    if (item)
+        return *item;
+    else
+        return NULL;
 }