Code: Select all | Expand
// C:\FWH\SAMPLES\VIACEP02.PRG - SOMENTE XHABROUR POR ENQUANTO.
#include "Directry.ch"
#include "FiveWin.ch"
#include "hbstruct.ch" // Only xHarbour.
REQUEST HB_LANG_PT, HB_CODEPAGE_PTISO, HB_CODEPAGE_PT850
STATIC lTiraAcento := .T.
FUNCTION Main()
LOCAL cCep := "01330-000"
// FW_SetUnicode( .T. ) // NAO ADIANTOU
HB_LANGSELECT( 'PT' ) // Default language is now Portuguese
HB_SETCODEPAGE( "PT850" )
HB_CDPSELECT( "PTISO" )
// SET(_SET_CODEPAGE,"CP850")
// BASTA POR UM DIALOGO AQUI, PARA PESQUISAR O CEP.
ViaCep( cCep )
RETURN NIL
FUNCTION ViaCep( cCep )
LOCAL cLink := "http://viacep.com.br/ws/<<CEP>>/xml/"
LOCAL oOle, cXml, oCep, oXml, oTag// ,bError
#ifdef __XHARBOUR__
STRUCTURE oCep // "hbstruct.ch"
Member lFound INIT .F.
Member cNome, cCompl, cBair, cCid, cUf
Member cTipo
ENDSTRUCTURE
#else
// ISTO NAO FUNCIONA EM HARBOUR, NAO ENTENDI????
STRUCTURE oCep
Member lFound INIT .F.
Member cNome, cCompl, cBair, cCid, cUf
Member cTipo
ENDSTRUCTURE
#endif
IF Empty( cCep ); RETURN oCep; ENDIF
cCep := StrTran( cCep, ' ', '' )
IF Len( cCep ) < 8; RETURN oCep; ENDIF
cLink := StrTran( cLink, "<<CEP>>", cCep )
#ifdef __XHARBOUR__
Try
Try
oOle := CreateObject( 'Microsoft.XMLHTTP' )
Catch
oOle := CreateObject( 'MSXML2.XMLHTTP' )
End
oOle:Open("GET",cLink,.F.)
oOle:Send()
If oOle:STATUS != 200
BREAK
Endif
cXml := oOle:ResponseBody
oOle := nil
Catch
MsgInfo( 'Erro na Criação do Serviço' )
RETURN NIL
End
#else
Try
Try
oOle := win_OleCreateObject( 'MSXML2.XMLHTTP' )
Catch
oOle := win_OleCreateObject( 'Microsoft.XMLHTTP' )
End
oOle:Open("GET",cLink,.F.)
oOle:Send()
If oOle:STATUS != 200
BREAK
Endif
cXml := oOle:ResponseBody
oOle := nil
Catch
MsgInfo( 'Erro na Criação do Serviço! Com Harbour', 'Atenção!' )
RETURN NIL
End
#endif
// verifica se não encontrou o cep atraves tag "erro"
oXml := TXmlDocument():New(cXml)
oTag := oXml:findFirst("erro") //<erro>true</erro>
//Este erro so pega se o CEP tiver 8 digitos
If .NOT. Empty(oTag) .AND. oTag:cData == "true"
BREAK
Endif
// #ifdef __XHARBOUR__
oCep:lFound := .T. // NAO FUNCIONA COM HARBOUR
// #endif
oTag := oXml:findFirst("logradouro")
oCep:cNome := IIF(Empty(oTag),'',oTag:cData) //HtmlToOem(oTag:cData))
// oCep:cNome := HtmlToOeM(oCep:cNome)
// oCep:cNome := HB_Translate(oCep:cNome,"UTF8","PT850")
oCep:cNome := FW_UTF8PADCHAR(oCep:cNome,60)
oCep:cNome := Upper(oCep:cNome)
If At("AVENIDA",oCep:cNome) > 0
oCep:cTipo := "AV:"
oCep:cNome := StrTran(Upper(oCep:cNome),"AVENIDA ",'')
Endif
If At("RUA",oCep:cNome) > 0
oCep:cTipo := "RUA"
oCep:cNome := StrTran(Upper(oCep:cNome),"RUA ",'')
Endif
If At("PRAÇA",oCep:cNome) > 0
oCep:cTipo := "PC:"
oCep:cNome := StrTran(Upper(oCep:cNome),"PRAÇA ",'')
Endif
If At("TRAVESSA",oCep:cNome) > 0
oCep:cTipo := "TV:"
oCep:cNome := StrTran(Upper(oCep:cNome),"TRAVESSA ",'')
Endif
If At("ALAMEDA",oCep:cNome) > 0
oCep:cTipo := "AL:"
oCep:cNome := StrTran(Upper(oCep:cNome),"ALAMEDA ",'')
Endif
// ASSIM NAO FUNCIONA
// oCep:cNome := oCep:cNome:RemoveAcentos():Upper()
oCep:cNome := RemoveAcentos( oCep:cNome )
// oCep:cNome := SubStr(oCep:cNome,1,40) //ERRO TABCEP 60 E NO CADASTRO 40
// oTag := oXml:findFirst("complemento")
// oCep:cCompl := IIF(Empty(oTag),'',oTag:cData) //UPPER DA ERRO SE VAZIO
oTag := oXml:findFirst("bairro")
oCep:cBair := IIF(Empty(oTag),'',oTag:cData)
oCep:cBair := FW_UTF8PADCHAR(oCep:cBair,30)
oCep:cBair := Upper(oCep:cBair)
oCep:cBair := PadL(Upper(oCep:cBair),30)
oCep:cBair := PadL(Upper(oCep:cBair),30)
// oCep:cBair := oCep:cBair:RemoveAcentos():Upper()
oCep:cBair := RemoveAcentos( oCep:cBair )
// oCep:cBair := Upper(oCep:cBair)
// Alert(Asc(SubStr(oCep:cBair,Len(oCep:cBair)-1)))
oCep:cCompl := IIF(Empty(oTag),'',oTag:cData)
oCep:cCompl:= UPPER( oCep:cCompl ) // AGORA SIM, PERFEITO! POHHA. KKKK
oTag := oXml:findFirst("localidade")
oCep:cCid := IIF(Empty(oTag),'',oTag:cData)
oCep:cCid := FW_UTF8PADCHAR( oCep:cCid,30)
oCep:cCid := Upper(oCep:cCid)
oCep:cCid := PadL(oCep:cCid,30)
// ASSIM NAO FUNCIONA - NAO ENTENDI ESSA LOGICA.
// oCep:cCid := oCep:cCid:RemoveAcentos():Upper() // ????
oCep:cCid := RemoveAcentos( oCep:cCid ) // NAO USAR UPPER JUMA!
oTag := oXml:findFirst("uf")
oCep:cUf := IIF(Empty(oTag),Space(2),oTag:cData)
xBrowse( oCep )
RETURN oCep
FUNCTION RemoveAcentos(cText)
cText := strtran(cText,'á','a')
cText := strtran(cText,'é','e')
cText := strtran(cText,'í','i')
cText := strtran(cText,'ó','o')
cText := strtran(cText,'ú','u')
cText := strtran(cText,'Á','A')
cText := strtran(cText,'à','A') // 224
cText := strtran(cText,'á','A') // 225
cText := strtran(cText,'â','A') // 226
cText := strtran(cText,'ã','A') // 227
cText := strtran(cText,'ä','A') // 228
cText := strtran(cText,'å','A') // 229
cText := strtran(cText,"Æ",'A')
cText := strtran(cText,"Â",'A')
cText := strtran(cText,"µ",'A')
cText := strtran(cText,"…",'A')
cText := strtran(cText,"·",'A')
cText := strtran(cText,"ƒ",'A') // 131 ?
cText := strtran(cText,"„",'A') // 132 ?
cText := strtran(cText,"…",'A') // 133 ?
cText := strtran(cText,"†",'A') // 134 ?
cText := strtran(cText,'À','A')
cText := strtran(cText,'Ã','A') // AQUI - SÆO PAULO
cText := strtran(cText,"£",'') // Ã £ AQUI METO UM BRANCO - YES!
cText := strtran(cText,'É','E')
cText := strtran(cText,'Í','I')
cText := strtran(cText,'Ó','O')
cText := strtran(cText,'Ú','U')
cText := strtran(cText,'Ô','O')
cText := strtran(cText,'Ò','O')
cText := strtran(cText,'Ö','O')
cText := strtran(cText,'ä','a')
cText := strtran(cText,'â','a')
cText := strtran(cText,'ã','a')
cText := strtran(cText,'å','a')
cText := strtran(cText,'ê','a')
cText := strtran(cText,'ë','a')
cText := strtran(cText,'ç','c')
cText := strtran(cText,'Ç','C')
RETURN( cText )
// FIN / END
Regards, saludos.