/*
 * PCB mask modification program
 * Copyright (C) 2005 Darrell Harmon
 *
 *  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 <stdio.h>
#include <string.h>

strmatch(char string[], char match[]){
  int i,j;
  for(i=0; string[i]!='\0'; i++){
    j = 0;
    while((string[i+j]==match[j])&&(string[i+j]!='\0')){
      if(match[++j]=='\0')
	return 1;
    }
  }
  return 0;
}

int findchar(char string[], char search){
  int i;
  for(i=0; string[i]!='\0'; i++){
    if(string[i]==search)
      return i;
  }
  return 0;
}

/*
* Format of pad line:  
* Pad[-17500 -13500 -17500 -7000 2000 1000 3000 "1" "1" 0x00000180]
* X1 Y1 X2 Y2 Width polyclear mask ....
*/

void padmod(char string[], int clear){
  char buf[200];
  int i; 
  int x1, x2, y1, y2, width, pclear, mask;
  i = 1 + findchar(string, '[');
  strncpy(buf, string, i);
  sscanf(string+i, "%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &width, &pclear);
  mask = width + clear*200;
  sprintf(buf+i, "%d %d %d %d %d %d %d ", x1, y1, x2, y2, width, pclear, mask);
  i = findchar(string, '"');
  strcat(buf, string+i);
  strcpy(string,buf);  
  return;
}

/* Formats of lines to edit:

 * Pin[0 20000 6000 3000 6600 3000 "3" "3" 0xc0400001]
 * X Y DIA DRILL MASK ...
 */

void pinmod(char string[], int clear){
  char buf[200];
  int i; 
  int x, y, dia, drill, mask, something;
  i = 1 + findchar(string, '[');
  strncpy(buf, string, i);
  sscanf(string+i, "%d %d %d %d %d %d", &x, &y, &dia, &drill, &mask, &something);
  mask = dia + clear*200;
  sprintf(buf+i, "%d %d %d %d %d %d", x, y ,dia, drill, mask, something);
  i = findchar(string, '"');
  strcat(buf, string+i);
  strcpy(string,buf);  
  return;
}

main(){
  char buf1[200], buf2[200];
  int clearance, i;
  FILE* in;
  FILE* out;
  printf("PCB mask modifier by Darrell Harmon\n\n");
  printf("name of source PCB file: ");
  scanf("%s", buf1);
  printf("\nname output of file: ");
  scanf("%s", buf2);
  printf("\ndesired mask clearance in mils: ");
  scanf("%d", &clearance);
  if ( !(in=fopen(buf1, "r")) ){
    printf("couldn't open input\n");
    return;
  }
  if(!(out=fopen(buf2, "w"))){
     printf("couldn't open input\n");
     return;
  }
  while(!feof(in)){
    /* Read a line */
    for(i = 0; i < 199; i++){
      if(!(buf1[i]=fgetc(in))){
	i--;
	break;
      }
      if(buf1[i]=='\n')
	break;
    }
    buf1[++i] = '\0';
    if(strmatch(buf1, "Pad"))
      padmod(buf1, clearance);      
    else if(strmatch(buf1, "Pin"))
      pinmod(buf1, clearance);
    fprintf(out, "%s", buf1);
  }
  fclose(in);
  fclose(out);
  printf("done processing file, exiting\n");
  return;
}

