static int is_target_file(BlockDriverState *bs, char *filename, char *header) { int retval = 0; if (str_ends(filename, ".raw")) retval = 1; if (str_ends(filename, ".qcow")) retval = 1; if (str_ends(filename, ".qcow2")) retval = 1; if (str_ends(filename, ".vmdk")) retval = 1; if (retval && (header[OFFS_TYPE] != '0') && (header[OFFS_TYPE] != 'S')) { retval = 0; } dprintf("does filename %s match? %s\n", filename, retval ? "yes" : "no"); /* make sure we're not using this name again */ filename[0] = '\0'; return retval; }
int main(void) { const char *s = "ABCD1234EFGHXXXX"; const char *t = "ABCD1234EFGHXXXX"; const char *u = "AbCdEfGh"; const char *v = "aBcDeFgH"; char *x; long i; unsigned long pos; test_assert(str_chr(s, 'D') == 3); i = str_char(s, 'D', &pos); test_assert(i == 1); test_assert(pos == 3); test_assert(str_diff(s, t) == 0); test_assert(str_same(s, t)); test_assert(str_ndiff(s, t, 8) == 0); test_assert(str_nsame(s, t, 8) == 1); test_assert(str_casei_diff(u, v) == 0); test_assert(str_casei_same(u, v) == 1); test_assert(str_casei_ndiff(u, v, 8) == 0); test_assert(str_casei_nsame(u, v, 8) == 1);; test_assert(str_rchr(s, 'X') == 15); i = str_rchar(s, 'X', &pos); test_assert(i == 1); test_assert(pos == 15); test_assert(str_dup(s, &x)); test_assert(str_same(s, x) == 1); test_assert(str_nsame(s, x, 8) == 1); test_assert(str_starts(s, "ABCD") == 1); test_assert(str_starts(s, "XYZA") == 0); test_assert(str_starts(s, "1234EFGH1234EFGH1234EFGH") == 0); test_assert(str_ends(s, "XXXX") == 1); test_assert(str_ends(s, "ABCD") == 0); test_assert(str_ends(s, "GH1234EFGH123GH1234EFGH123") == 0); test_assert(str_len(s) == 16); str_toupper(x); test_assert(x[0] == 'A'); str_tolower(x); test_assert(x[0] == 'a'); return 0; }
struct install_status_t ntran_copy (struct install_item *ins) { static char src_name [INSTALL_MAX_PATHLEN]; static char dst_name [INSTALL_MAX_PATHLEN]; static char dst_tmp [INSTALL_MAX_PATHLEN]; struct install_status_t status = INSTALL_STATUS_INIT; if (!ins->src) { status.message = "source file undefined"; return status; } if (!ins->dir) { status.message = "directory undefined"; return status; } if (!ins->dst) ins->dst = ins->src; if (str_ends (ins->src, ".vlb")) { if (!libname (ins->src, src_name)) { status.message = "could not build library name"; return status; } ins->src = src_name; } if (str_ends (ins->dst, ".vlb")) { if (!libname (ins->dst, dst_name)) { status.message = "could not build library name"; return status; } ins->dst = dst_name; } if (!base_name (ins->dst, &ins->dst)) { status.message = "invalid destination path"; return status; } if (snprintf (dst_tmp, sizeof (dst_tmp), "%s/%s", ins->dir, ins->dst) < 0) { status.status = INSTALL_STATUS_ERROR; status.message = "could not format destination path"; return status; } ins->dst = dst_tmp; status.status = INSTALL_STATUS_OK; return status; }
DesktopIcon *Desktop::read_desktop_file(const char *path, const char *base, DesktopConfig *pos) { DesktopIcon *ret = NULL; if(file_test(path, FILE_TEST_IS_DIR)) { ret = new DesktopIcon(_("No Name")); ret->set_icon_type(DESKTOP_ICON_TYPE_FOLDER); /* hardcoded */ ret->set_image("folder"); /* copy label explicitly, as DesktopIcon() will only store a pointer */ ret->copy_label(base); } else { /* * try to load it as plain .desktop file by looking only at extension * TODO: MimeType can be used here too */ if(!str_ends(path, EDE_DESKTOP_DESKTOP_EXT)) return ret; DesktopFile df; char buf[PATH_MAX]; E_RETURN_VAL_IF_FAIL(df.load(path), ret); E_RETURN_VAL_IF_FAIL(df.type() != DESK_FILE_TYPE_UNKNOWN, ret); ret = new DesktopIcon(_("No Name")); ret->set_icon_type(DESKTOP_ICON_TYPE_NORMAL); if(df.name(buf, sizeof(buf))) ret->copy_label(buf); ret->set_image(df.icon(buf, sizeof(buf)) ? buf : NULL); if(df.comment(buf, sizeof(buf))) ret->set_tooltip(buf); } /* try to put random icon position in the middle of the desktop, so it is easier to notice */ int X = (rand() % (w() / 4)) + (w() / 4); int Y = (rand() % (h() / 4)) + (h() / 4); /* lookup icons locations if possible */ if(base && pos && *pos) { pos->get(base, "X", X, X); pos->get(base, "Y", Y, Y); } E_DEBUG("Setting icon '%s' (%i,%i)\n", base, X, Y); ret->position(X, Y); /* use loaded icon options */ ret->set_options(icon_opts); ret->set_path(path); return ret; }
struct install_status_t ntran_liblink (struct install_item *ins) { static char dst_tmp [INSTALL_MAX_PATHLEN]; static char src_name [INSTALL_MAX_PATHLEN]; static char src_tmp [INSTALL_MAX_PATHLEN]; struct install_status_t status = INSTALL_STATUS_INIT; if (!ins->src) { status.message = "source file undefined"; return status; } if (!ins->dir) { status.message = "directory undefined"; return status; } if (!ins->dst) { status.message = "destination file undefined"; return status; } if (str_ends (ins->src, ".vlb")) { if (!libname (ins->src, src_tmp)) { status.message = "could not build library name"; return status; } ins->src = src_tmp; if (!base_name (ins->src, &ins->src)) { status.message = "invalid source path"; return status; } memcpy (src_name, ins->src, INSTALL_MAX_PATHLEN); ins->src = src_name; } /* build name of library */ if (!base_name (ins->dst, &ins->dst)) { status.message = "invalid destination path"; return status; } if (snprintf (dst_tmp, INSTALL_MAX_PATHLEN, "%s%s", ins->dst, inst_dlib_suffix) < 0) { status.status = INSTALL_STATUS_ERROR; status.message = "could not format destination path"; return status; } ins->dst = dst_tmp; status.status = INSTALL_STATUS_OK; return status; }
bool dir_list(const char *dir, list<String> &lst, bool full_path, bool show_hidden, bool show_dots) { E_ASSERT(dir != NULL); DIR *dirp = opendir(dir); E_RETURN_VAL_IF_FAIL(dirp != NULL, false); /* make sure the list is empty */ lst.clear(); String dirstr, tmp; if(full_path) { /* resolve full name if given folder in form: './file' */ dirstr = (dir[0] == '.' && dir[1] == '\0') ? dir_current() : dir; if(!str_ends(dirstr.c_str(), E_DIR_SEPARATOR_STR)) dirstr += E_DIR_SEPARATOR_STR; } for(dirent *dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { if(!show_hidden && dp->d_name[0] == '.' && !DOT_OR_DOTDOT(dp->d_name)) continue; if(!show_dots && DOT_OR_DOTDOT(dp->d_name)) continue; if(full_path) { tmp = dirstr; tmp += dp->d_name; lst.push_back(tmp); } else { lst.push_back(dp->d_name); } } lst.sort(); closedir(dirp); return true; }
static void scan_object(EdbusConnection *conn, EdbusMessage &msg, const char *service, const char *path, ObjectTree *self) { EdbusMessage reply; msg.create_method_call(service, path, INTROSPECTABLE_INTERFACE, INTROSPECTABLE_METHOD); if(!conn->send_with_reply_and_block(msg, 1000, reply)) { E_WARNING(E_STRLOC ": Did not get reply from service bus. Skipping introspection of '%s' service (object path: '%s')\n", service, path); return; } /* reply must be single element and string, which is xml */ if(reply.size() != 1) { E_WARNING(E_STRLOC ": Expected only one element, but got '%i'\n", reply.size()); return; } EdbusMessage::const_iterator it = reply.begin(); if(!it->is_string()) { E_WARNING(E_STRLOC ": Expected string in reply, but got some junk\n"); return; } TiXmlDocument doc; char buf[128]; Fl_Tree_Item *titem; doc.Parse(it->to_string()); TiXmlNode *el = doc.FirstChild("node"); if(!el) return; for(el = el->FirstChildElement(); el; el = el->NextSibling()) { /* we have subobjects */ if(STR_CMP_VALUE(el, "node")) { const char *name = el->ToElement()->Attribute("name"); if(!name) { E_DEBUG(E_STRLOC ": <node> is expected to have 'name' attribute\n"); continue; } /* TODO: maybe use EdbusObjectPath? */ if(strcmp(path, "/") == 0) snprintf(buf, sizeof(buf), "/%s", name); else snprintf(buf, sizeof(buf), "%s/%s", path, name); titem = self->add(buf); self->close(titem); titem->usericon(&image_package); /* recurse */ scan_object(conn, msg, service, buf, self); } else if(STR_CMP_VALUE(el, "interface")) { /* full interface: get methods and properties */ const char *interface_name, *name = el->ToElement()->Attribute("name"); /* remember it for Entity */ interface_name = name; if(!name) { E_DEBUG(E_STRLOC ": <interface> is expected to have 'name' attribute\n"); continue; } /* append interface to tree */ snprintf(buf, sizeof(buf), "%s/%s", path, name); titem = self->add(buf); self->close(titem); titem->usericon(&image_interface); /* append methods, signals and properties */ TiXmlNode *sel; char buf2[256]; Fl_Pixmap *icon; EntityType et; for(sel = el->FirstChildElement(); sel; sel = sel->NextSibling()) { if(STR_CMP_VALUE(sel, "method")) { icon = &image_method; et = ENTITY_METHOD; } else if(STR_CMP_VALUE(sel, "signal")) { icon = &image_signal; et = ENTITY_SIGNAL; } else if(STR_CMP_VALUE(sel, "property")) { icon = &image_property; et = ENTITY_PROPERTY; } else { E_WARNING(E_STRLOC ": Got unknown node '%s'. Skipping...\n", sel->Value()); continue; } /* everything else are common elements between different types */ name = sel->ToElement()->Attribute("name"); snprintf(buf2, sizeof(buf2), "%s/%s", buf, name); titem = self->add(buf2); titem->usericon(icon); self->close(titem); /* fill our metadata */ Entity *en = new Entity(); en->set_type(et); en->set_name(name); en->set_interface(interface_name); en->set_path(path); /* TODO: this doesn't have to be copied */ en->set_service(service); if(et == ENTITY_PROPERTY) { const char *argstype, *argsname, *argsaccess; argstype = sel->ToElement()->Attribute("type"); argsname = sel->ToElement()->Attribute("name"); argsaccess = sel->ToElement()->Attribute("access"); en->append_arg(argsname, argstype, DIRECTION_NONE, argsaccess); } else { TiXmlNode *argsnode; for(argsnode = sel->FirstChildElement(); argsnode; argsnode = argsnode->NextSibling()) { if(STR_CMP_VALUE(argsnode, "arg")) { /* arguments */ const char *argstype, *argsname, *argsdirection; ArgDirection dir = DIRECTION_NONE; argstype = argsnode->ToElement()->Attribute("type"); if(!argstype) continue; argsname = argsnode->ToElement()->Attribute("name"); if(!argsname) continue; /* it is fine to not have direction, which means it is only a method */ argsdirection = argsnode->ToElement()->Attribute("direction"); if(argsdirection) { if(STR_CMP(argsdirection, "in")) dir = DIRECTION_IN; else if(STR_CMP(argsdirection, "out")) dir = DIRECTION_OUT; } en->append_arg(argsname, argstype, dir); } else if(STR_CMP_VALUE(argsnode, "annotation")) { const char *annoname, *argsval; annoname = argsnode->ToElement()->Attribute("name"); /* allow whatever annotation name ending with '.DocString' to be supported (e.g. org.gtk.GDBus.DocString or org.equinoxproject.DBus.DocString) */ if(!str_ends(annoname, ".DocString")) { E_WARNING(E_STRLOC ": We are supporting now only DocString annotations. Skipping '%s'...\n", annoname); continue; } argsval = argsnode->ToElement()->Attribute("value"); if(argsval) en->set_doc(argsval); } } } /* put it inside our tree so we can manage it */ self->append_entity(en); /* add also inside Fl_Tree_Item */ titem->user_data(en); } } } }
char s8[] = "some with spaces "; UT_VERIFY( STR_EQUAL("some with spaces", str_trimright(s8)) ); char s9[] = "some with spaces |"; UT_VERIFY( STR_EQUAL("some with spaces |", str_trimright(s9)) ); char s10[] = "some without spaces"; UT_VERIFY( STR_EQUAL("some without spaces", str_trimright(s10)) ); char s11[] = "\t\t\t\t\tsome\tstring \t\t"; UT_VERIFY( STR_EQUAL("some\tstring", str_trim(s11)) ); char s12[] = "\nsss\n"; UT_VERIFY( STR_EQUAL("sss", str_trim(s12)) ); UT_VERIFY( str_ends("sample.png", ".png") == true ); UT_VERIFY( str_ends("sample", "foo") == false ); UT_VERIFY( str_ends("sample", "ple") == true ); UT_VERIFY( str_ends("le", "a long that should fail") == false ); UT_VERIFY( str_ends("not-empty", "") == false ); UT_VERIFY( str_ends("sample/path", "/") == false ); UT_VERIFY( str_ends("sample/path/", "/") == true ); UT_VERIFY( str_ends("sample/path////", "/") == true ); UT_VERIFY( str_ends("sample/path////aa", "/aa") == true ); UT_VERIFY( str_ends("/aa", "/aa") == true ); UT_VERIFY( str_ends("/aa", "/aaaaa") == false); } UT_FUNC(stringtok, "Test stringtok") { list<String> ls;
static struct install_status_t ntran_copy (struct install_item *item) { static char src_name [INSTALL_MAX_PATHLEN]; static char dst_name [INSTALL_MAX_PATHLEN]; static char dst_tmp [INSTALL_MAX_PATHLEN]; struct install_status_t status = INSTALL_STATUS_INIT; assert (item != NULL); if (item->src == NULL) { install_status_assign (&status, INSTALL_STATUS_ERROR, "source file undefined"); return status; } if (item->dir == NULL) { install_status_assign (&status, INSTALL_STATUS_ERROR, "directory undefined"); return status; } if (item->dst == NULL) item->dst = item->src; /* If source or destination file are virtual library files, build * real library names. */ if (str_ends (item->src, ".vlb") != 0) { if (libname (item->src, src_name) == 0) { install_status_assign (&status, INSTALL_STATUS_ERROR, "could not build library name"); return status; } item->src = src_name; } if (str_ends (item->dst, ".vlb") != 0) { if (libname (item->dst, dst_name) == 0) { install_status_assign (&status, INSTALL_STATUS_ERROR, "could not build library name"); return status; } item->dst = dst_name; } assert (item->src != NULL); assert (item->dst != NULL); if (base_name (item->dst, &item->dst) == 0) { install_status_assign (&status, INSTALL_STATUS_ERROR, "invalid destination path"); return status; } assert (item->dst != NULL); /* Build file names, possibly prefixed with fake root */ if (inst_fake_root != NULL) { if (snprintf (dst_tmp, sizeof (dst_tmp), "%s/%s/%s", inst_fake_root, item->dir, item->dst) < 0) { install_status_assign (&status, INSTALL_STATUS_ERROR, "could not format destination path"); return status; } } else { if (snprintf (dst_tmp, sizeof (dst_tmp), "%s/%s", item->dir, item->dst) < 0) { install_status_assign (&status, INSTALL_STATUS_ERROR, "could not format destination path"); return status; } } item->dst = dst_tmp; install_status_assign (&status, INSTALL_STATUS_OK, NULL); return status; }
static struct install_status_t ntran_liblink (struct install_item *item) { static char dir_name [INSTALL_MAX_PATHLEN]; static char dst_tmp [INSTALL_MAX_PATHLEN]; static char src_name [INSTALL_MAX_PATHLEN]; static char src_tmp [INSTALL_MAX_PATHLEN]; struct install_status_t status = INSTALL_STATUS_INIT; assert (item != NULL); if (item->src == NULL) { install_status_assign (&status, INSTALL_STATUS_ERROR, "source file undefined"); return status; } if (item->dir == NULL) { install_status_assign (&status, INSTALL_STATUS_ERROR, "directory undefined"); return status; } if (item->dst == NULL) { install_status_assign (&status, INSTALL_STATUS_ERROR, "destination file undefined"); return status; } /* Modify path if fake root was specified */ if (inst_fake_root != NULL) { if (snprintf (dir_name, INSTALL_MAX_PATHLEN, "%s/%s", inst_fake_root, item->dir) < 0) { install_status_assign (&status, INSTALL_STATUS_ERROR, "could not format destination path"); return status; } item->dir = dir_name; } /* If source or destination file are virtual library files, build * real library names. */ if (str_ends (item->src, ".vlb") != 0) { if (libname (item->src, src_tmp) == 0) { install_status_assign (&status, INSTALL_STATUS_ERROR, "could not build library name"); return status; } item->src = src_tmp; if (base_name (item->src, &item->src) == 0) { install_status_assign (&status, INSTALL_STATUS_ERROR, "invalid source path"); return status; } memcpy (src_name, item->src, INSTALL_MAX_PATHLEN); item->src = src_name; } /* Build library name */ if (base_name (item->dst, &item->dst) == 0) { install_status_assign (&status, INSTALL_STATUS_ERROR, "invalid destination path"); return status; } assert (item->dst != NULL); if (snprintf (dst_tmp, INSTALL_MAX_PATHLEN, "%s%s", item->dst, inst_dlib_suffix) < 0) { install_status_assign (&status, INSTALL_STATUS_ERROR, "could not format destination path"); return status; } item->dst = dst_tmp; install_status_assign (&status, INSTALL_STATUS_OK, NULL); return status; }