str: add an overloaded startswith() utility method
for various string types and use it in a few places.
This commit is contained in:
parent
d55115936e
commit
b647b48bf4
|
@ -45,6 +45,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "base/misc.hh"
|
#include "base/misc.hh"
|
||||||
|
#include "base/str.hh"
|
||||||
#include "base/types.hh"
|
#include "base/types.hh"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
@ -77,7 +78,7 @@ procInfo(const char *filename, const char *target)
|
||||||
|
|
||||||
while (fp && !feof(fp) && !done) {
|
while (fp && !feof(fp) && !done) {
|
||||||
if (fgets(line, 80, fp)) {
|
if (fgets(line, 80, fp)) {
|
||||||
if (strncmp(line, target, strlen(target)) == 0) {
|
if (startswith(line, target)) {
|
||||||
snprintf(format, sizeof(format), "%s %%ld", target);
|
snprintf(format, sizeof(format), "%s %%ld", target);
|
||||||
sscanf(line, format, &usage);
|
sscanf(line, format, &usage);
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
#define __STR_HH__
|
#define __STR_HH__
|
||||||
|
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
#include <cstring>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -140,4 +141,35 @@ quote(const std::string &s)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if 's' starts with the prefix string 'prefix'.
|
||||||
|
*/
|
||||||
|
inline bool
|
||||||
|
startswith(const char *s, const char *prefix)
|
||||||
|
{
|
||||||
|
return (strncmp(s, prefix, strlen(prefix)) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if 's' starts with the prefix string 'prefix'.
|
||||||
|
*/
|
||||||
|
inline bool
|
||||||
|
startswith(const std::string &s, const char *prefix)
|
||||||
|
{
|
||||||
|
return (s.compare(0, strlen(prefix), prefix) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if 's' starts with the prefix string 'prefix'.
|
||||||
|
*/
|
||||||
|
inline bool
|
||||||
|
startswith(const std::string &s, const std::string &prefix)
|
||||||
|
{
|
||||||
|
return (s.compare(0, prefix.size(), prefix) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif //__STR_HH__
|
#endif //__STR_HH__
|
||||||
|
|
|
@ -640,8 +640,8 @@ openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
|
||||||
DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
|
DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
|
||||||
|
|
||||||
int fd;
|
int fd;
|
||||||
if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
|
if (startswith(path, "/proc/") || startswith(path, "/system/") ||
|
||||||
!path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
|
startswith(path, "/platform/") || startswith(path, "/sys/")) {
|
||||||
// It's a proc/sys entery and requires special handling
|
// It's a proc/sys entery and requires special handling
|
||||||
fd = OS::openSpecialFile(path, process, tc);
|
fd = OS::openSpecialFile(path, process, tc);
|
||||||
return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
|
return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
|
||||||
|
|
|
@ -51,6 +51,7 @@
|
||||||
#include "arch/vtophys.hh"
|
#include "arch/vtophys.hh"
|
||||||
#include "base/loader/object_file.hh"
|
#include "base/loader/object_file.hh"
|
||||||
#include "base/loader/symtab.hh"
|
#include "base/loader/symtab.hh"
|
||||||
|
#include "base/str.hh"
|
||||||
#include "base/trace.hh"
|
#include "base/trace.hh"
|
||||||
#include "config/the_isa.hh"
|
#include "config/the_isa.hh"
|
||||||
#include "cpu/thread_context.hh"
|
#include "cpu/thread_context.hh"
|
||||||
|
@ -402,8 +403,7 @@ MasterID
|
||||||
System::getMasterId(std::string master_name)
|
System::getMasterId(std::string master_name)
|
||||||
{
|
{
|
||||||
// strip off system name if the string starts with it
|
// strip off system name if the string starts with it
|
||||||
if (master_name.size() > name().size() &&
|
if (startswith(master_name, name()))
|
||||||
master_name.compare(0, name().size(), name()) == 0)
|
|
||||||
master_name = master_name.erase(0, name().size() + 1);
|
master_name = master_name.erase(0, name().size() + 1);
|
||||||
|
|
||||||
// CPUs in switch_cpus ask for ids again after switching
|
// CPUs in switch_cpus ask for ids again after switching
|
||||||
|
|
Loading…
Reference in a new issue