pcb 4.1.1
An interactive printed circuit board layout editor.

hid/common/flags.c

Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif
00004 
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 
00009 #include "global.h"
00010 #include "data.h"
00011 #include "misc.h"
00012 
00013 #include "hid.h"
00014 #include "../hidint.h"
00015 
00016 #ifdef HAVE_LIBDMALLOC
00017 #include <dmalloc.h>
00018 #endif
00019 
00020 typedef struct HID_FlagNode
00021 {
00022   struct HID_FlagNode *next;
00023   HID_Flag *flags;
00024   int n;
00025 } HID_FlagNode;
00026 
00027 HID_FlagNode *hid_flag_nodes = 0;
00028 static int n_flags = 0;
00029 static HID_Flag *all_flags = 0;
00030 
00031 void
00032 hid_register_flags (HID_Flag * a, int n)
00033 {
00034   HID_FlagNode *ha;
00035 
00036   /* printf("%d flag%s registered\n", n, n==1 ? "" : "s"); */
00037   ha = (HID_FlagNode *) malloc (sizeof (HID_FlagNode));
00038   ha->next = hid_flag_nodes;
00039   hid_flag_nodes = ha;
00040   ha->flags = a;
00041   ha->n = n;
00042   n_flags += n;
00043   if (all_flags)
00044     {
00045       free (all_flags);
00046       all_flags = 0;
00047     }
00048 }
00049 
00050 static int
00051 flag_sort (const void *va, const void *vb)
00052 {
00053   HID_Flag *a = (HID_Flag *) va;
00054   HID_Flag *b = (HID_Flag *) vb;
00055   return strcmp (a->name, b->name);
00056 }
00057 
00058 HID_Flag *
00059 hid_find_flag (const char *name)
00060 {
00061   HID_FlagNode *hf;
00062   int i, n, lower, upper;
00063 
00064   if (all_flags == 0)
00065     {
00066       n = 0;
00067       all_flags = (HID_Flag *)malloc (n_flags * sizeof (HID_Flag));
00068       for (hf = hid_flag_nodes; hf; hf = hf->next)
00069         for (i = 0; i < hf->n; i++)
00070           all_flags[n++] = hf->flags[i];
00071       qsort (all_flags, n_flags, sizeof (HID_Flag), flag_sort);
00072     }
00073 
00074   lower = -1;
00075   upper = n_flags + 1;
00076   /*printf("search flag %s\n", name); */
00077   while (lower < upper - 1)
00078     {
00079       i = (lower + upper) / 2;
00080       n = strcmp (all_flags[i].name, name);
00081       /*printf("try [%d].%s, cmp %d\n", i, all_flags[i].name, n); */
00082       if (n == 0)
00083         return all_flags + i;
00084       if (n > 0)
00085         upper = i;
00086       else
00087         lower = i;
00088     }
00089   printf ("unknown flag `%s'\n", name);
00090   return 0;
00091 }
00092 
00093 int
00094 hid_get_flag (const char *name)
00095 {
00096   static char *buf = 0;
00097   static int nbuf = 0;
00098   const char *cp;
00099   HID_Flag *f;
00100 
00101   cp = strchr (name, ',');
00102   if (cp)
00103     {
00104       int wv;
00105 
00106       if (nbuf < (cp - name + 1))
00107         {
00108           nbuf = cp - name + 10;
00109           buf = (char *)realloc (buf, nbuf);
00110         }
00111       memcpy (buf, name, cp - name);
00112       buf[cp - name] = 0;
00113       /* A number without units is just a number.  */
00114       wv = GetValueEx (cp + 1, NULL, NULL, NULL, NULL);
00115       f = hid_find_flag (buf);
00116       if (!f)
00117         return 0;
00118       return f->function (f->parm) == wv;
00119     }
00120 
00121   f = hid_find_flag (name);
00122   if (!f)
00123     return 0;
00124   return f->function (f->parm);
00125 }
00126 
00127 
00128 void
00129 hid_save_and_show_layer_ons (int *save_array)
00130 {
00131   int i;
00132   for (i = 0; i < max_copper_layer + SILK_LAYER; i++)
00133     {
00134       save_array[i] = PCB->Data->Layer[i].On;
00135       PCB->Data->Layer[i].On = 1;
00136     }
00137 }
00138 
00139 void
00140 hid_restore_layer_ons (int *save_array)
00141 {
00142   int i;
00143   for (i = 0; i < max_copper_layer + SILK_LAYER; i++)
00144     PCB->Data->Layer[i].On = save_array[i];
00145 }
00146 
00147 const char *
00148 layer_type_to_file_name (int idx, int style)
00149 {
00150   int group;
00151   int nlayers;
00152   const char *single_name;
00153 
00154   switch (idx)
00155     {
00156     case SL (SILK, TOP):
00157       return "topsilk";
00158     case SL (SILK, BOTTOM):
00159       return "bottomsilk";
00160     case SL (MASK, TOP):
00161       return "topmask";
00162     case SL (MASK, BOTTOM):
00163       return "bottommask";
00164     case SL (PDRILL, 0):
00165       return "plated-drill";
00166     case SL (UDRILL, 0):
00167       return "unplated-drill";
00168     case SL (PASTE, TOP):
00169       return "toppaste";
00170     case SL (PASTE, BOTTOM):
00171       return "bottompaste";
00172     case SL (INVISIBLE, 0):
00173       return "invisible";
00174     case SL (FAB, 0):
00175       return "fab";
00176     case SL (ASSY, TOP):
00177       return "topassembly";
00178     case SL (ASSY, BOTTOM):
00179       return "bottomassembly";
00180     default:
00181       group = GetLayerGroupNumberByNumber(idx);
00182       nlayers = PCB->LayerGroups.Number[group];
00183       single_name = PCB->Data->Layer[idx].Name;
00184       if (group == GetLayerGroupNumberBySide(TOP_SIDE))
00185         {
00186           if (style == FNS_first
00187               || (style == FNS_single
00188                   && nlayers == 2))
00189             return single_name;
00190           return "top";
00191         }
00192       else if (group == GetLayerGroupNumberBySide(BOTTOM_SIDE))
00193         {
00194           if (style == FNS_first
00195               || (style == FNS_single
00196                   && nlayers == 2))
00197             return single_name;
00198           return "bottom";
00199         }
00200       else if (nlayers == 1
00201                && (strcmp (PCB->Data->Layer[idx].Name, "route") == 0 ||
00202                    strcmp (PCB->Data->Layer[idx].Name, "outline") == 0))
00203         {
00204           return "outline";
00205         }
00206       else
00207         {
00208           static char buf[20];
00209           if (style == FNS_first
00210               || (style == FNS_single
00211                   && nlayers == 1))
00212             return single_name;
00213           sprintf (buf, "group%d", group);
00214           return buf;
00215         }
00216       break;
00217     }
00218 }
00219 
00220 const char *
00221 layer_type_to_file_name_ex (int idx, int style, const char *layer_name)
00222 {
00223   if (idx == SL (PDRILL, 0)
00224       || idx == SL (UDRILL, 0))
00225     return layer_name;
00226   else
00227     return layer_type_to_file_name (idx,  style);
00228 }