#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "lib.h"


#define MAXINDENT 30
int indentStep;
int level;

void rmnewline(char s[]){
	int n = strlen(s);
	if (s[n-1]=='\n') s[n-1]=0;
}

void newIndent(){
	if (level < MAXINDENT) level += indentStep ;
}
void unIndent(){
	if (level >= indentStep) level -= indentStep ;
}
void setIndentStep(int n){indentStep=n;}
void logMsg(FILE *fp, char *msg){
	int i;
	for (i=level; i> 0; i--) fputc(' ', fp);
	fprintf(fp,msg);
}

#define MAXPROCS 100
struct entry {char   *name;
              int    count;};
struct entry table[MAXPROCS];
int numentry;
int logging;

void loggingOn()  {
	logging=1;
	logMsg(stdout,"Logging on\n");
}
void loggingOff() {
	logging=0;
	logMsg(stdout,"Logging off\n");
}

void inc(char *id){
    struct entry *p = table;
    int n = 0; 	/* number of entries checked */

    if (!logging) return;
    while (n < numentry) {
        if (strcmp(p->name, id) == 0){
	    p->count++;
	    break;
	}
	else {
	    p++; n++;
	}
    }
    if (n == numentry){
        /* id not found	*/
        p->count=1;
        strcpy(p->name,id);
        numentry++;
    }
}

void profile(){
    struct entry *p = table;
    int n = 0;
    
    if (!logging) return;
    while (n++ < numentry) {
        printf("%-20s%d\n", p->name, p->count);
    }
}



