本文整理汇总了C++中str::cstr方法的典型用法代码示例。如果您正苦于以下问题:C++ str::cstr方法的具体用法?C++ str::cstr怎么用?C++ str::cstr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类str
的用法示例。
在下文中一共展示了str::cstr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: m
str
trunc_at_first_null (const str &s)
{
assert (s);
const char *cp;
size_t len = s.len ();
size_t l;
const char nullc = '\0';
// Figure out if a '\0' or the end-of-string comes first.
for (cp = s.cstr (), l = 0; *cp != nullc && l < len; cp++, l++);
str ret;
if (*cp == nullc) {
// The most likely outcome
if (l == len) ret = s;
// There was a '\0' inside the string
else ret = s.cstr ();
} else {
// String is not null terminated!
mstr m (len + 1);
memcpy (m.cstr (), s.cstr (), len);
m[len] = nullc;
ret = m;
}
return ret;
}
示例2: wrap
void
dhblock_keyhash_srv::real_store (chordID key, str od, str nd, u_int32_t exp, cb_dhstat cb)
{
u_int32_t v1 = dhblock_keyhash::version (nd.cstr (), nd.len ());
if (od.len ()) {
u_int32_t v0 = dhblock_keyhash::version (od.cstr (), od.len ());
if (v0 > v1) {
chordID p = node->my_pred ()->id ();
chordID m = node->my_ID ();
if (betweenrightincl (p, m, key))
cb (DHASH_STALE);
else
cb (DHASH_RETRY);
} else {
info << "db delete: " << key << "\n";
db->remove (key, v0,
wrap (this, &dhblock_keyhash_srv::delete_cb, key,
nd, v1, exp, cb));
}
} else {
info << "db write: " << node->my_ID ()
<< " N " << key << " " << nd.len () << "\n";
db_store (key, nd, v1, exp, cb);
}
}
示例3: memset
wide_str_t::wide_str_t (str utf8_in)
{
if (utf8_in) {
_init (utf8_in.len ());
mbstate_t state;
memset (&state, 0, sizeof (state));
const char *src = utf8_in.cstr ();
setlocale (LC_CTYPE, "en_US.UTF-8");
ssize_t ret = mbstowcs (_buf->base (), src, _buf->size ());
if (ret < 0) {
_err = true;
_buf = NULL;
_len = 0;
} else {
_len = ret;
if (0 && src) {
warn << "XX failed to completely convert string ('"
<< utf8_in << "'): "
<< "expected " << _len << " bytes, but only converted "
<< (src - utf8_in.cstr ()) << "\n";
_err = true;
} else {
_err = false;
}
}
}
}
示例4: return
bool
can_read (const str &f)
{
struct stat sb;
return (f && stat(f.cstr (), &sb) == 0 && S_ISREG (sb.st_mode)
&& access (f.cstr(), R_OK) == 0);
}
示例5: assert
/* If the directory specified by path does not exist, create it with
* the given mode. If we fail for any reason, terminate with error. */
void
mksfsdir (str path, mode_t mode, struct stat *sbp, uid_t uid)
{
assert (path[0] == '/');
mode_t m = umask (0);
struct stat sb;
if (stat (path, &sb) < 0) {
if (errno != ENOENT || (mkdir (path, mode) < 0 && errno != EEXIST))
fatal ("%s: %m\n", path.cstr ());
if (chown (path, uid, sfs_gid) < 0) {
int saved_errno = errno;
rmdir (path);
fatal ("chown (%s): %s\n", path.cstr (), strerror (saved_errno));
}
if (stat (path, &sb) < 0)
fatal ("stat (%s): %m\n", path.cstr ());
}
umask (m);
if (!S_ISDIR (sb.st_mode))
fatal ("%s: not a directory\n", path.cstr ());
if (sb.st_uid != uid)
fwarn << path << ": owned by uid " << sb.st_uid
<< ", should be uid " << uid << "\n";
if (sb.st_gid != sfs_gid)
fwarn << path << ": has gid " << sb.st_gid
<< ", should be gid " << sfs_gid << "\n";
if (sb.st_mode & 07777 & ~mode)
fwarn ("%s: mode 0%o, should be 0%o\n",
path.cstr (), int (sb.st_mode & 07777), int (mode));
if (sbp)
*sbp = sb;
}
示例6: fatal
int
suidgetfd_required (str prog)
{
int fds[2];
if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) < 0)
fatal ("socketpair: %m\n");
close_on_exec (fds[0]);
str path = fix_exec_path ("suidconnect");
char *av[] = { "suidconnect", const_cast<char *> (prog.cstr ()), NULL };
if (spawn (path, av, fds[1]) == -1)
fatal << path << ": " << strerror (errno) << "\n";
close (fds[1]);
int fd = recvfd (fds[0]);
if (fd < 0) {
struct stat sb;
if (!runinplace && !stat (path, &sb)
&& (sb.st_gid != sfs_gid || !(sb.st_mode & 02000))) {
if (struct group *gr = getgrgid (sfs_gid))
warn << path << " should be setgid to group " << gr->gr_name << "\n";
else
warn << path << " should be setgid to group " << sfs_gid << "\n";
}
else {
warn << "have you launched the appropriate daemon (sfscd or sfssd)?\n";
warn << "have subsidiary daemons died (check your system logs)?\n";
}
fatal ("could not suidconnect for %s\n", prog.cstr ());
}
close (fds[0]);
return fd;
}
示例7: convertint
static void
idlookup (str uid, str gid)
{
if (!uid)
uid = "sfs";
if (!gid)
gid = uid;
bool uidok = convertint (uid, &sfs_uid);
struct passwd *pw = uidok ? getpwuid (sfs_uid) : getpwnam (uid.cstr ());
bool gidok = convertint (gid, &sfs_gid);
struct group *gr = gidok ? getgrgid (sfs_gid) : getgrnam (gid.cstr ());
if (!uidok) {
if (!pw)
fatal << "Could not find user " << uid << "\n";
sfs_uid = pw->pw_uid;
}
if (!gidok) {
if (!gr)
fatal << "Could not find group " << gid << "\n";
sfs_gid = gr->gr_gid;
}
if (gr && gr->gr_mem[0])
fwarn << "Group " << gid << " must not have any members\n";
if (pw && gr && (gid_t) pw->pw_gid != (gid_t) gr->gr_gid)
fwarn << "User " << uid << " must have login group " << gid << ".\n";
endpwent ();
endgrent ();
}
示例8: substr
inline str
group_prefix (str s, str prefix)
{
if (s.len () <= prefix.len () || s[prefix.len ()] != '.'
|| memcmp (s.cstr (), prefix.cstr (), prefix.len ()))
return NULL;
return substr (s, prefix.len () + 1);
}
示例9: if
str
can_exec (const str &p)
{
if (access (p.cstr (), R_OK))
return ("cannot read executable");
else if (access (p.cstr (), X_OK))
return ("cannot execute");
return NULL;
}
示例10: count_newlines
u_int count_newlines (str s)
{
const char *end = s.cstr () + s.len ();
u_int c = 0;
for (const char *cp = s.cstr (); cp < end; cp++) {
if (*cp == '\n')
c++;
}
return c;
}
示例11:
void
suio_print (suio *uio, const str &s)
{
if (s.len () <= suio::smallbufsize)
uio->copy (s.cstr (), s.len ());
else {
uio->print (s.cstr (), s.len ());
uio->iovcb (wrap (&s.b.Xplug, s.b.Xleak ()));
}
}
示例12: munmap
void
random_set_seedfile (str path)
{
if (!path) {
if (seed) {
munmap (reinterpret_cast<char *> (seed), mapsize);
seed = NULL;
}
return;
}
if (path[0] == '~' && path[1] == '/') {
const char *home = getenv ("HOME");
if (!home) {
warn ("$HOME not set in environment\n");
return;
}
path = strbuf () << home << (path.cstr () + 1);
}
int fd = open (path, O_CREAT|O_RDWR, 0600);
if (fd < 0) {
warn ("%s: %m\n", path.cstr ());
return;
}
struct stat sb;
char c;
if (read (fd, &c, 1) < 0 || fstat (fd, &sb) < 0
|| lseek (fd, mapsize - 1, SEEK_SET) == -1
|| write (fd, "", 1) < 0) {
/* The read call avoids a segfault on NFS 2. Specifically, if we
* are root and the random_seed file is over NFS 2, the open will
* succeed even though read returns EACCES. If we map the file
* when we can't read it--bingo, segfault. (In fact, on some OSes
* it also seems to cause a kernel panic when you examine the
* mmapped memory from the debugger.) */
close (fd);
warn ("%s: %m\n", path.cstr ());
return;
}
if ((sb.st_mode & 07777) != 0600)
warn ("%s: mode 0%o should be 0600\n", path.cstr (), sb.st_mode & 07777);
if (seed)
munmap (reinterpret_cast<char *> (seed), mapsize);
seed = mmap (NULL, (size_t) mapsize, PROT_READ|PROT_WRITE,
MAP_FILE|MAP_SHARED, fd, 0);
if (seed == reinterpret_cast<void *> (MAP_FAILED)) {
warn ("mmap: %s: %m\n", path.cstr ());
seed = NULL;
}
else
rnd_input.update (seed, seedsize);
close (fd);
}
示例13: strdup
int
open_file (const str &nm, int flags)
{
char *s = strdup (nm.cstr ());
int rc = 0;
rc = mkdir_p (s);
if (rc == 0)
rc = ::open (nm.cstr (), flags, 0666);
if (s) free (s);
return rc;
}
示例14: start_log_to_file
int
start_logger (const str &priority, const str &tag, const str &line,
const str &logfile, int flags, mode_t mode)
{
str logger;
#ifdef PATH_LOGGER
logger = PATH_LOGGER;
#endif
if (logger) {
const char *av[] = { NULL, "-p", NULL, "-t", NULL, NULL, NULL };
av[0] = const_cast<char *> (logger.cstr ());
av[2] = const_cast<char *> (priority.cstr ());
if (line)
av[5] = const_cast<char *> (line.cstr ());
else
av[5] = "log started";
if (tag)
av[4] = const_cast<char *> (tag.cstr ());
else
av[4] = "";
pid_t pid;
int status;
if ((pid = spawn (av[0], av, 0, 0, errfd)) < 0) {
warn ("%s: %m\n", logger.cstr ());
return start_log_to_file (line, logfile, flags, mode);
}
if (waitpid (pid, &status, 0) <= 0 || !WIFEXITED (status) ||
WEXITSTATUS (status))
return start_log_to_file (line, logfile, flags, mode);
int fds[2];
if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) < 0)
fatal ("socketpair: %m\n");
close_on_exec (fds[0]);
if (fds[1] != 0)
close_on_exec (fds[1]);
av[5] = NULL;
if (spawn (av[0], av, fds[1], 0, 0) >= 0) {
close (fds[1]);
return fds[0];
} else {
warn ("%s: %m\n", logger.cstr ());
}
}
return start_log_to_file (line, logfile, flags, mode);
}
示例15: strlen
static str
makehdrname (str fname)
{
strbuf hdr;
const char *p;
if ((p = strrchr (fname.cstr(), '/')))
p++;
else p = fname.cstr();
hdr.buf (p, strlen (p) - 1);
hdr.cat ("h");
return hdr;
}