summaryrefslogtreecommitdiff
path: root/app/tools/listxtp.c
blob: 35d0daaadde8ccdd910734d015295af16237c179 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/** \file listxtp.c
 * Create a contents list of all parameter files
 *
 * $Header: /home/dmarkle/xtrkcad-fork-cvs/xtrkcad/app/tools/listxtp.c,v 1.2 2009-05-31 21:55:37 tshead Exp $
 */

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2009 Martin Fischer
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

#include "dirent.h"

#define TRUE 1
#define FALSE 0
#define MAX_FILES 500
#ifdef _WIN32
	#define WIKIFORMATOPTION "/w"
	#pragma warning( disable : 4996 )
#else
	#define WIKIFORMATOPTION "-w"
#endif

#define CONTENTSCOMMAND "CONTENTS"

#ifndef WIN32
	#define stricmp strcasecmp
	#define strnicmp strncasecmp
#endif // !WIN32

/*
 * needed for qsort
 */

int
Compare( const void *s1, const void * s2 )
{
	char *str1 = *(char **)s1;
	char *str2 = *(char **)s2;

	return( strcmp( str1, str2 ));
}


int
main( int argc, char **argv )
{
	char buffer[ 512 ];
	int found;
	DIR *d;
	struct dirent *ent;
	FILE *fh;
	struct stat buf;
	char filename[ 256 ];
	char path[ 256 ];
	char *results[MAX_FILES];
	int cnt = 0;
	int i;
	int bWiki = FALSE;

	/*
		this is a fast hack: there is one optional argument 
		if this was found, set the flag and remove it from the array. 
		As there is a maximum of one more argument, one simple assignment should be enough.
	*/

	if( argc >= 2 && !strcmp(argv[ 1 ], WIKIFORMATOPTION )) {
		bWiki = TRUE;
		if( argc > 2 ) {
			argv[ 1 ] = argv[ 2 ];
			argv[ 2 ] = NULL;
		}
		argc--;
	}

	/* 
	 * only other argument is the name of the directory to search through
	 */
	if( argc == 2 ) {
		strcpy( path,  argv[ 1 ] );
	} else {
		if( argc == 1 ) {
			strcpy( path, "." );
		} else {
			printf( "Invalid nummer of arguments. Execute with: listxtp "WIKIFORMATOPTION" [dir]\n" );
		}
	}

	/*
	 * open the directory
	 */
	d = opendir( path );
	if( !d ) {
		printf( "Directory %s not found!\n", path);
		exit( 1 );
	}

	/*
	 * get all files from the directory
	 */
	while((ent = readdir(d)))
	{
		/*
		 * create full file name and get the state for that file
		 */

		strcpy( filename, path );
		strcat( filename, "\\" );
		strcat( filename, ent->d_name );

		if( stat( filename, &buf ) == -1 ) {
			fprintf( stderr, "Error getting file state for %s\n", filename );
			exit( 1 );
		}
		/*
		 * ignore any directories
		 */
		if( buf.st_mode & S_IFDIR )
			continue;

		/*
		 * open the file and search for a line beginning with CONTENTS
		 */ 
		found = FALSE;
		fh = fopen( filename, "rt" );
		if( fh ) {
			while( !found ) {
				if( fgets( buffer, sizeof( buffer ), fh )) {
					if( !strnicmp( buffer, CONTENTSCOMMAND, strlen( CONTENTSCOMMAND ))) {
						/*
						 * if found, store the restof the line and the filename
						 */
						buffer[ strlen( buffer ) - 1 ] = '\0';
						sprintf( buffer, "%s (%s)", buffer + strlen( CONTENTSCOMMAND ) + 1, ent->d_name );
						results[ cnt ] = malloc( strlen( buffer ) + 1 );
						strcpy( results[ cnt ], buffer );
						cnt++;
						if( cnt == MAX_FILES ) {
							fprintf( stderr, "Error: too many files\n" );
							exit( 1 );
						}
						found = TRUE;
					}
				} else {
					fprintf( stderr, "Nothing found in %s\n", filename );
					found = TRUE;
				}
			}
			fclose( fh );
		} else {
			fprintf( stderr, "Error opening %s\n", filename );
		}
	}

	/*
	 * sort the list that was created
	 */
	qsort( (void *)results, (size_t )cnt, sizeof( char *), Compare );

	/*
	 * print the results. If Wiki option was set, format the line for usage
	 * in the Wiki
	 */
	for( i = 0; i < cnt; i++) {
		if( bWiki ) {
			printf("~-\"\"%s\"\"\n", results[ i ] );
			if( (i < cnt - 1) && *results[ i ] != *results[ i + 1 ] )
				printf( "\n" );
		} else {
			printf( "%s\n", results[ i ]);
		}
	}
	return( 0 );
}