Skip to content

Instantly share code, notes, and snippets.

@natebenes
Created June 4, 2010 20:22
Show Gist options
  • Save natebenes/425898 to your computer and use it in GitHub Desktop.
Save natebenes/425898 to your computer and use it in GitHub Desktop.
module jsonFx
implicit none
contains
subroutine getFileNames(names, counter)
implicit none
integer, intent(out) :: counter
character(len=512), dimension(500000), INTENT(OUT) :: names
character(len=512) :: tempStr = "", pwd="", blankage
integer :: openErr, readErr, strLen, k
counter = 0
call system("ls -R > lsAll.out")
open(file="lsAll.out", unit=55, action="read", iostat=openErr)
do k=1,512
blankage(k:k) = " "
end do
do
read(55,"(A)",iostat=readErr) tempStr
if(readErr /= 0) then
if(counter<10) WRITE(*,*) "can't open"
exit
end if
if(tempStr(1:2) == "./") then ! its a dir
pwd = TRIM(tempStr)
else
counter = counter + 1
if(len_trim(trim(pwd)//trim(tempStr))<512) then
names(counter) = blankage
names(counter) = trim(pwd)//trim(tempStr)
write(*,*) trim(names(counter))
end if
end if
tempStr = blankage
pwd = blankage
end do
close(unit=55)
call system("rm lsAll.out")
end subroutine
subroutine pushJson(fileName)
implicit none
character(len=16), intent(in) :: fileName
character(len=10) :: outputName
character(len=512), dimension(20000,2) :: jsonObj
integer :: useLen, counter = 0,i,k, openErr, openoutErr, strStr, strEnd, strFin, readErr, strDet, equals
character(len=512) :: tempStr = "", pwd="", blankage, key, value
character, parameter :: quote = '"'
useLen = LEN_TRIM(fileName) - 5
outputName(6:10) = ".json"
outputName(1:5) = fileName(1:useLen)
if(((index(TRIM(fileName),":") == 0) .AND. (LEN_TRIM(fileName) > 6)) .AND. &
((fileName(1:2) /= "f2") .AND. (fileName(1:5) /= "a.out"))) then
open(file=TRIM(fileName), unit=55, action="read", iostat=openErr)
open(file=outputName, unit=555, action="write", iostat=openoutErr)
do k=1,512
blankage(k:k) = " "
end do
221 format(A)
write(*,*) TRIM(filename)
if((openErr == 0) .AND. (openoutErr == 0)) then
write(*,*) "win"
do
tempStr = blankage
read(55,221,iostat=readErr) tempStr
if(readErr /= 0) exit
strDet = index(tempStr, ":")
equals = index(tempStr, "=")
if ((strDet /= 0) .AND. (equals == 0)) then ! make a new key
counter = counter + 1
jsonObj(counter,1) = blankage
jsonObj(counter,2) = blankage
key = blankage
value = blankage
strStr = strDet + 1
StrEnd = strDet - 1
strFin = LEN_TRIM(tempStr)
key = tempStr(1:strEnd)
value = tempStr(strStr:strFin)
jsonObj(counter,1) = trim(key)
jsonObj(counter,2) = trim(value)
write(*,*) trim(key)
write(*,*) trim(value)
else
write(*,*) "else"
if(equals ==0) then
value = TRIM(value)//" "//TRIM(tempStr)
jsonObj(counter,2) = TRIM(value)
end if
end if
end do
! create json file
write(555,*) "{"
write(555,*) '"_id":"'//trim(fileName(1:useLen))//'"'
do i=1,counter
if(counter == i) then
write(555,*) quote//trim(jsonObj(i,1))//quote//":"//quote//trim(jsonObj(i,2))//quote
else
write(555,*) quote//trim(jsonObj(i,1))//quote//":"//quote//trim(jsonObj(i,2))//quote//","
end if
end do
write(555,*) "}"
else
write(*,*) "failed to open",fileName
stop
end if
close(unit=55)
close(unit=555)
end if
end subroutine
end module jsonFx
program testSuite
use jsonFx
implicit none
integer :: count, knownLen=0,i, incr, readErr, j, openErr
character(len=512), dimension(500000) :: AllNames, records, known
call getFileNames(AllNames, count)
DO i=1,count
call pushJson(TRIM(AllNames(i)))
END DO
! DO i=j,knownLen
! WRITE(*,*) known(j)
! END DO
WRITE(*,*) count
end program testSuite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment