C2F translation of command line arguments 
Author Message
 C2F translation of command line arguments

Below is a test case that illustrates how easy it is to access command line
arguments in C.

===============
void checkargs( int argc, char *argv[]);

main( int argc, char *argv[] )
{
  int n;

  for(n=0;n<argc;n++)
    printf("arg %d = %s\n",n,argv[n]);
  printf("%c\n", argv[0][0]);

  checkargs(argc, argv);

Quote:
}

void checkargs( int argc, char *argv[])
{
  int n;

  for(n=0;n<argc;n++)
    printf("arg %d = %s\n",n,argv[n]);
  printf("%c\n", argv[0][0]);

Quote:
}

====================

In contrast, the new  C2F F90 translation of above C source  (see below)
has to jump thru hoops to access 2 dimension character data..

Anyone have a easier way than my method below?

! --------------------------------------------------
MODULE TEST_1         ! global declarations
! --------------------------------------------------
! INCLUDE 'C2F.FI'    commented out to allow reader to compile
! INCLUDE 'C2F.FD'

INTERFACE     ! extracted from C2F.FI to allow reader to compile
FUNCTION NARGS()  RESULT(argc)
  INTEGER :: argc
END FUNCTION

SUBROUTINE GETARG(n, buffer, status)
  INTEGER(2) :: n
  CHARACTER (LEN=*) :: buffer
  INTEGER(2), OPTIONAL :: status
END SUBROUTINE
END INTERFACE

END MODULE
! --------------------------------------------------
PROGRAM TEST
! --------------------------------------------------
USE TEST_1
IMPLICIT NONE
! - - - local declarations - - -
  INTEGER :: n,argc
  CHARACTER (LEN=30) :: argv(20)
  CHARACTER :: argv2(30,20); EQUIVALENCE (argv2,argv)

! - - - begin - - -
  argc = NARGS()       ! #args from command line
  DO n = 1,20
     CALL GETARG(n-1, argv(n))  ! get command line arguments
     IF (n == argc) EXIT
  END DO
  DO n = 0,argc-1
    WRITE (*,901) "arg ",n,"  = ",argv(n+1)
  END DO
  WRITE (*,902) "1st ch = ",argv2(0+1,0+1)
  CALL checkargs(argc, argv,argv2,30,20)

901 FORMAT (A,I0,A,A)
902 FORMAT (99A)
END PROGRAM

! --------------------------------------------------
SUBROUTINE checkargs(argc,argv,argv2,$1,$2)
! --------------------------------------------------
USE TEST_1
IMPLICIT NONE
! - - - arg types - - -
  INTEGER :: argc,$1,$2
  CHARACTER (LEN=*) :: argv(*)
  CHARACTER :: argv2($1,$2)
! - - - local declarations - - -
  INTEGER :: n

! - - - begin - - -
  DO n = 0,argc-1
    WRITE (*,901) "arg ",n,"  = ",argv(n+1)
  END DO
  WRITE (*,902) "1st ch = ",argv2(0+1,0+1)

901 FORMAT (A,I0,A,A)
902 FORMAT (99A)
END SUBROUTINE
! INCLUDE 'C2F_LIB.F90'   commented out to allow reader compile
!  0 Errors detected



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. C2F translation of command line

2. command environments and command line arguments

3. Fortran command to read command line argument?

4. recent C2F translations

5. another C2F translation

6. C2F another nasty translation problem

7. another C2F translation problem solution

8. C2F another pointer translation problem (using DVF).

9. C2F and function pointer arguments

10. J hack capturing command line arguments

11. Command Line Arguments

12. Command line argument

 

 
Powered by phpBB® Forum Software