void GProfile::ProfileParse(const char *szBuffer, __int64 dwSize) { GProfileSection *pSection = 0; // parse the file __int64 nIdx = 0; GString strLine; while (nIdx < dwSize) { GetLine(strLine, szBuffer, &nIdx, dwSize); strLine.TrimRightWS(); strLine.TrimLeftWS(); if ((strLine.IsEmpty()) || (strLine.GetAt(0) == ';')) continue; strLine.Replace("\\n", '\n'); if (strLine.GetAt(0) == '[') { __int64 nIdx = strLine.Find(']'); if (nIdx == -1) nIdx = strLine.Length(); // new section pSection = new GProfileSection; pSection->m_strName = strLine.Mid(1, nIdx - 1); pSection->m_strName.TrimLeftWS(); pSection->m_strName.TrimRightWS(); m_lstSections.AddLast(pSection); } else if (pSection) { __int64 nIdx = strLine.Find('='); if (nIdx == -1) continue; GProfileEntry *pNVP = new GProfileEntry; pSection->m_lstNVP.AddLast(pNVP); pNVP->m_strName = strLine.Left(nIdx); pNVP->m_strName.TrimLeftWS(); pNVP->m_strName.TrimRightWS(); pNVP->m_strValue = strLine.Mid(nIdx + 1); pNVP->m_strValue.TrimLeftWS(); pNVP->m_strValue.TrimRightWS(); } } m_bCached = true; }
GString * GStringList::FindStringContaining(const char *pzWhatToFind, __int64 *pnFoundIndex, int bMatchCase/* = 1*/, __int64 nStartingIndex/* = 0*/) { GString *p = GetStrAt(nStartingIndex); while(p) { if (bMatchCase) { if (p->Find(pzWhatToFind) != -1) { if (pnFoundIndex) *pnFoundIndex = nStartingIndex; return p; } } else { if (p->FindCaseInsensitive(pzWhatToFind) == 0) { if (pnFoundIndex) *pnFoundIndex = nStartingIndex; return p; } } nStartingIndex++; p = (GString *)Next(); } if (pnFoundIndex) *pnFoundIndex = -1; return 0; // not found }
GString GHttp::Get(const GString &g) { GString page(g); if (!page.StartWith("http://")) { page = this->_host + page; if (!page.StartWith("http://")) page = "http://" + page; } GString str = "GET " + page + " HTTP/1.1\r\n"; str += "Host: " + this->_host + "\r\n"; if (!this->_proxyUser.IsEmpty()) { str += "Proxy-Authorization: Basic " + GCryptography::GBase64::Encode(this->_proxyUser + ":" + this->_proxyPass) + "\r\n"; str += "Proxy-Connection : keep-alive\r\n"; } else { str += "Keep-Alive: 20000\r\n"; str += "Connection : keep-alive\r\n"; } if (!this->_user.IsEmpty()) str += "Authorization: Basic " + GCryptography::GBase64::Encode(this->_user + ":" + this->_pass) + "\r\n"; str += "\r\n"; this->_socket.Send(str); GString rectot; while (true) { GString rec; rec = this->_socket.Receive(); if (rec.Size() < 1024) { rectot += rec; GString sub = rec.Substr(0, rec.Find("\r\n")); GStringList tab = sub.Split(" "); if (tab.Size() > 2) { //GESTION DES ERREUR } int pos = rec.Find("\r\n\r\n"); if (pos != GString::NotFound) ;//this->_response = this->_response.Substr(pos + 4); } } return (rectot); }
GArray<GString> GString::Split(GString& str, char const token) { str.Trim(token); GArray<GString> arr; size_t lastPos = 0; size_t tokenPos = str.Find(token); while (tokenPos != -1) { arr.Add(str.SubString(lastPos, tokenPos - 1)); lastPos = tokenPos + 1; tokenPos = str.Find(token, tokenPos + 1); } arr.Add(str.SubString(lastPos, str.Size() - 1)); return arr; }
// returns -1 if not found otherwise the Index GString * GStringList::FindStringContaining(char chWhatToFind, __int64 *pnFoundIndex, __int64 nStartingIndex/* = 0*/) { GString *p = GetStrAt(nStartingIndex); while(p) { if (p->Find(chWhatToFind) != -1) { if (pnFoundIndex) *pnFoundIndex = nStartingIndex; return p; } nStartingIndex++; p = (GString *)Next(); } if (pnFoundIndex) *pnFoundIndex = -1; return 0; // not found }
void GString::FormatNumber(const char *szFormat, char decimal_separator, char grouping_separator, char minus_sign, const char *NaN, char zero_digit, char digit, char pattern_separator, char percent, char permille) { if (szFormat && *szFormat) { // make sure that the string is a number {0..9, '.', '-'} // if the string contains a character not in the number // subset then set the value of the string to NaN. const char *szValue = _str; if (IsNaN(szValue, '.', ',', '-')) *this = NaN; else { // it's a number, get the whole part and the fraction part int nIdx = Find('.'); GString strWhole; strWhole = (nIdx == -1) ? _str : (const char *)Left(nIdx); GString strFraction('0', (short)1); nIdx = Find('.') + 1; if (nIdx > 0) strFraction = Mid(nIdx); bool bIsNeg = (Find(minus_sign) != -1); long nWhole = abs(atol((const char *)strWhole)); long nFract = abs(atol((const char *)strFraction)); // check for % and ? if (percent == szFormat[0]) { double d = atof(_str); d *= 100; GString strTemp; strTemp.Format("%f", d); nIdx = strTemp.Find('.'); strFraction = (nIdx == -1) ? strTemp._str : (const char *)strTemp.Left(nIdx); nWhole = atol((const char *)strFraction); nFract = 0; } if (permille == szFormat[0]) { double d = atof(_str); d *= 1000; GString strTemp; strTemp.Format("%f", d); nIdx = strTemp.Find('.'); strFraction = (nIdx == -1) ? strTemp._str : (const char *)strTemp.Left(nIdx); nWhole = atol((const char *)strFraction); nFract = 0; } // if the number is negative, get the negative pattern out of the format. // if a negative pattern doesn't exist, the minus_sign will be prepended // to the positive pattern. GString strFormat(szFormat); nIdx = strFormat.Find(pattern_separator); if (bIsNeg) { if (nIdx != -1) strFormat = strFormat.Mid(nIdx + 1); else strFormat.Format("%c%s", minus_sign, (const char *)strFormat); } else { if (nIdx != -1) strFormat = strFormat.Left(nIdx); } GString strFormatWhole(strFormat); GString strFormatDecimal('#', (short)1); // determine the number of digits per group int nGroup = 0; nIdx = strFormat.Find(','); if (nIdx != -1) { nIdx++; int nNext = strFormat.Find(',', nIdx); if (nNext == -1) nNext = strFormat.Find('.', nIdx); if (nNext == -1) nNext = strFormat.Length(); nGroup = (nNext - nIdx); } // determine the number of decimals to display int nDecimals = 0; nIdx = strFormat.Find('.'); if ((nIdx != -1) && (percent != szFormat[0]) && (permille != szFormat[0])) { if (nGroup) strFormatWhole = strFormat.Mid(nIdx - nGroup, nGroup); else strFormatWhole = strFormat.Left(nIdx); nIdx++; strFormatDecimal = strFormat.Mid(nIdx); nDecimals = (strFormat.Length() - nIdx); } // Format the whole part of the number int nCount = CountOf((const char *)strFormatWhole, zero_digit); strWhole.Format("%0ld", nWhole); if (strWhole.Length() < nCount) { GString temp(zero_digit, (short)(nCount - (short)strWhole.Length())); strWhole.Format("%s%s", (const char *)temp, (const char *)strWhole); } Empty(); // add all prefix characters nIdx = 0; const char *szP = (const char *)strFormat; while (*szP) { if (*szP == digit || *szP == zero_digit || *szP == decimal_separator || *szP == grouping_separator || *szP == percent || *szP == permille) break; szP++; nIdx++; } strFormat = strFormat.Left(nIdx); strFormat.MakeReverse(); int i, j; for (i = 0, j = strWhole.Length() - 1; j >= 0; j--, i++) { if ((nGroup) && (i == nGroup)) { *this += grouping_separator; i = 0; } *this += strWhole[j]; } *this += strFormat; MakeReverse(); if (nDecimals) { *this += decimal_separator; strFraction.Format("%ld", nFract); const char *szF1 = (const char *)strFormatDecimal; const char *szF2 = (const char *)strFraction; i = 0; while (*szF1) { if (*szF2) *this += *szF2; else if (*szF1 == zero_digit) *this += zero_digit; else if (*szF1 != digit) // add all sufix characters *this += *szF1; if (*szF2) szF2++; if (*szF1) szF1++; } } if (percent == szFormat[0]) *this += percent; if (permille == szFormat[0]) *this += permille; } } }