aboutsummaryrefslogtreecommitdiff
path: root/src/pproc.cpp
blob: eb52913ad1e236f620e0c22955fa4200674dcfa8 (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
#include <stdio.h>
#include <string.h>
#include "pproc.h"

void pprocHelp( PPROC *pproc )
{
	int maxlen = 0;
	for( int j = 0; pproc[j].proc || pproc[j].stateVar; j++ )
	{
		int len = strlen( pproc[j].lpWord );
		if( len > maxlen ) maxlen = len;
	}

	char fmt[100];
	sprintf( fmt, "  -%%c, --%%-%ds  %%s\n", maxlen );

	for( int j = 0; pproc[j].proc || pproc[j].stateVar; j++ )
	{
		printf( fmt,
			pproc[j].cChar,
			pproc[j].lpWord,
			pproc[j].shortHelp );
	}
}

void grabParamAsData( PPROC *pproc, char *str, int *aind, int *cind )
{
	switch( pproc->cMode & PPROC_TYPE )
	{
		case PPROC_BOOL_TRUE:
			*((bool *)pproc->stateVar) = true;
			break;
			
		case PPROC_BOOL_FALSE:
			*((bool *)pproc->stateVar) = false;
			break;
			
		case PPROC_BOOL_TOGGLE:
			*((bool *)pproc->stateVar) = !(*((bool *)pproc->stateVar));
			break;
			
		case PPROC_CHAR:
			(*cind)++;
			*((char *)pproc->stateVar) = str[(*cind)];
			break;
			
		case PPROC_SHORT:
			break;
			
		case PPROC_LONG:
			break;
			
		case PPREC_LONG_LONG:
			break;
			
		case PPROC_UCHAR:
			break;
			
		case PPROC_USHORT:
			break;
			
		case PPROC_ULONG:
			break;
			
		case PPREC_ULONG_LONG:
			break;
			
		case PPROC_FLOAT:
			break;
			
		case PPROC_DOUBLE:
			break;
			
		case PPROC_LONG_DOUBLE:
			break;
			
		case PPROC_STRING:
			strcpy( (char *)(pproc->stateVar), str );
			(*aind)++;
			break;
	}
}

void processParams( int argc, char *argv[], PPROC *pproc )
{
	bool bUsed;
	// Loop over all the params except the first, no params, no looping!
	for( int j = 1; j < argc; j++ )
	{
		//printf("Param[%d]: \"%s\"\n", j, argv[j] );
		if( argv[j][0] == '-' )
		{
			if( argv[j][1] == '-' )
			{
				bUsed = false;
				// Proccess a long-word param string
				for( int k = 0;
					pproc[k].proc != NULL || pproc[k].stateVar != NULL;
					k++ )
				{
					if( !strcmp( pproc[k].lpWord, &argv[j][2] ) )
					{
						bUsed = true;
						if( pproc[k].proc != NULL )
						{
							j += pproc[k].proc( argc-j, &argv[j] );
						}
						if( pproc[k].stateVar != NULL )
						{
							grabParamAsData( &pproc[k], argv[j+1], &j, &k );
						}
						break;
					}
				}
				if( !bUsed )
				{
					if( !strcmp( "help", &argv[j][2] ) )
					{
						pprocHelp( pproc );
					}
				}
			}
			else
			{
				bUsed = false;
				// Process a one char param string
				for( int k = 0;
					pproc[k].proc != NULL || pproc[k].stateVar != NULL;
					k++ )
				{
					if( pproc[k].cChar == argv[j][1] )
					{
						bUsed = true;
						if( pproc[k].proc != NULL )
						{
							j += pproc[k].proc( argc-j, &argv[j] );
						}
						if( pproc[k].stateVar != NULL )
						{
							int tmp = 1;
							if( argv[j][2] == '\0' )
							{
								grabParamAsData( &pproc[k], argv[j+1], &j, &tmp );
							}
							else
							{
								j--;
								grabParamAsData( &pproc[k], (&argv[j+1][2]), &j, &tmp );
							}
						}
						break;
					}
				}
				if( !bUsed )
				{
					if( argv[j][1] == 'h' )
					{
						pprocHelp( pproc );
					}
				}
			}
		}
		else
		{
			// Handle generic params here.
		}
	}
}