static int LoadNewlibFootprintsFromDir(const char *subdir, const char *toppath, int is_root) { LibraryMenuTypePtr menu = NULL; /* Pointer to PCB's library menu structure */ list_st_t l; list_dir_t *d, *nextd; char working_[MAXPATHLEN + 1]; char *working; /* String holding abs path to working dir */ int menuidx; sprintf(working_, "%s%c%s", toppath, PCB_DIR_SEPARATOR_C, subdir); resolve_path(working_, &working); /* Get pointer to memory holding menu */ menu = GetLibraryMenuMemory (&Library, &menuidx); /* Populate menuname and path vars */ menu->Name = strdup (pcb_basename(subdir)); menu->directory = strdup (pcb_basename(toppath)); l.menu = menu; l.subdirs = NULL; l.children = 0; pcb_fp_list(working, 0, list_cb, &l, is_root); /* now recurse to each subdirectory mapped in the previous call; by now we don't care if menu is ruined by the realloc() in GetLibraryMenuMemory() */ for(d = l.subdirs; d != NULL; d = nextd) { l.children+=LoadNewlibFootprintsFromDir(d->subdir, d->parent, 0); nextd = d->next; free(d->subdir); free(d->parent); free(d); } if (l.children == 0) { DeleteLibraryMenuMemory (&Library, menuidx); } free(working); return l.children; }
/* 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; }