const char *WordWrap(const char *src, size_t cols) { __gnu_cxx::StrSplitMap lines; __gnu_cxx::StrSplitMap parts; string::size_type partcount; string::size_type linecount; string startstr; string curline; string::size_type curpos; char *tmpstr; char *retStr; retStr = new char[strlen(src) * 2 + 512]; memset(retStr, 0, strlen(src) * 2 + 512); linecount = StrSplit(src, "\n", lines); for (unsigned int j = 0; j < linecount; j++) { startstr.erase(); curline.erase(); if (strlen(lines[j].c_str())) { tmpstr = new char[strlen(lines[j].c_str())+1024]; memset(tmpstr, 0, strlen(lines[j].c_str())+1024); strcpy(tmpstr, lines[j].c_str()); startstr.assign(tmpstr); // Replace all tabs with spaces. while ((curpos = startstr.find("\t")) != string::npos) startstr.replace(curpos, 1, " "); while ((curpos = startstr.find("\r")) != string::npos) startstr.replace(curpos, 1, " "); // Now, simplify all the white space. while ((curpos = startstr.find(" ")) != string::npos) startstr.replace(curpos, 2, " "); StripWhiteSpace(startstr); partcount = StrSplit(startstr.c_str(), " ", parts); for (unsigned int i = 0; i < partcount; i++) { if (curline.size() + parts[i].size() > cols) { StripWhiteSpace(curline); curline += "\n"; strcat(retStr, curline.c_str()); curline.assign(parts[i].c_str()); curline += " "; } else { curline += parts[i].c_str(); curline += " "; } } curline += "\n"; strcat(retStr, curline.c_str()); } else { strcat(retStr, "\n"); } } return retStr; }
int GetIniLineType( char *pszLine ) { int nLineType; char ch; /* get the first non space char */ StripWhiteSpace( pszLine ); /* now that we have striped the white space off... */ /* the first char is the right one */ ch = pszLine[0]; switch ( ch ) { case ';': nLineType = ILT_COMMENT; break; case '[': nLineType = ILT_SECTION; break; case '\n': nLineType = ILT_BLANK; break; default: /* anything we cannot determine becomes an entry */ nLineType = ILT_ENTRY; break; } return(nLineType); }
void TextScanner::StripLeadingSpaces() { for (unsigned int i=0; i < m_lines.size(); i++) { m_lines[i] = StripWhiteSpace(m_lines[i]); } }
string TextScanner::GetAll() { string s; for (unsigned int i=0; i < m_lines.size(); i++) { s += StripWhiteSpace(m_lines[i])+"\n"; } return s; }
BOOL CFileTextLines::Save(const CString& sFilePath, bool bSaveAsUTF8, DWORD dwIgnoreWhitespaces /*=0*/, BOOL bIgnoreCase /*= FALSE*/, bool bBlame /*= false*/) { try { CString destPath = sFilePath; // now make sure that the destination directory exists int ind = 0; while (destPath.Find('\\', ind)>=2) { if (!PathIsDirectory(destPath.Left(destPath.Find('\\', ind)))) { if (!CreateDirectory(destPath.Left(destPath.Find('\\', ind)), NULL)) return FALSE; } ind = destPath.Find('\\', ind)+1; } CStdioFile file; // Hugely faster than CFile for big file writes - because it uses buffering if (!file.Open(sFilePath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary)) { m_sErrorString.Format(IDS_ERR_FILE_OPEN, (LPCTSTR)sFilePath); return FALSE; } if ((!bSaveAsUTF8)&&(m_UnicodeType == CFileTextLines::UNICODE_LE)) { //first write the BOM UINT16 wBOM = 0xFEFF; file.Write(&wBOM, 2); for (int i=0; i<GetCount(); i++) { CString sLine = GetAt(i); EOL ending = GetLineEnding(i); StripWhiteSpace(sLine,dwIgnoreWhitespaces, bBlame); if (bIgnoreCase) sLine = sLine.MakeLower(); file.Write((LPCTSTR)sLine, sLine.GetLength()*sizeof(TCHAR)); if (ending == EOL_AUTOLINE) ending = m_LineEndings; switch (ending) { case EOL_CR: sLine = _T("\x0d"); break; case EOL_CRLF: case EOL_AUTOLINE: sLine = _T("\x0d\x0a"); break; case EOL_LF: sLine = _T("\x0a"); break; case EOL_LFCR: sLine = _T("\x0a\x0d"); break; default: sLine.Empty(); break; } if ((m_bReturnAtEnd)||(i != GetCount()-1)) file.Write((LPCTSTR)sLine, sLine.GetLength()*sizeof(TCHAR)); } } else if ((!bSaveAsUTF8)&&((m_UnicodeType == CFileTextLines::ASCII)||(m_UnicodeType == CFileTextLines::AUTOTYPE))) { for (int i=0; i< GetCount(); i++) { // Copy CString to 8 bit without conversion CString sLineT = GetAt(i); CStringA sLine = CStringA(sLineT); EOL ending = GetLineEnding(i); StripAsciiWhiteSpace(sLine,dwIgnoreWhitespaces, bBlame); if (bIgnoreCase) sLine = sLine.MakeLower(); if ((m_bReturnAtEnd)||(i != GetCount()-1)) { if (ending == EOL_AUTOLINE) ending = m_LineEndings; switch (ending) { case EOL_CR: sLine += '\x0d'; break; case EOL_CRLF: case EOL_AUTOLINE: sLine.Append("\x0d\x0a", 2); break; case EOL_LF: sLine += '\x0a'; break; case EOL_LFCR: sLine.Append("\x0a\x0d", 2); break; } } file.Write((LPCSTR)sLine, sLine.GetLength()); } } else if ((bSaveAsUTF8)||((m_UnicodeType == CFileTextLines::UTF8BOM)||(m_UnicodeType == CFileTextLines::UTF8))) { if (m_UnicodeType == CFileTextLines::UTF8BOM) { //first write the BOM UINT16 wBOM = 0xBBEF; file.Write(&wBOM, 2); UINT8 uBOM = 0xBF; file.Write(&uBOM, 1); } for (int i=0; i<GetCount(); i++) { CStringA sLine = CUnicodeUtils::GetUTF8(GetAt(i)); EOL ending = GetLineEnding(i); StripAsciiWhiteSpace(sLine,dwIgnoreWhitespaces, bBlame); if (bIgnoreCase) sLine = sLine.MakeLower(); if ((m_bReturnAtEnd)||(i != GetCount()-1)) { if (ending == EOL_AUTOLINE) ending = m_LineEndings; switch (ending) { case EOL_CR: sLine += '\x0d'; break; case EOL_CRLF: case EOL_AUTOLINE: sLine.Append("\x0d\x0a",2); break; case EOL_LF: sLine += '\x0a'; break; case EOL_LFCR: sLine.Append("\x0a\x0d",2); break; } } file.Write((LPCSTR)sLine, sLine.GetLength()); } } file.Close(); } catch (CException * e) { e->GetErrorMessage(m_sErrorString.GetBuffer(4096), 4096); m_sErrorString.ReleaseBuffer(); e->Delete(); return FALSE; } return TRUE; }
int extractConnectInfo(FILE *fp, ConnectNotice &cn) { // First, clear out the connect notice. cn.noticeID = 0; cn.customerID = 0; cn.ticketNo = 0; strcpy(cn.noticeDate, ""); strcpy(cn.connType, ""); strcpy(cn.custName, ""); strcpy(cn.addr1, ""); strcpy(cn.addr2, ""); strcpy(cn.addr3, ""); strcpy(cn.isSelect, ""); strcpy(cn.cbr, ""); strcpy(cn.dueDate, ""); strcpy(cn.custCirc, ""); strcpy(cn.hostCirc, ""); strcpy(cn.speed, ""); strcpy(cn.vpi, ""); strcpy(cn.vci, ""); // Create our buffers and work variables. int retVal = 0; int bufsize = 65536; char *buf = new char[bufsize]; __gnu_cxx::StrSplitMap tmpList; __gnu_cxx::StrSplitMap bodyLines; int lineCount; string tmpStr1; string tmpStr2; int splitCount; size_t byteCount; time_t msgUnixTime; // Load the entire message into memory. byteCount = fread((void *)buf, sizeof(char), bufsize, fp); if (!byteCount) return(retVal); // Okay the whole file should be in memory now. Parse it as a DwMessage DwMessage msg(buf); msg.Parse(); msgUnixTime = msg.Headers().Date().AsCalendarTime(); tm *t = localtime(&msgUnixTime); int theYear = t->tm_year + 1900; sprintf(cn.noticeDate, "%04d-%02d-%02d %02d:%02d:%02d", theYear, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); lineCount = StrSplit(msg.Body().AsString().c_str(), "\n", bodyLines); // Loop through the file until EOF. //while ((fgets(buf, bufsize, fp) != NULL)) { for (int i = 0; i < lineCount; i++) { strcpy(buf, bodyLines[i].c_str()); // Strip off the whitespace. tmpList.clear(); tmpStr1 = buf; StripWhiteSpace(tmpStr1); splitCount = StrSplit(tmpStr1.c_str(), " ", tmpList, 2); // Look for any of our values if (!strncasecmp(buf, "ACTION", strlen("ACTION"))) { strcpy(cn.connType, tmpList[1].c_str()); } if (!strncasecmp(buf, "NAME", strlen("NAME"))) { strcpy(cn.custName, tmpList[1].c_str()); } if (!strncasecmp(buf, "ADDR1", strlen("ADDR1"))) { strcpy(cn.addr1, tmpList[1].c_str()); } if (!strncasecmp(buf, "ADDR2", strlen("ADDR2"))) { strcpy(cn.addr2, tmpList[1].c_str()); } if (!strncasecmp(buf, "ADDR3", strlen("ADDR3"))) { strcpy(cn.addr3, tmpList[1].c_str()); } if (!strncasecmp(buf, "SELECT256", strlen("SELECT256"))) { strcpy(cn.isSelect, tmpList[1].c_str()); } if (!strncasecmp(buf, "CBR", strlen("CBR"))) { strcpy(cn.cbr, tmpList[1].c_str()); } if (!strncasecmp(buf, "DUEDATE", strlen("DUEDATE"))) { strcpy(cn.dueDate, tmpList[1].c_str()); } if (!strncasecmp(buf, "CKTTN", strlen("CKTTN"))) { strcpy(cn.custCirc, tmpList[1].c_str()); } if (!strncasecmp(buf, "HOSTCKT", strlen("HOSTCKT"))) { strcpy(cn.hostCirc, tmpList[1].c_str()); } if (!strncasecmp(buf, "SPEED", strlen("SPEED"))) { strcpy(cn.speed, tmpList[1].c_str()); } if (!strncasecmp(buf, "VPI", strlen("VPI"))) { strcpy(cn.vpi, tmpList[1].c_str()); } if (!strncasecmp(buf, "VCI", strlen("VCI"))) { strcpy(cn.vci, tmpList[1].c_str()); } } // Now, if we got a CKKTN, and an ACTION, it is good. if (strlen(cn.connType) && strlen(cn.custCirc)) retVal = 1; return retVal; }