在下面的示例中,我使用指向类mySession
中结构的指针,并通过手动分配和解除分配内存来填充和使用两种不同的方法。仅仅通过使用智能指针,怎么可能做和我在这里做的一样的事情呢?我根本不知道如何使用智能指针。请通过更改使用智能指针的代码来显示它。谢谢你。OpenStackSession
OpenStack
请注意:我必须在不使用命名空间 std 和库字符串的情况下执行此操作。我不知道它对智能指针是否重要,但我认为值得告知。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct OpenStackSession
{
int limit;
char prefix[256];
char marker[256];
char delimiter[256];
char format[256];
char path[256];
};
class OpenStack
{
public:
OpenStackSession* mySession;
void requestbegin(const char*);
void requestpayload();
};
void OpenStack::requestbegin(const char* url)
{
mySession=new OpenStackSession();
char urlCopy[2000];
strcpy(urlCopy,url);
urlCopy[strlen(url)]='\0';
char* questionMarkPointer=strchr(urlCopy,'?');
int questionMarkPosition=questionMarkPointer-urlCopy;
char query[2000];
strncpy(query,urlCopy+questionMarkPosition+1,strlen(urlCopy)-questionMarkPosition-1);
query[strlen(urlCopy)-questionMarkPosition-1]='\0';
int lenForEqSign=0;
int plusAndSignPosition=0;
int andSignPosition=0;
bool check=true;
while(check == true)
{
char* equalSignPointer=strchr(query+plusAndSignPosition,'=');
int lenForEqSign=equalSignPointer-query;
char* andSignPointer=strchr(query+plusAndSignPosition,'&');
if(andSignPointer!=NULL)
{
andSignPosition=andSignPointer-query;
}
else
{
andSignPosition = strlen(query);
check=false;
}
if(strncmp(query+plusAndSignPosition,"limit",5) == 0)
{
char temp[256];
strncpy(temp,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
temp[andSignPosition-lenForEqSign-1]='\0';
mySession->limit=atoi(temp);
}
else if(strncmp(query+plusAndSignPosition,"prefix",6) == 0)
{
strncpy(mySession->prefix,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
mySession->prefix[andSignPosition-lenForEqSign-1]='\0';
}
else if(strncmp(query+plusAndSignPosition,"marker",6) == 0)
{
strncpy(mySession->marker,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
mySession->marker[andSignPosition-lenForEqSign-1]='\0';
}
else if(strncmp(query+plusAndSignPosition,"delimiter",9) == 0)
{
strncpy(mySession->delimiter,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
mySession->delimiter[andSignPosition-lenForEqSign-1]='\0';
}
else if(strncmp(query+plusAndSignPosition,"path",4) == 0)
{
strncpy(mySession->path,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
mySession->path[andSignPosition-lenForEqSign-1]='\0';
}
else if(strncmp(query+plusAndSignPosition,"format",6) == 0)
{
strncpy(mySession->format,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
mySession->format[andSignPosition-lenForEqSign-1]='\0';
}
plusAndSignPosition=andSignPosition+1;
}
}
void OpenStack::requestpayload()
{
printf("Limit %d\n",mySession->limit);
printf("Prefix %s\n",mySession->prefix);
printf("Marker %s\n",mySession->marker);
printf("Delimiter %s\n",mySession->delimiter);
printf("Path %s\n",mySession->path);
printf("Format %s\n",mySession->format);
delete mySession;
}
void main()
{
const char* url="/v1/AUTH_abb52a71-fc76-489b-b56b-732b66bf50b1/images/a/plusAndSignPosition/c/andSignPosition/e?limit=100000&prefix=this-is-prefixdelimiter&marker={}marker&path=/temp/path/marko/it&delimiter=/delimiter&format=xmlformat" ;
OpenStack open;
open.requestbegin(url);
open.requestpayload();
}