示例#1
0
TEST(corestr,strreplacechr) 
{
   std::string value = "String for doing replaces";
   strreplacechr(value,'a','A');
   strreplacechr(value,'e','E');
   strreplacechr(value,'i','I');
   strreplacechr(value,'o','O');
   EXPECT_STREQ("StrIng fOr dOIng rEplAcEs", value.c_str());
}
示例#2
0
static bool find_include_file(std::string &srcincpath, int srcrootlen, int dstrootlen, std::string &srcfile, std::string &dstfile, std::string &filename)
{
	// iterate over include paths and find the file
	for (include_path *curpath = incpaths; curpath != nullptr; curpath = curpath->next)
	{
		// a '.' include path is specially treated
		if (curpath->path.compare(".") == 0)
			srcincpath.assign(srcfile.substr(0, srcfile.find_last_of(PATH_SEPARATOR[0])));
		else
			srcincpath.assign(srcfile.substr(0, srcrootlen + 1)).append(curpath->path);

		// append the filename piecemeal to account for directories
		int lastsepindex = 0;
		int sepindex;
		while ((sepindex = filename.find_first_of('/', lastsepindex)) != -1)
		{
			// handle .. by removing a chunk from the incpath
			std::string pathpart(filename, lastsepindex, sepindex - lastsepindex);
			if (pathpart.compare("..")==0)
			{
				sepindex = srcincpath.find_last_of(PATH_SEPARATOR[0]);
				if (sepindex != -1)
					srcincpath.substr(0, sepindex);
			}

			// otherwise, append a path separator and the pathpart
			else
				srcincpath.append(PATH_SEPARATOR).append(pathpart);

			// advance past the previous index
			lastsepindex = sepindex + 1;
		}

		// now append the filename
		srcincpath.append(PATH_SEPARATOR).append(filename.substr(lastsepindex, -1));

		// see if we can open it
		util::core_file::ptr testfile;
		if (util::core_file::open(srcincpath, OPEN_FLAG_READ, testfile) == osd_file::error::NONE)
		{
			// close the file
			testfile.reset();

			// find the longest matching directory substring between the include and source file
			lastsepindex = 0;
			while ((sepindex = srcincpath.find_first_of(PATH_SEPARATOR[0], lastsepindex)) != -1)
			{
				// get substrings up to the current directory
				std::string tempfile(srcfile, 0, sepindex);
				std::string tempinc(srcincpath, 0, sepindex);

				// if we don't match, stop
				if (tempfile.compare(tempinc)!=0)
					break;
				lastsepindex = sepindex + 1;
			}

			// chop off the common parts of the paths
			std::string tempfile(srcfile, lastsepindex, -1);
			srcincpath = srcincpath.substr(lastsepindex, -1);
			strreplacechr(srcincpath, PATH_SEPARATOR[0], '/');

			// for each directory left in the filename, we need to prepend a "../"
			while ((sepindex = tempfile.find_first_of(PATH_SEPARATOR[0])) != -1)
			{
				tempfile.substr(sepindex + 1, -1);
				srcincpath.insert(0, "../");
			}
			srcincpath.append(".html");

			// free the strings and return the include path
			return true;
		}
	}
	return false;
}
示例#3
0
static std::string &normalized_subpath(std::string &dest, std::string &path, int start)
{
	strreplacechr(dest.assign(path.substr(start, -1)),PATH_SEPARATOR[0], '/');
	return dest;
}
示例#4
0
int main(int argc, char *argv[])
{
	// loop over arguments
	include_path **incpathhead = &incpaths;
	std::string srcdir, dstdir, tempfilename, tempheader, tempfooter;
	int unadorned = 0;
	for (int argnum = 1; argnum < argc; argnum++)
	{
		char *arg = argv[argnum];

		// include path?
		if (arg[0] == '-' && arg[1] == 'I')
		{
			*incpathhead = new include_path;
			if (*incpathhead != nullptr)
			{
				(*incpathhead)->next = nullptr;
				strreplacechr((*incpathhead)->path.assign(&arg[2]), '/', PATH_SEPARATOR[0]);
				incpathhead = &(*incpathhead)->next;
			}
		}

		// other parameter
		else if (arg[0] != '-' && unadorned == 0)
		{
			srcdir.assign(arg);
			unadorned++;
		}
		else if (arg[0] != '-' && unadorned == 1)
		{
			dstdir.assign(arg);
			unadorned++;
		}
		else if (arg[0] != '-' && unadorned == 2)
		{
			tempfilename.assign(arg);
			unadorned++;
		}
		else
			usage(argv[0]);
	}

	// make sure we got 3 parameters
	if (srcdir.length() == 0 || dstdir.length() == 0 || tempfilename.length() == 0)
		usage(argv[0]);

	// read the template file into an std::string
	uint32_t bufsize;
	void *buffer;
	if (util::core_file::load(tempfilename.c_str(), &buffer, bufsize) == osd_file::error::NONE)
	{
		tempheader.assign((const char *)buffer, bufsize);
		free(buffer);
	}

	// verify the template
	if (tempheader.length() == 0)
	{
		fprintf(stderr, "Unable to read template file\n");
		return 1;
	}
	int result = tempheader.find("<!--CONTENT-->");
	if (result == -1)
	{
		fprintf(stderr, "Template is missing a <!--CONTENT--> marker\n");
		return 1;
	}
	tempfooter.assign(tempheader).substr(result + 14, -1);
	tempheader.substr(0, result);

	// recurse over subdirectories
	return recurse_dir(srcdir.length(), dstdir.length(), srcdir, dstdir, tempheader, tempfooter);
}
示例#5
0
void
scan_printers_do(void)
{
    int num_dests, i, j;
    CUPSDest *dests;
    gchar *prn_id, *prn_moreinfo;

    g_free(printer_list);
    g_free(printer_icons);

    if (!cups_init) {
        init_cups();
        
        printer_icons = g_strdup("");
        printer_list = g_strdup(_("[Printers]\n"
                                "No suitable CUPS library found="));
        return;
    }

    /* remove old devices from global device table */
    moreinfo_del_with_prefix("DEV:PRN");

    num_dests = cups_dests_get(&dests);
    if (num_dests > 0) {
	printer_list = g_strdup_printf(_("[Printers (CUPS)]\n"));
        printer_icons = g_strdup("");
	for (i = 0; i < num_dests; i++) {
	    GHashTable *options;
	    
	    options = g_hash_table_new(g_str_hash, g_str_equal);
	    
	    for (j = 0; j < dests[i].num_options; j++) {
	      g_hash_table_insert(options,
	                          g_strdup(dests[i].options[j].name),
	                          g_strdup(dests[i].options[j].value));
	    }
	
            prn_id = g_strdup_printf("PRN%d", i);
	
	    printer_list = h_strdup_cprintf("\n$%s$%s=%s\n",
					    printer_list,
					    prn_id,						
					    dests[i].name,
					    dests[i].is_default ? "<i>Default</i>" : "");
            printer_icons = h_strdup_cprintf("\nIcon$%s$%s=printer.png",
                                             printer_icons,
                                             prn_id,
                                             dests[i].name);

            prn_moreinfo = g_strdup("");
            for (j = 0; j < G_N_ELEMENTS(cups_fields); j++) {
              if (!cups_fields[j].name) {
                prn_moreinfo = h_strdup_cprintf("[%s]\n",
                                                prn_moreinfo,
                                                cups_fields[j].key);
              } else {
                gchar *temp;
                
                temp = g_hash_table_lookup(options, cups_fields[j].key);
                
                if (cups_fields[j].callback) {
                  temp = cups_fields[j].callback(temp);
                } else {
                  if (temp) {
                    /* FIXME Do proper escaping */
                    temp = g_strdup(strreplacechr(temp, "&=", ' '));
                  } else {
                    temp = g_strdup(_("Unknown"));
                  }
                }
                
                prn_moreinfo = h_strdup_cprintf("%s=%s\n",
                                                prn_moreinfo,
                                                cups_fields[j].name,
                                                temp);
                
                g_free(temp);
              }
            }
            
            moreinfo_add_with_prefix("DEV", prn_id, prn_moreinfo);
            g_free(prn_id);
            g_hash_table_destroy(options);
	}
	
	cups_dests_free(num_dests, dests);
    } else {
	printer_list = g_strdup(_("[Printers]\n"
	                        "No printers found=\n"));
    }
}
示例#6
0
void __scan_ide_devices(void)
{
    FILE *proc_ide;
    gchar *device, iface, *model, *media, *pgeometry = NULL, *lgeometry = NULL;
    gint n = 0, i = 0, cache, nn = 0;
    gchar *capab = NULL, *speed = NULL, *driver = NULL, *ide_storage_list;

    /* remove old devices from global device table */
    moreinfo_del_with_prefix("DEV:IDE");

    ide_storage_list = g_strdup(_("\n[IDE Disks]\n"));

    iface = 'a';
    for (i = 0; i <= 16; i++) {
	device = g_strdup_printf("/proc/ide/hd%c/model", iface);
	if (g_file_test(device, G_FILE_TEST_EXISTS)) {
	    gchar buf[128];

	    cache = 0;

	    proc_ide = fopen(device, "r");
	    if (!proc_ide)
	        continue;

	    (void) fgets(buf, 128, proc_ide);
	    fclose(proc_ide);

	    buf[strlen(buf) - 1] = 0;

	    model = g_strdup(buf);

	    g_free(device);

	    device = g_strdup_printf("/proc/ide/hd%c/media", iface);
	    proc_ide = fopen(device, "r");
	    if (!proc_ide) {
	        free(model);
	        continue;
            }

	    (void) fgets(buf, 128, proc_ide);
	    fclose(proc_ide);
	    buf[strlen(buf) - 1] = 0;

	    media = g_strdup(buf);
	    if (g_str_equal(media, "cdrom")) {
		/* obtain cd-rom drive information from cdrecord */
		GTimer *timer;
		gchar *tmp = g_strdup_printf("cdrecord dev=/dev/hd%c -prcap 2>/dev/stdout", iface);
		FILE *prcap;

		if ((prcap = popen(tmp, "r"))) {
		    /* we need a timeout so cdrecord does not try to get information on cd drives
		       with inserted media, which is not possible currently. half second should be
		       enough. */
		    timer = g_timer_new();
		    g_timer_start(timer);

		    while (fgets(buf, 128, prcap)
			   && g_timer_elapsed(timer, NULL) < 0.5) {
			if (g_str_has_prefix(buf, "  Does")) {
			    if (g_str_has_suffix(buf, "media\n")
				&& !strstr(buf, "speed")) {
				gchar *media_type = g_strstrip(strstr(buf, "Does "));
				gchar **ttmp = g_strsplit(media_type, " ", 0);

				capab = h_strdup_cprintf("\nCan %s#%d=%s\n", capab, ttmp[1], ++nn, ttmp[2]);

				g_strfreev(ttmp);
			    } else if (strstr(buf, "Buffer-Underrun-Free")) {
				capab =
				    h_strdup_cprintf
				    ("\nSupports BurnProof=%s\n", capab, strstr(buf, "Does not") ? "No" : "Yes");
			    } else if (strstr(buf, "multi-session")) {
				capab =
				    h_strdup_cprintf
				    ("\nCan read multi-session CDs=%s\n",
				     capab, strstr(buf, "Does not") ? "No" : "Yes");
			    } else if (strstr(buf, "audio CDs")) {
				capab =
				    h_strdup_cprintf
				    ("\nCan play audio CDs=%s\n", capab, strstr(buf, "Does not") ? "No" : "Yes");
			    } else if (strstr(buf, "PREVENT/ALLOW")) {
				capab =
				    h_strdup_cprintf
				    ("\nCan lock media=%s\n", capab, strstr(buf, "Does not") ? "No" : "Yes");
			    }
			} else if ((strstr(buf, "read")
				    || strstr(buf, "write"))
				   && strstr(buf, "kB/s")) {
			    speed =
				g_strconcat(speed ? speed : "", strreplacechr(g_strstrip(buf), ":", '='), "\n", NULL);
			} else if (strstr(buf, "Device seems to be")) {
			    driver = g_strdup_printf(_("Driver=%s\n"), strchr(buf, ':') + 1);
			}
		    }

		    pclose(prcap);
		    g_timer_destroy(timer);
		}

		g_free(tmp);
	    }
	    g_free(device);

	    device = g_strdup_printf("/proc/ide/hd%c/cache", iface);
	    if (g_file_test(device, G_FILE_TEST_EXISTS)) {
		proc_ide = fopen(device, "r");
		if (proc_ide) {
                    (void) fscanf(proc_ide, "%d", &cache);
                    fclose(proc_ide);
                } else {
                    cache = 0;
                }
	    }
	    g_free(device);

	    device = g_strdup_printf("/proc/ide/hd%c/geometry", iface);
	    if (g_file_test(device, G_FILE_TEST_EXISTS)) {
		gchar *tmp;

		proc_ide = fopen(device, "r");
		if (proc_ide) {
                    (void) fgets(buf, 64, proc_ide);
                    for (tmp = buf; *tmp; tmp++) {
                        if (*tmp >= '0' && *tmp <= '9')
                            break;
                    }

                    pgeometry = g_strdup(g_strstrip(tmp));

                    (void) fgets(buf, 64, proc_ide);
                    for (tmp = buf; *tmp; tmp++) {
                        if (*tmp >= '0' && *tmp <= '9')
                            break;
                    }
                    lgeometry = g_strdup(g_strstrip(tmp));

                    fclose(proc_ide);
                } else {
                    pgeometry = g_strdup("Unknown");
                    lgeometry = g_strdup("Unknown");
                }

	    }
	    g_free(device);

	    n++;

	    gchar *devid = g_strdup_printf("IDE%d", n);

	    ide_storage_list = h_strdup_cprintf("$%s$%s=\n", ide_storage_list, devid, model);
	    storage_icons =
		h_strdup_cprintf("Icon$%s$%s=%s.png\n", storage_icons,
				 devid, model, g_str_equal(media, "cdrom") ? "cdrom" : "hdd");

	    gchar *strhash = g_strdup_printf(_("[Device Information]\n" "Model=%s\n"),
					     model);

	    const gchar *url = vendor_get_url(model);

	    if (url) {
		strhash = h_strdup_cprintf(_("Vendor=%s (%s)\n"), strhash, vendor_get_name(model), url);
	    } else {
		strhash = h_strdup_cprintf(_("Vendor=%s\n"), strhash, vendor_get_name(model));
	    }

	    strhash = h_strdup_cprintf(_("Device Name=hd%c\n"
					 "Media=%s\n" "Cache=%dkb\n"), strhash, iface, media, cache);
	    if (driver) {
		strhash = h_strdup_cprintf("%s\n", strhash, driver);

		g_free(driver);
		driver = NULL;
	    }

	    if (pgeometry && lgeometry) {
		strhash = h_strdup_cprintf(_("[Geometry]\n"
					     "Physical=%s\n" "Logical=%s\n"), strhash, pgeometry, lgeometry);

		g_free(pgeometry);
		pgeometry = NULL;
		g_free(lgeometry);
		lgeometry = NULL;
	    }

	    if (capab) {
		strhash = h_strdup_cprintf(_("[Capabilities]\n%s"), strhash, capab);

		g_free(capab);
		capab = NULL;
	    }

	    if (speed) {
		strhash = h_strdup_cprintf(_("[Speeds]\n%s"), strhash, speed);

		g_free(speed);
		speed = NULL;
	    }

	    moreinfo_add_with_prefix("DEV", devid, strhash);
	    g_free(devid);
	    g_free(model);
	} else {
	    g_free(device);
        }

	iface++;
    }

    if (n) {
	storage_list = h_strconcat(storage_list, ide_storage_list, NULL);
	g_free(ide_storage_list);
    }
}
示例#7
0
void
scan_filesystems(void)
{
    FILE *mtab;
    gchar buf[1024];
    struct statfs sfs;
    int count = 0;

    g_free(fs_list);
    fs_list = g_strdup("");
    moreinfo_del_with_prefix("COMP:FS");

    mtab = fopen("/etc/mtab", "r");
    if (!mtab)
	return;

    while (fgets(buf, 1024, mtab)) {
	gfloat size, used, avail;
	gchar **tmp;

	tmp = g_strsplit(buf, " ", 0);
	if (!statfs(tmp[1], &sfs)) {
		gfloat use_ratio;

		size = (float) sfs.f_bsize * (float) sfs.f_blocks;
		avail = (float) sfs.f_bsize * (float) sfs.f_bavail;
		used = size - avail;

		if (size == 0.0f) {
			continue;
		}

		if (avail == 0.0f) {
			use_ratio = 100.0f;
		} else {
			use_ratio = 100.0f * (used / size);
		}

		gchar *strsize = size_human_readable(size),
		      *stravail = size_human_readable(avail),
	  	      *strused = size_human_readable(used);

		gchar *strhash;
		
		strreplacechr(tmp[0], "#", '_');
		strhash = g_strdup_printf("[%s]\n"
					  "Filesystem=%s\n"
					  "Mounted As=%s\n"
					  "Mount Point=%s\n"
					  "Size=%s\n"
					  "Used=%s\n"
					  "Available=%s\n",
					  tmp[0],
					  tmp[2],
					  strstr(tmp[3], "rw") ? "Read-Write" :
					  "Read-Only", tmp[1], strsize, strused,
					  stravail);
               gchar *key = g_strdup_printf("FS%d", ++count);
		moreinfo_add_with_prefix("COMP", key, strhash);
		g_free(key);

		fs_list = h_strdup_cprintf("$FS%d$%s=%.2f %% (%s of %s)|%s\n",
					  fs_list,
					  count, tmp[0], use_ratio, stravail, strsize, tmp[1]);

		g_free(strsize);
		g_free(stravail);
		g_free(strused);
	}
	g_strfreev(tmp);
    }

    fclose(mtab);
}