testUtils.c 2.92 KB
Newer Older
johan's avatar
johan committed
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
/*
 testUtils.c

 Copyright (C) 2011 Belledonne Communications, Grenoble, France
 Author : Johan Pascal
 
 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 <stdlib.h>
#include <string.h>


#include "typedef.h"

void printUsage(char *command)
{
	printf ("Usage:\n %s -p|<input file name>\n\n This executable request one argument either:\n  -p : display the computation type(fixed or floating) and exit\nor\n  <input file name> : process the input file and write the output in a file with the same prefix a .out extension\n\n",command);
	exit (-1);

}

int getArgument(int argc, char *argv[], char** filePrefix)
{
	/* We have only one argument wich can be either the input filename or -p which will answer either floating or fixed according */
	/* computation mode being fixed or floating point */ 
	if (argc != 2) {
		printUsage(argv[0]);
		exit (-1);
	}

	if (argv[1][0] == '-') {
		if (argv[1][1] == 'p') { /* -p switch, return fixed or floating and exit */
#ifdef FLOATING_POINT
			printf ("floating\n");
#else /* ifdef FLOATING_POINT */
			printf ("fixed\n");
#endif /* ifdef FLOATING_POINT */
			exit(0);
		} else { /* unknow switch, display usage and exit */
			printUsage(argv[0]);
			exit (-1);
		}
	} else { /* argument is the input file */
		/* get the input file prefix */
  		int i = strlen(argv[1])-1;
		int pos = 0;
		while (pos==0) {
			if (argv[1][i]=='.') {
			pos = i;
			}
			i--;
			if (i==0) { 
				printf("%s - Error input file  %s doesn't contain any ., impossible to extract prefix\n", argv[0], argv[1]);
				exit(-1);
			}
  		}
		*filePrefix = malloc((pos+3)*sizeof(char));
		strncpy(*filePrefix, argv[1], pos);
		(*filePrefix)[pos]='\0';
	}

	return 0;
}

int getArgumentsMultiChannel(int argc, char *argv[], char *filePrefix[])
{
	while (argc>1) { /* loop over all the argument which shall be input file names */
		argc--;
		/* get the input file prefix */
  		int i = strlen(argv[argc])-1;
		int pos = 0;
		while (pos==0) {
			if (argv[argc][i]=='.') {
			pos = i;
			}
			i--;
			if (i==0) { 
				printf("%s - Error input file  %s doesn't contain any ., impossible to extract prefix\n", argv[0], argv[argc]);
				exit(-1);
			}
  		}
		
		filePrefix[argc-1] = malloc((pos+3)*sizeof(char));

		strncpy(filePrefix[argc-1], argv[argc], pos);
		filePrefix[argc-1][pos]='\0';
	}

	return 0;
}