C Assignment: Parsing a text file

C Assignment: Parsing a text file October 19, 2009 Abstract In this experiment, you will begin to write a program that will parse a simple spice contr...
Author: Gilbert Snow
62 downloads 0 Views 110KB Size
C Assignment: Parsing a text file October 19, 2009 Abstract In this experiment, you will begin to write a program that will parse a simple spice control file, construct the circuit to be simulated, and solve the circuit equations. The circuit has only simple devices. The full code will be developed over a few weeks. This week we read in a control file, analyse it and save the information in a linked list.

1

Introduction

A spice netlist consists of lines of the following forms:

n1 n2 type value n1 n2 n3 n4 type value where n1, n2, n3 and n4 are symbolic names representing nodes, type is either “R”, “L”, “C”, “I” or “V” for the first type of line, representing impedences or sources. type is one of “VCVS”, “VCCS”, “CCVS”, “CCCS” for the second type of line. Value is the value of the element in Ω, H, F, Amps or Volts, respectively. Each impedence line in the file translates into an equation: Vn1 −Vn2 Vn1 −Vn2 In1,n2

= In1,n2 Rn1,n2 dIn1,n2 = Ln1,n2 dt d (Vn1 −Vn2 ) = Cn1,n2 dt

Each source line in the file becomes Vn1 −Vn2 In1,n2

= εn1,n2 = In1,n2

Vn1 −Vn2

= E (Vn3 −Vn4 )

Vn1 −Vn2

= FIn3,n4

In1,n2

= G (Vn3 −Vn4 )

In1,n2

= HIn3,n4 1

2 2.1

Coding Details Getting the File Name

The file name is given on the command line. We have to open it for reading. Commandline arguments in C are passed to the program through the ARGV pointer. Another variable is also passed to the program, namely argc, It is an int, and contains the number of arguments. Let us see how this works in a simple program. 2

heg1 2i≡ #include #include int main(int argc,char **argv); // declare main int main(int argc,char **argv){ float ans=0.0; // holds sum of values int i; // index variable for( i=1 ; i