I am trying to write a golang program which will be able to read from a file of the following type of data
#define __LPM_classic__(addr) (__extension__({ uint16_t __addr16 = (uint16_t)(addr); uint8_t __result; __asm__ __volatile__ ( "lpm" "
\t" "mov %0, r0" "
\t" : "=r" (__result) : "z" (__addr16) : "r0" ); __result; }))
#define PRIXFAST32 "lX"
#define INT0 0
#define INT1 1
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
#define B00010000 16
#define B11101000 232
#define B11101001 233
#define PRADC 0
#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
#define Arduino_h
#define sq(x) ((x)*(x))
#define B01000100 68
#define B01000101 69
(For anyone wondering, yes this is an output from a Clang pre-processor. The reasons for me doing this is pretty complicated, and I don't want to trouble people with un-necessary details)
I need to write a golang program, which will read through this file, and give me the defined value of a string. For example
func getDefinedValue(filepath string,defineName string) string{
//Need help with this part
}
So if I run the function in the following manner
Value :=getDefinedValue("preprocessorOutput.txt","PRADC")
Then the variable value should hold 0, as per the example file above.
I have tried using fmt.Fscnaf in the following manner
file, err := os.Open("preprocessorOutput.txt")
Value :=""
readLine :=""
defName :=""
var errr error
for (errr == nil) && (len(Value)==0){
ret,err :=fmt.Fscanf(file,"#define %s %s",&defName,&readLine)
errr=err
fmt.Println("This is returned ",ret, " and this is the defName ",defName, " And this is the value ",readLine," and this is the error",err)
if(ret <1){
continue
}
//I planned to process the defName and readline to get the actual value here, as per which defName I want to get.
}
(Don't sweat the details, this is my attempt at an MCVE) which allows me to use a format string like CLang's scanf allows, but it fails on the 1st line of the example and throws an error on the 2nd line.
This is my first-day using golang, and I would probably kill to have a package do this work for me.