
#import "StandardInfo.h"

@implementation StandardInfo

- initForAppUsingNib:(const char *)ptrNibName
{
#define  STR_MAXLEN 1024
    char                tmpString[STR_MAXLEN + 1],
                        numString[16];
    NXStream           *infoStream;
    char               *curPos,
                       *curPosSave;
    int                 lineLen;
    int                 nextchar;	/* has to be "int" so we can check
					 * EOF */
    const char          authorFlag[] = "Author";
    const int           authorFlen = 6;
    const char          versionFlag[] = "Version";
    const int           versionFlen = 7;
    const char          emailFlag[] = "Email";
    const int           emailFlen = 5;
    const char          dateFlag[] = "YYYYMMDD";
    const int           dateFlen = 8;
    const int           dayOfYearMap[12] = {
					    0, 31, 59, 90,
					    120, 151, 181, 212,
					    243, 273, 304, 334};

    infoNibName = ptrNibName;
    strcpy(tmpString,[[NXBundle mainBundle] directory]);
    strcat(tmpString, "/StandardInfo");
    infoStream = NXMapFile(tmpString, NX_READONLY);
    if (infoStream == NULL) {
    /* should do some kind of error processing here... */
        printf("%s: StandardInfo processing unable to find %s\n",
	       [NXApp appName], tmpString);
	return self;
    }
    do {
	lineLen = 0;
	curPos = tmpString;
	while (((nextchar = NXGetc(infoStream)) != EOF)) {
	    if (nextchar == '\n')
		break;
	    if (lineLen < STR_MAXLEN) {
		*(curPos++) = nextchar;
		lineLen++;
	    }
	}
	*curPos = '\0';

	if (tmpString[0] == '#')
	    continue;

	if (lineLen > 8) {
	/* -- check for Author name info -- */
	    if (!strncmp(tmpString, authorFlag, authorFlen)) {
		curPosSave = curPos = &tmpString[emailFlen];
		while ((*curPos == ' ') || (*curPos == '\t'))
		    curPos++;
		if (curPos != curPosSave) {
		    strcpy(authorNameStr, curPos);
		    continue;
		}
	    }
	/* -- check for Version info -- */
	    if (!strncmp(tmpString, versionFlag, versionFlen)) {
		curPosSave = curPos = &tmpString[versionFlen];
		while ((*curPos == ' ') || (*curPos == '\t'))
		    curPos++;
		if (curPos != curPosSave) {
		    strcpy(versionStr, "Version ");
		    strcat(versionStr, curPos);
		    continue;
		}
	    }
	/* -- check for Email contact info -- */
	    if (!strncmp(tmpString, emailFlag, emailFlen)) {
		curPosSave = curPos = &tmpString[emailFlen];
		while ((*curPos == ' ') || (*curPos == '\t'))
		    curPos++;
		if (curPos != curPosSave) {
		    strcpy(emailContactStr, curPos);
		    continue;
		}
	    }
	/* -- check for Date info, as YYYY/MM/DD -- */
	    if (!strncmp(tmpString, dateFlag, dateFlen)) {
		curPosSave = curPos = &tmpString[dateFlen];
		while ((*curPos == ' ') || (*curPos == '\t'))
		    curPos++;
		if (curPos != curPosSave) {
		    strncpy(numString, "\0\0\0\0\0\0", 6);
		    if (NXIsDigit(*curPos)) numString[0] = *(curPos++);
		    if (NXIsDigit(*curPos)) numString[1] = *(curPos++);
		    if (NXIsDigit(*curPos)) numString[2] = *(curPos++);
		    if (NXIsDigit(*curPos)) numString[3] = *(curPos++);
		    dateValue.tm_year = strtol(numString, (char **)NULL, 10);
		    if (dateValue.tm_year < 70)   dateValue.tm_year += 100;
		    if (dateValue.tm_year > 1900) dateValue.tm_year -= 1900;
		    if ((*curPos == '/') || (*curPos == '-'))
			curPos++;

		    strncpy(numString, "\0\0\0\0\0\0", 6);
		    if (NXIsDigit(*curPos)) numString[0] = *(curPos++);
		    if (NXIsDigit(*curPos)) numString[1] = *(curPos++);
		    dateValue.tm_mon = strtol(numString, (char **)NULL, 10) - 1;
		    if ((*curPos == '/') || (*curPos == '-'))
			curPos++;

		    strncpy(numString, "\0\0\0\0\0\0", 6);
		    if (NXIsDigit(*curPos)) numString[0] = *(curPos++);
		    if (NXIsDigit(*curPos)) numString[1] = *(curPos++);
		    dateValue.tm_mday = strtol(numString, (char **)NULL, 10);

		    dateValue.tm_hour = 12;
		    dateValue.tm_min = dateValue.tm_sec = 0;
		    dateValue.tm_isdst = -1;
		    dateValue.tm_yday = dayOfYearMap[dateValue.tm_mon]
		      + dateValue.tm_mday;
		    if ((dateValue.tm_mon > 1) && ((dateValue.tm_year % 4) == 0)) {
			if ((dateValue.tm_year % 4) != 0)
			    dateValue.tm_yday++;
		    }
		    dateValue.tm_wday = 0;

		    strftime(dateStr, DATESTR_LEN, "%b %d, %Y", &dateValue);
		    continue;
		}
	    }
	}
    } while (nextchar != EOF);

    NXCloseMemory(infoStream, NX_FREEBUFFER);
    return self;
}

- showInfoPanel:sender
{
    if (infoNibName) {
	if (!infoPanel) {
	    [NXApp loadNibSection:infoNibName owner:self];
	    [dateFieldID setStringValueNoCopy:dateStr];
	    [versionFieldID setStringValueNoCopy:versionStr];
	    [emailFieldID setStringValueNoCopy:emailContactStr];
	}
	[infoPanel makeKeyAndOrderFront:self];
    } else {
	NXRunAlertPanel(NULL,
			"Standard Info object not initialized correctly.",
			NULL, NULL, NULL);
    }

    return self;
}

- (const char *)infoVersion
{
    return versionStr;
}

- (const char *)infoVersionDate
{
    return dateStr;
}

- (const char *)infoAuthorName
{
    return authorNameStr;
}

- (const char *)infoEmailContact
{
    return emailContactStr;
}


@end
