PROCEDURE SEPARTEXT(cFrase,nAncho,xaMatriz,xnMinDef)
*-------------------------------------------------------------------------------------
local fi,qi,nit,lenfra,apunto,di,gaparte,ngap,xi,nis,;
nspacios,nSaltos,sbFrase,xi_,nSepar
*
* xnMinDef se utiliza cuando se necesita un minimo de items en
* la matriz pasada por referencia. Completa con " "
*
if Vartype(xnMindef)#"N"
xnMindef=0
endif
if vartype(nAncho)#"N"
nAncho=70
endif
cFrase=alltrim(cFrase)
sbFrase=cFrase
* Si la frase es Vacio, vuelve de inmediato
if empty(cfrase)
if xnMindef=0
Dimension xaMatriz(1)
else
Dimension xaMatriz(xnMindef)
endif
Store "" to xaMatriz
return
else
*Revisar los puntos
npuntos=Occurs(".",cFrase)
if npuntos#0
oFrase=""
nlen=len(cFrase)
dimension gapuntos(1)
gpase=0
ni=0
do while .t.
ni=ni+1
if ni=nlen
exit
endif
if Substr(cFrase,ni,1)="."
cExa=Substr(cFrase,ni+1,1)
if !INlist(cExa,Chr(32),".")
cFrase=Substr(cFrase,1,ni)+" "+Substr(cFrase,ni+1)
nlen=nlen+1
endif
endif
enddo
endif
endif
* Calcula cantidad de saltos y divide en gaParte
nspacios=Occurs(Chr(32),cFrase)
ntrece=Occurs(chr(13),cfrase)
nsaltos=Occurs(chr(10),cFrase)
ngap=iif(nSaltos<2,1,nSaltos)
* Antes decia ngap=iif(nsaltos<2,1,nTrece)
nis=0
Dimension gaParte(ngap)
if nGap=1
gaparte[1]=cfrase
else
xcFrase=cFrase
do while .t.
xngap=At(Chr(10),xcFrase)
if xnGap=0
exit
endif
nis=nis+1
dimension gaParte(nis)
gaParte[nis]=Left(xcFrase,xnGap-1)
xcFrase=Substr(xcFrase,xnGap+1)
enddo
if !empty(xcFrase)
nis=nis+1
dimension gaparte(nis)
gaParte[nis]=alltrim(xcFrase)
endif
endif
for xi_=1 to alen(gaParte)
gaparte[xi_]=VmChar(gaparte[xi_])
if At(chr(13),gaParte[xi_])>0
gaParte[xi_]=strtran(gaParte[xi_],chr(13),"")
endif
if At(chr(19),gaparte[xi_])>0
gaparte[xi_]=Strtran(gaparte[xi_],chr(19))
endif
next
nSumPerdi=0
Dimension xaMatriz(1)
nit=0
for xi=1 to alen(gaParte)
sbFrase=alltrim(gaparte[xi])
nspacios=Occurs(Chr(32),sbFrase)
Lenfra=Len(sbFrase)
IF LENFRA<=nAncho
nit=nit+1
dimension xaMatriz(nit)
xaMatriz[nit]=sbFrase
else
if nSpacios>0
*aPunto[] recoge las posiciones de aparición de Chr(32)
Dimension aPunto(1)
di=0
for qi=1 to len(sbFrase)
npun=At(Chr(32),sbFrase,qi)
if npun=0
exit
endif
di=di+1
Dimension aPunto(di)
aPunto[di]=npun
next
nLast=1
cRest=sbFrase
nSumlen=0
nParte=nAncho
For di=1 to alen(aPunto)
keBodrio=""
nRemp=aPunto[di]
if aPunto[di]>=nParte
cProye=Alltrim(Substr(sbFrase,nLast,aPunto[di]-nLast+1))
nLastResta=0
do while len(cProye)>nAncho
nLastResta=nLastResta+1
cProye=Alltrim(Substr(sbFrase,nLast,aPunto[di-nLastREsta]-nLast+1))
enddo
nit=nit+1
Dimension xaMatriz(nit)
xaMatriz[Nit]=cProye
nSumLen=nSumLen+Len(xaMatriz[nit])
nPerdido=nAncho-len(xaMatriz[nit])
nSumPerdi=nSumperdi+nPerdido
nLast=aPunto[di-nLastResta]+1
nParte=nSumLen+nAncho+nSumPerdi
endif
* Ultima parte
if di=alen(apunto)
nit=nit+1
Dimension xaMatriz(nit)
xaMatriz[Nit]=Alltrim(Substr(sbFrase,nLast,lenfra-nLast+1))
endif
next
else
nSta=int(len(sbfrase)/nAncho)
if Mod(Len(sbfrase),nAncho)#0
nSta=nsta+1
endif
xSta=1
zbFrase=sbfrase
for fi=1 to nSta
nit=nit+1
dimension xaMatriz(nit)
zbFrase=Substr(zbFrase,xSta)
xaMatriz[nit]=Left(zbfrase,nAncho)
xSta=xSta+nAncho
next
endif
endif
next
if xnMindef>nit
Dimension xaMatriz(xnMindef)
for jo=Nit+1 to xnMindef
xaMatriz[jo]=" "
next
endif
nSepar=ALEN(xaMatriz)
IF nSepar=1.and.Empty(xaMatriz[1])
nSepar=0
ENDIF
RETURN nSepar
endproc