/* This is a helper function for ParseLibrary Tree. Given a char *path, * it finds all newlib footprints in that dir and sticks them into the * library menu structure named entry. */ static int LoadNewlibFootprintsFromDir(char *libpath, char *toppath) { char olddir[MAXPATHLEN + 1]; /* The directory we start out in (cwd) */ char subdir[MAXPATHLEN + 1]; /* The directory holding footprints to load */ DIR *subdirobj; /* Interable object holding all subdir entries */ struct dirent *subdirentry; /* Individual subdir entry */ struct stat buffer; /* Buffer used in stat */ LibraryMenuType *menu = NULL; /* Pointer to PCB's library menu structure */ LibraryEntryType *entry; /* Pointer to individual menu entry */ size_t l; size_t len; int n_footprints = 0; /* Running count of footprints found in this subdir */ /* Cache old dir, then cd into subdir because stat is given relative file names. */ memset (subdir, 0, sizeof subdir); memset (olddir, 0, sizeof olddir); if (GetWorkingDirectory (olddir) == NULL) { Message (_("LoadNewlibFootprintsFromDir: Could not determine initial working directory\n")); return 0; } if (strcmp (libpath, "(local)") == 0) strcpy (subdir, "."); else strcpy (subdir, libpath); if (chdir (subdir)) { ChdirErrorMessage (subdir); return 0; } /* Determine subdir is abs path */ if (GetWorkingDirectory (subdir) == NULL) { Message (_("LoadNewlibFootprintsFromDir: Could not determine new working directory\n")); if (chdir (olddir)) ChdirErrorMessage (olddir); return 0; } /* First try opening the directory specified by path */ if ( (subdirobj = opendir (subdir)) == NULL ) { OpendirErrorMessage (subdir); if (chdir (olddir)) ChdirErrorMessage (olddir); return 0; } /* Get pointer to memory holding menu */ menu = GetLibraryMenuMemory (&Library); /* Populate menuname and path vars */ menu->Name = strdup (pcb_basename(subdir)); menu->directory = strdup (pcb_basename(toppath)); /* Now loop over files in this directory looking for files. * We ignore certain files which are not footprints. */ while ((subdirentry = readdir (subdirobj)) != NULL) { #ifdef DEBUG /* printf("... Examining file %s ... \n", subdirentry->d_name); */ #endif /* Ignore non-footprint files found in this directory * We're skipping .png and .html because those * may exist in a library tree to provide an html browsable * index of the library. */ l = strlen (subdirentry->d_name); if (!stat (subdirentry->d_name, &buffer) && S_ISREG (buffer.st_mode) && subdirentry->d_name[0] != '.' && NSTRCMP (subdirentry->d_name, "CVS") != 0 && NSTRCMP (subdirentry->d_name, "Makefile") != 0 && NSTRCMP (subdirentry->d_name, "Makefile.am") != 0 && NSTRCMP (subdirentry->d_name, "Makefile.in") != 0 && (l < 4 || NSTRCMP(subdirentry->d_name + (l - 4), ".png") != 0) && (l < 5 || NSTRCMP(subdirentry->d_name + (l - 5), ".html") != 0) && (l < 4 || NSTRCMP(subdirentry->d_name + (l - 4), ".pcb") != 0) ) { #ifdef DEBUG /* printf("... Found a footprint %s ... \n", subdirentry->d_name); */ #endif n_footprints++; entry = GetLibraryEntryMemory (menu); /* * entry->AllocatedMemory points to abs path to the footprint. * entry->ListEntry points to fp name itself. */ len = strlen(subdir) + strlen("/") + strlen(subdirentry->d_name) + 1; entry->AllocatedMemory = (char *)calloc (1, len); strcat (entry->AllocatedMemory, subdir); strcat (entry->AllocatedMemory, PCB_DIR_SEPARATOR_S); /* store pointer to start of footprint name */ entry->ListEntry = entry->AllocatedMemory + strlen (entry->AllocatedMemory); /* Now place footprint name into AllocatedMemory */ strcat (entry->AllocatedMemory, subdirentry->d_name); /* mark as directory tree (newlib) library */ entry->Template = (char *) -1; } } /* Done. Clean up, cd back into old dir, and return */ closedir (subdirobj); if (chdir (olddir)) ChdirErrorMessage (olddir); return n_footprints; }
/* This function loads the newlib footprints into the Library. * It examines all directories pointed to by Settings.LibraryTree. * In each directory specified there, it looks both in that directory, * as well as *one* level down. It calls the subfunction * LoadNewlibFootprintsFromDir to put the footprints into PCB's internal * datastructures. */ static int ParseLibraryTree (void) { char toppath[MAXPATHLEN + 1]; /* String holding abs path to top level library dir */ char working[MAXPATHLEN + 1]; /* String holding abs path to working dir */ char *libpaths; /* String holding list of library paths to search */ char *p; /* Helper string used in iteration */ DIR *dirobj; /* Iterable directory object */ struct dirent *direntry = NULL; /* Object holding individual directory entries */ struct stat buffer; /* buffer used in stat */ int n_footprints = 0; /* Running count of footprints found */ /* Initialize path, working by writing 0 into every byte. */ memset (toppath, 0, sizeof toppath); memset (working, 0, sizeof working); /* Save the current working directory as an absolute path. * This fcn writes the abs path into the memory pointed to by the input arg. */ if (GetWorkingDirectory (working) == NULL) { Message (_("ParseLibraryTree: Could not determine initial working directory\n")); return 0; } /* Additional loop to allow for multiple 'newlib' style library directories * called out in Settings.LibraryTree */ libpaths = strdup (Settings.LibraryTree); for (p = strtok (libpaths, PCB_PATH_DELIMETER); p && *p; p = strtok (NULL, PCB_PATH_DELIMETER)) { /* remove trailing path delimeter */ strncpy (toppath, p, sizeof (toppath) - 1); /* start out in the working directory in case the path is a * relative path */ if (chdir (working)) { ChdirErrorMessage (working); free (libpaths); return 0; } /* * Next change to the directory which is the top of the library tree * and extract its abs path. */ if (chdir (toppath)) { ChdirErrorMessage (toppath); continue; } if (GetWorkingDirectory (toppath) == NULL) { Message (_("ParseLibraryTree: Could not determine new working directory\n")); continue; } #ifdef DEBUG printf("In ParseLibraryTree, looking for newlib footprints inside top level directory %s ... \n", toppath); #endif /* Next read in any footprints in the top level dir */ n_footprints += LoadNewlibFootprintsFromDir("(local)", toppath); /* Then open this dir so we can loop over its contents. */ if ((dirobj = opendir (toppath)) == NULL) { OpendirErrorMessage (toppath); continue; } /* Now loop over files in this directory looking for subdirs. * For each direntry which is a valid subdirectory, * try to load newlib footprints inside it. */ while ((direntry = readdir (dirobj)) != NULL) { #ifdef DEBUG printf("In ParseLibraryTree loop examining 2nd level direntry %s ... \n", direntry->d_name); #endif /* Find subdirectories. Ignore entries beginning with "." and CVS * directories. */ if (!stat (direntry->d_name, &buffer) && S_ISDIR (buffer.st_mode) && direntry->d_name[0] != '.' && NSTRCMP (direntry->d_name, "CVS") != 0) { /* Found a valid subdirectory. Try to load footprints from it. */ n_footprints += LoadNewlibFootprintsFromDir(direntry->d_name, toppath); } } closedir (dirobj); } /* restore the original working directory */ if (chdir (working)) ChdirErrorMessage (working); #ifdef DEBUG printf("Leaving ParseLibraryTree, found %d footprints.\n", n_footprints); #endif free (libpaths); return n_footprints; }
/* This function loads the newlib footprints into the Library. * It examines all directories pointed to by Settings.LibraryTree. * It calls the subfunction LoadNewlibFootprintsFromDir to put the * footprints into PCB's internal datastructures. */ static int ParseLibraryTree (void) { char toppath[MAXPATHLEN + 1]; /* String holding abs path to top level library dir */ char working[MAXPATHLEN + 1]; /* String holding abs path to working dir */ char *libpaths; /* String holding list of library paths to search */ char *p; /* Helper string used in iteration */ int n_footprints = 0; /* Running count of footprints found */ /* Initialize path, working by writing 0 into every byte. */ memset (toppath, 0, sizeof toppath); memset (working, 0, sizeof working); /* Save the current working directory as an absolute path. * This fcn writes the abs path into the memory pointed to by the input arg. */ if (GetWorkingDirectory (working) == NULL) { Message (_("ParseLibraryTree: Could not determine initial working directory\n")); return 0; } /* Additional loop to allow for multiple 'newlib' style library directories * called out in Settings.LibraryTree */ libpaths = strdup (Settings.LibraryTree); for (p = strtok (libpaths, PCB_PATH_DELIMETER); p && *p; p = strtok (NULL, PCB_PATH_DELIMETER)) { /* remove trailing path delimeter */ strncpy (toppath, p, sizeof (toppath) - 1); /* start out in the working directory in case the path is a * relative path */ if (chdir (working)) { ChdirErrorMessage (working); free (libpaths); return 0; } /* * Next change to the directory which is the top of the library tree * and extract its abs path. */ if (chdir (toppath)) { ChdirErrorMessage (toppath); continue; } if (GetWorkingDirectory (toppath) == NULL) { Message (_("ParseLibraryTree: Could not determine new working directory\n")); continue; } #ifdef DEBUG printf("In ParseLibraryTree, looking for newlib footprints inside top level directory %s ... \n", toppath); #endif /* Next read in any footprints in the top level dir and below */ n_footprints += LoadNewlibFootprintsFromDir("(local)", toppath, *p == '/'); } /* restore the original working directory */ if (chdir (working)) ChdirErrorMessage (working); #ifdef DEBUG printf("Leaving ParseLibraryTree, found %d footprints.\n", n_footprints); #endif free (libpaths); return n_footprints; }