/* 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 <string.h>
#include <stdlib.h>

/* Read integers from pbm/pgm/ppm header, skipping comments etc.
 * MJR 10/02
 */

void pnmheader(FILE *pnm,int n,unsigned int *data)
{
  int i,j;
  char c;
  char whitespace[5]={' ',9,10,13,0};
  char digits[11]="0123456789";

  /* We now want to read n numbers... */

  for(i=0;i<n;i++){
    data[i]=0;
    do{
      j=fgetc(pnm);
      if (j==EOF) exit(1);
      c=(char)j;
      if (c=='#') { /* Swallow line */
        do{
          j=fgetc(pnm);
        } while ((j!=EOF)&&(j!='\n'));
        c=(char)j;
      }
    } while(strchr(whitespace,c));

    if(!strchr(digits,c)){
      fprintf(stderr,"Error in pnm header\n");
      exit(1);
    }
    do{
      data[i]=10*data[i]+(c-'0');
      j=fgetc(pnm);
      if (j==EOF) exit(1);
      c=(char)j;
    } while (strchr(digits,c));
  }

}
