// sattool - visual satellite tracking and prediction tool.
// Copyright 2000 Tom Rothamel <tom-idbg@onegeek.org>
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.  

#include "sattool.h"

// This prints an epoch time into the appropriate string. The format
// string is the same one as strftime.
void strfsgptime(char *s, int max, char *fmt, double t) {
	time_t ut;
	struct tm *tm;

	ut = sgp_unix(t);
	tm = localtime(&ut);
	strftime(s, max, fmt, tm);
}

// This duplicates a string.
char *strdup(char *s) {
	char *ns;
	int l;

	l = strlen(s);
	ns = (char *) malloc(l + 2);
	strncpy(ns, s, l+1);
	return ns;
}

// This is strcat, with a reallocation of the string size.
char *stradd(char *s, char *n) {
	int l;
	
	l = strlen(s) + strlen(n) + 2;
	s = (char *) realloc(s, l);
	strncat(s, n, l);

	return s;
}	

// This removes newlines from the end of the string (if present).
void chomp(char *s) {
	int l;
	
	l = strlen(s) - 1;

	if (s[l] == '\n') s[l] = 0;
}

// This pads a line.
void pad(FILE *f, int w) {
	int i;

	for(i = 0; i < w; i++) {
		fprintf(f, "  ");
	}
}


static char *compass[] = {
        "N ",
		"NE",
		"E ",
		"SE",
		"S ",
		"SW",
		"W ",
		"NW",
		};

// This returns a compass direction for a given azimuth.
char *CompassDir(double ang) {
        int i;
        
        ang += 22.5;
        ang = fmod(ang, 360);
        ang /= 45;

        i = (int) ang;
        return compass[i];
}
