/* Copyright (c) 2010 MJ Rutter 
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 * 
 * 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., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 */ 

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

/* Read ASCII data part of pnm file */

/* Standard says max line length is 70, so let's be generous */
#define LINELENGTH 161

void pnmadata(FILE *pnm,int n,unsigned char *data)
{
  int i,j,d;
  char line[LINELENGTH];

  i=0;
  while(i<n){
    if (!fgets(line,LINELENGTH,pnm)) {
      fprintf(stderr,"Read error in pnmadata\n");
      exit(1);
    }

/*    fprintf(stderr,"pnmaread read line\n"); */

    j=0;
    while(i<n){
      while((line[j]==9)||(line[j]==10)||
	    (line[j]==13)||(line[j]==32)) j++;
      if (line[j]==0) break;

      if ((line[j]>='0')&&(line[j]<='9')){
	d=0;
	while((line[j]>='0')&&(line[j]<='9')) d=10*d+line[j++]-'0';
	data[i++]=d;
      }
      else{
	fprintf(stderr,"Unexpected character %c in pnmadata\n",line[j]);
	exit(1);
      }
    }
  }
}
