📄 segment.cpp
字号:
// Segment.cpp: implementation of the Segment class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Segment.h"
#include "Base64.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Segment::Segment()
{
subsegment_ptr = NULL;
m_subsegmentsaccount = 0;
boundary_ptr = NULL;
}
Segment::~Segment()
{
if(boundary_ptr != NULL)
{
delete []boundary_ptr;
boundary_ptr = NULL;
}
if(subsegment_ptr != NULL)
{
for(int i = 0;i< m_subsegmentsaccount;i++)
{
if(subsegment_ptr[i] != NULL)
delete subsegment_ptr[i];
}
delete[] subsegment_ptr;
}
}
bool Segment::ParseContentType()
{
char* buf = m_body;
char* p = strstr(buf, "Content-Type: ");
if ( p == NULL )
{
strcpy(m_ContentType,"text/plain");
return true;
}
p = p + 14;
for (int i = 0; i < 128; i++) {
if ( p[i] == '\r' || p[i] == '\n' || p[i] == ';') {
this->m_ContentType[i] = '\0';
break;
}
this->m_ContentType[i] = p[i];
}
return true;
}
bool Segment::ParseTransferEncode()
{
char* buf = m_body;
char* p = strstr(buf, "Content-Transfer-Encoding: ");
if ( p == NULL )
return false;
p = p + 27;
for (int i = 0; i < 128; i++) {
if ( p[i] == '\r' || p[i] == '\n') {
this->m_ContentTransferEncoding[i] = '\0';
break;
}
this->m_ContentTransferEncoding[i] = p[i];
}
return true;
}
bool Segment::ParseContent()
{
char* buf = m_body;
//char* content_start = st; ////////////第一个subsegment的开始
char* p = strstr(buf,"\r\n\r\n");
if( p == NULL)
return false;
while(*p == '\r' || *p == '\n')
{
p++;
}
this->m_Content = p;
return true;
}
void Segment::ParseSegmentAccount()
{
char* temp = m_Content;
int count = 0;
char subsegment_start[50] = "--";
strcat(subsegment_start,m_Boundary);
while(true)
{
temp = strstr(temp , subsegment_start);
if(temp == (char*)NULL )
break;
temp = temp+ strlen(m_Boundary) + 2;
count++;
}
if(count != 0)
{
this->m_subsegmentsaccount = count;
this->subsegment_ptr = new Segment*[m_subsegmentsaccount];
this->boundary_ptr = new char*[m_subsegmentsaccount];
for(int i = 0;i < m_subsegmentsaccount;++i)
boundary_ptr[i] = NULL;
}
}
bool Segment::ParseBoundary()
{
char* buf = m_body;
char* p = strstr(buf, "boundary");
if ( p == NULL )
return false;
p = p + 8;
p = strstr(p,"=") + 1;
while(*p == ' ' || *p == '"')
{
++p;
}
for (int i = 0; i < 128; i++) {
if ( p[i] == '\r' || p[i] == '\n' || p[i] == '"' ) {
this->m_Boundary[i] = '\0';
break;
}
this->m_Boundary[i] = p[i];
}
return true;
}
void Segment::ParseSubSegment()
{
char* p= NULL;
for(int i = 0;i < m_subsegmentsaccount ; i++)
{
char* buf = boundary_ptr[i];
buf = buf + strlen(m_Boundary) + 2;
if(i != (m_subsegmentsaccount - 1))
{
char* segment_end = boundary_ptr[i+1] - 2;
*segment_end = '\0';
}
this->subsegment_ptr[i] = new Segment;
this->subsegment_ptr[i]->m_body = buf;
}
}
bool Segment::ParseEnd()
{
char segment_end[50] = "--";
strcat(segment_end,m_Boundary);
strcat(segment_end,"--");
char* p ;
if( (p = strstr(m_body,segment_end)) != (char*)NULL)
{
char* end = p - 2;
*end = '\0';
return true;
}
return false;
}
bool Segment::AddSubSegment(long index,Segment* subsegment)
{
this->subsegment_ptr[index] = subsegment;
return true;
}
void Segment::ParseSubSegmentStart()
{
char* temp = m_Content;
int count = 0;
char subsegment_start[50] = "--";
strcat(subsegment_start,m_Boundary);
for(int i = 0;i<m_subsegmentsaccount;i++)
{
temp = strstr(temp , subsegment_start);
boundary_ptr[i] = temp;
temp = temp+ strlen(m_Boundary) + 2;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -