예제 #1
0
char *test_read_input()
{
        FILE *f;

        /* open, read and parse some invalid xml */
        f = fopen("test.invalid.xml", "r");
        mu_assert("read invalid xml", read_input(f) == 0);
        fclose(f);
        mu_assert("parse invalid xml", xml_parse(buf) == XML_STATUS_INVALID);

        /* open, read and parse some valid xml */
        f = fopen("test.xml", "r");
        mu_assert("read valid xml", read_input(f) == 0);
        fclose(f);
        mu_assert("buffer contains request", strncmp(buf,"<request>", 9) == 0);
        mu_assert("parse some valid xml", xml_parse(buf) == XML_STATUS_OK);

        /* fetch value of an element */
        char *value = xml_element("//username");
        mu_assert("perform xpath search for element", value != NULL);
        mu_assert("get value of xml element", strcmp(value, "iamauser") == 0);
        xml_free(value);

        xml_cleanup();

        return 0;
}
예제 #2
0
int btdev_cache_load(int locked)
{
	char		buf[256];
	FILE		*cfd;
	size_t		read;
	char		*next = NULL, *elem, *attrs, *attr, *value;
	BD_ADDR		bda;
	int		found = 0, eof = 0;
	struct stat	st;

	if (!devcache.file && btdev_cache_init() < 0)
		return -1;

	if (stat(devcache.file, &st) < 0) {
		return -1;
	}
	
	if (st.st_mtime == devcache.stamp) {
		if (locked && btdev_cache_lock() < 0) {
			return -1;
		}
		return 0;
	}
	
	devcache.stamp = st.st_mtime;
	
	if (btdev_cache_lock() < 0) {
		return -1;
	}

	cfd = fopen(devcache.file, "r");
	if (!cfd){
		fprintf(stderr, "Unable to open cache: %s\n", devcache.file);
		btdev_cache_unlock();
		return -1;
	}

	if (devcache.head) {
		s_list_destroy(&devcache.head);
		devcache.head = NULL;
		devcache.count = 0;
	}
	
	for (;;) {
		int	free;
		
		if (next) {
			/* we have some info in the buffer */
			free =  next - buf;
			memmove(buf, next, sizeof(buf) - free);
		} else
			free = sizeof(buf);
			
		if (!eof) {
			//printf("reading %d butes\n", free);
			read = fread(buf + sizeof(buf) - free, 1, free, cfd);
			if (!read)
				eof = 1;
		}

		next = (void*)buf;
		elem = xml_element(&next, &attrs);
		if (!elem)
			break;

		if (!found)
			if (strcmp(elem, "device-listing") == 0) {
				found = 1;
				continue;
			}

		if (strcmp(elem, "/device-listing") == 0)
			break;
		//printf("element: %s\n", elem);
		//printf("attr left: %s\n", attrs);
		// get attributes
		if (strcmp(elem, "device") == 0) {
			btdev_struct	*entry;
			entry = NULL;
			while ((attr = xml_attribute(&attrs, &value))) {
				//printf("%s = %s\n", attr, value);
				if (!entry) {
					if (strcmp(attr, "bda") == 0) {
						str2bda(&bda, value);
						entry = btdev_cache_add(&bda);
					}
				} else if (strcmp(attr, "class") == 0) {
					sscanf(value, "%x", &entry->cod);
				} else if (strcmp(attr, "name") == 0) {
					strcpy(entry->name, value);
				} else if (strcmp(attr, "key") == 0) {
					unsigned int	val;
					int		i;
					/* convert key to binary format */
					for (i = 0; sscanf(value, "%2x", &val) > 0 && i < 16; i++, value += 2) {
						entry->link_key[i] = val;
					}
					if (i)
						entry->flags |= BTDEV_KEY;
				}
			}
		}
	}
	fclose(cfd);
	if (!locked)
		btdev_cache_unlock();
	return 0;
}
예제 #3
0
void
sectioning_underscore (char *cmd)
{
  char *temp, *secname;
  int level;
  
  /* If we're not indenting the first paragraph, we shall make it behave
     like @noindent is called directly after the section heading. */
  if (! do_first_par_indent)
    cm_noindent ();

  temp = xmalloc (2 + strlen (cmd));
  temp[0] = COMMAND_PREFIX;
  strcpy (&temp[1], cmd);
  level = what_section (temp, &secname);
  level -= 2;
  if (level < 0)
    level = 0;
  free (temp);

  /* If the argument to @top is empty, we try using the one from @settitle.
     Warn if both are unusable.  */
  if (STREQ (command, "top"))
    {
      int save_input_text_offset = input_text_offset;

      get_rest_of_line (0, &temp);

      /* Due to get_rest_of_line ... */
      line_number--;

      if (strlen (temp) == 0 && (!title || strlen (title) == 0))
        warning ("Must specify a title with least one of @settitle or @top");

      input_text_offset = save_input_text_offset;
    }

  if (xml)
    {
      /* If the section appears in the toc, it means it's a real section
	 unlike majorheading, chapheading etc. */
      if (section_alist[search_sectioning (cmd)].toc == TOC_YES)
	{
	  xml_close_sections (level);
	  /* Mark the beginning of the section
	     If the next command is printindex, we will remove
	     the section and put an Index instead */
	  flush_output ();
	  xml_last_section_output_position = output_paragraph_offset;

	  get_rest_of_line (0, &temp);

          /* Use @settitle value if @top parameter is empty.  */
          if (STREQ (command, "top") && strlen(temp) == 0)
            temp = xstrdup (title ? title : "");

          /* Docbook does not support @unnumbered at all.  So we provide numbers
             that other formats use.  @appendix seems to be fine though, so we let
             Docbook handle that as usual.  */
          if (docbook && enum_marker != APPENDIX_MAGIC)
            {
              if (section_alist[search_sectioning (cmd)].num == ENUM_SECT_NO
                  && section_alist[search_sectioning (cmd)].toc == TOC_YES)
                xml_insert_element_with_attribute (xml_element (secname),
                    START, "label=\"%s\" xreflabel=\"%s\"",
                    handle_enum_increment (level, search_sectioning (cmd)),
                    text_expansion (temp));
              else
                xml_insert_element_with_attribute (xml_element (secname),
                    START, "label=\"%s\"",
                    handle_enum_increment (level, search_sectioning (cmd)));
            }
          else
            xml_insert_element (xml_element (secname), START);

	  xml_insert_element (TITLE, START);
	  xml_open_section (level, secname);
	  execute_string ("%s", temp);
	  xml_insert_element (TITLE, END);

	  free (temp);
	}
      else
        {
          if (docbook)
            {
              if (level > 0)
                xml_insert_element_with_attribute (xml_element (secname), START,
                    "renderas=\"sect%d\"", level);
              else
                xml_insert_element_with_attribute (xml_element (secname), START,
                    "renderas=\"other\"");
            }
          else
            xml_insert_element (xml_element (secname), START);

          get_rest_of_line (0, &temp);
          execute_string ("%s", temp);
          free (temp);

          xml_insert_element (xml_element (secname), END);
        }
    }
  else if (html)
    sectioning_html (level, secname);
  else
    insert_and_underscore (level, secname);
}