// // GetClient.cpp - Use P4API to find current client name and root. // // Copyright (c) 2002 Anders Johnson <anders@ieee.org>. All rights // reserved. Refer to the accompanying README file for terms of use, // modification and distribution. // #include "clientapi.h" #if 0 // These (unguarded) headers happen to be included by clientapi.h #include "clientuser.h" #include "error.h" #endif // 0 #include "apiGetClient.h" #include <string> #include <cstring> #include <cstdlib> using std::string; class GetClient : public ClientUser { private: static const char *const client_str_="Client name: "; static const char *const root_str_="Client root: "; static bool initialized_; static int client_len_; static int root_len_; string name_; string root_; public: GetClient() { if(!initialized_) { client_len_=strlen(client_str_); root_len_=strlen(root_str_); initialized_=true; } name_=""; root_=""; }; void OutputInfo(char level, char *data) { if(strncmp(data, client_str_, client_len_)==0) { name_=string(data + client_len_); } else if(strncmp(data, root_str_, root_len_)==0) { root_=string(data + root_len_); } } virtual string name() const { return name_; } virtual string root() const { return root_; } virtual ~GetClient() {} }; bool GetClient::initialized_=false; int GetClient::client_len_; int GetClient::root_len_; static inline void str2cstr(char **cstrp, const string str, char *me) { if(cstrp) { int len=str.length(); if(len) { const char *const s=str.c_str(); *cstrp=static_cast<char*>( malloc((len+1)*sizeof(char)) ); if(!*cstrp) { perror(me); return; } strcpy(*cstrp, s); } else { *cstrp=0; } } } void apiGetClient(char **name, char **root, char *me) { GetClient ui; ClientApi client; Error e; e.SetTag(me); client.Init(&e); if( !client.Dropped() ) { client.SetArgv( 0, (char**) 0 ); client.Run( "info", &ui ); } client.Final(&e); if(e.GetSeverity() >= E_WARN) { e.Report(); } str2cstr(name, ui.name(), me); str2cstr(root, ui.root(), me); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 1265 | anders_johnson |
p4prompt-0.02 release. Added -p option. |