Logo Search packages:      
Sourcecode: libgphoto2 version File versions

int gp_port_info_list_lookup_path ( GPPortInfoList list,
const char *  path 
)

Lookup a specific path in the list.

Parameters:
list a GPPortInfoList
path a path
Looks for an entry in the list with the supplied path. If no exact match can be found, a regex search will be performed in the hope some driver claimed ports like "serial:*".

Returns:
The index of the entry or a gphoto2 error code

Definition at line 339 of file gphoto2-port-info-list.c.

References CHECK_NULL, _GPPortInfoList::count, GP_ERROR_UNKNOWN_PORT, gp_log(), GP_LOG_DEBUG, GP_LOG_ERROR, gp_port_info_list_append(), _GPPortInfoList::info, _GPPortInfo::name, and _GPPortInfo::path.

Referenced by gp_camera_init().

{
      int i, result, generic;
      regex_t pattern;
#ifdef HAVE_GNU_REGEX
      const char *rv;
#else
      regmatch_t match;
#endif

      CHECK_NULL (list && path);

      gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list", _("Looking for "
            "path '%s' (%i entries available)..."), path, list->count);

      /* Exact match? */
      for (generic = i = 0; i < list->count; i++)
            if (!strlen (list->info[i].name))
                  generic++;
            else if (!strcmp (list->info[i].path, path))
                  return (i - generic);

      /* Regex match? */
      gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list",
            _("Starting regex search for '%s'..."), path);
      for (i = 0; i < list->count; i++) {
            GPPortInfo newinfo;

            if (strlen (list->info[i].name))
                  continue;

            gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list",
                  _("Trying '%s'..."), list->info[i].path);

            /* Compile the pattern */
#ifdef HAVE_GNU_REGEX
            memset (&pattern, 0, sizeof (pattern));
            rv = re_compile_pattern (list->info[i].path,
                               strlen (list->info[i].path), &pattern);
            if (rv) {
                  gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list",
                        "%s", rv);
                  continue;
            }
#else
            result = regcomp (&pattern, list->info[i].path, REG_ICASE);
            if (result) {
                  char buf[1024];
                  if (regerror (result, &pattern, buf, sizeof (buf)))
                        gp_log (GP_LOG_ERROR, "gphoto2-port-info-list",
                              "%s", buf);
                  else
                        gp_log (GP_LOG_ERROR, "gphoto2-port-info-list",
                              _("regcomp failed"));
                  return (GP_ERROR_UNKNOWN_PORT);
            }
#endif

            /* Try to match */
#ifdef HAVE_GNU_REGEX
            result = re_match (&pattern, path, strlen (path), 0, NULL);
            regfree (&pattern);
            if (result < 0) {
                  gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list",
                        _("re_match failed (%i)"), result);
                  continue;
            }
#else
            result = regexec (&pattern, path, 1, &match, 0);
            regfree (&pattern);
            if (result) {
                  gp_log (GP_LOG_DEBUG, "gphoto2-port-info-list",
                        _("regexec failed"));
                  continue;
            }
#endif
            memcpy (&newinfo, &list->info[i], sizeof(newinfo));
            strncpy (newinfo.path, path, sizeof (newinfo.path));
            strncpy (newinfo.name, _("Generic Port"), sizeof (newinfo.name));
            CR (result = gp_port_info_list_append (list, newinfo));
            return result;
      }

      return (GP_ERROR_UNKNOWN_PORT);
}


Generated by  Doxygen 1.6.0   Back to index