//gcc -Wall ../m_function.c section_5.6_pointer_array.c && ll | awk '{print $5}' | ./a.out -n -r
int main(int argc, char *argv[])
{
	int nlines;
	int op_numeric, op_reverse, op_ignorecase, op_key;
	int (*op_func_comparison)(void *, void *);

	//Argument
	op_numeric = op_ignorecase = 0;
	op_reverse = 1;
	op_key = -1;
	while (--argc > 0) {
		argv++;
		if (op_key == -2)
			op_key = m_str2int(*argv, 10);
		else if (m_strcmp(*argv, "-n") == 0)
			op_numeric = 1;
		else if (m_strcmp(*argv, "-r") == 0)
			op_reverse = -1;
		else if (m_strcmp(*argv, "-f") == 0)
			op_ignorecase = 1;
		else if (m_strcmp(*argv, "-k") == 0)
			op_key = -2;
		else if (m_strncmp(*argv, "-k", 2) == 0)
			op_key = m_str2int((*argv + 2), 10);
	}
	op_key = op_key >= 0 ? op_key : 0;
	if (op_ignorecase)
		op_func_comparison = (int (*)(void*,void*)) m_strcasecmp;
	else if (op_numeric)
		op_func_comparison = (int (*)(void*,void*)) numcmp;
	else
		op_func_comparison = (int (*)(void*,void*)) m_strcmp;

	//Sort
	if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
		qsort(
			(void **)lineptr,
			0,
			nlines - 1,
			op_func_comparison,
			op_reverse,
			op_key
		);
		writelines(lineptr, nlines);
		printf("Total:%d\n", nlines);
		return 0;
	} else {
		printf("error: input too big to sort\n");
		return 1;
	}
}
Example #2
0
// Load interface settings from a file
void loadHUD(FILE *hud)
{
	char cmd[10];		// command string
	Uint8 panel = 0;	// which panel the command applies to

	while( fgets(cmd, 10, hud) != NULL )
	{
		// find out what these settings apply to, usually a specific panel
		if( m_strncmp(cmd, "minimap",10) )
			panel = 1;
		else if( m_strncmp(cmd, "infobar",10) )
			panel = 2;
		else if( m_strncmp(cmd, "player",10) )
			panel = 3;
		else if( m_strncmp(cmd, "misc",10) )
			panel = 0;
		else
			continue;

		// load the values
		if(panel == 1)
		{
			fgets(cmd, 10, hud);
			HUD.mapx = atoi(cmd);

			fgets(cmd, 10, hud);
			HUD.mapy = atoi(cmd);

			fgets(cmd, 10, hud);
			HUD.mapw = atoi(cmd);

			fgets(cmd, 10, hud);
			HUD.maph = atoi(cmd);
		}
		else if(panel == 2)
		{
			fgets(cmd, 10, hud);
			HUD.infox = atoi(cmd);

			fgets(cmd, 10, hud);
			HUD.infoy = atoi(cmd);

			fgets(cmd, 10, hud);
			HUD.infow = atoi(cmd);

			fgets(cmd, 10, hud);
			HUD.infoh = atoi(cmd);
		}
		else if(panel == 3)
		{
			fgets(cmd, 10, hud);
			HUD.playerx = atoi(cmd);

			fgets(cmd, 10, hud);
			HUD.playery = atoi(cmd);

			fgets(cmd, 10, hud);
			HUD.playerw = atoi(cmd);

			fgets(cmd, 10, hud);
			HUD.playerh = atoi(cmd);
		}
		else if(panel == 0)
		{
			fgets(cmd, 10, hud);
			aLevel = (float)atoi(cmd)/255.0;
		}
	}
	fclose(hud);
}