Conditional File Name
14 posts
• Page 1 of 2 • 1, 2
Conditional File Name
Hello,
Can I put the following in my code somewhere and have it record a file named mac_01.gmk?
Can I put the following in my code somewhere and have it record a file named mac_01.gmk?
- Code: Select all
uint32 grp = 1;
macro_record("mac_0" + grp + ".gmk");
-
USER101 - Sergeant Major
- Posts: 100
- Joined: Mon Dec 03, 2018 5:45 pm
Re: Conditional File Name
Not getting errors but when I run the following it doesn't seem to work. Not seeing it in macro explorer either.
macro_run("mac_0" + grp + ".gmk");
macro_run("mac_0" + grp + ".gmk");
-
USER101 - Sergeant Major
- Posts: 100
- Joined: Mon Dec 03, 2018 5:45 pm
Re: Conditional File Name
There are no string functions available, so this isn't going to work.
You may be able to merge them on your own.
Example code , doesn't check if the merged string fits into its destination and no idea if this is safe to use, but seems to be working:
Output:
You may be able to merge them on your own.
Example code , doesn't check if the merged string fits into its destination and no idea if this is safe to use, but seems to be working:
- Code: Select all
uint32 grp = 1;
char MacName[12];
init {
// create a name of src1, number, fill missing digits with zero (four = 0001), append src2, all into dst/MacName
NameCreate("mac_",grp,4,".gmk",&MacName);
}
void NameCreate(char *src1, uint32 grp, uint8 digits, char *src2, char *dst) {
uint8 i=0;
i=NameChar(i,src1,dst);
i=NameInt(i,grp,digits,dst);
i=NameChar(i,src2,dst);
printf("macname/dst:");
printf(dst);
}
// add string
uint8 NameChar(uint8 index, char *src, char *dst) {
uint8 i;
for (i=0;src[i]!='\0';i++) {
printf("char: %c",src[i]); // printf current char
//
dst[index]=src[i];
index++;
}
dst[index]='\0';
return index;
}
// number to char conversion and add to string
uint8 NameInt(uint8 index, int32 Num, uint8 digits, char *dst) {
uint8 i=0;
uint8 iThis;
// number of digits
uint8 iDigits=1;
while(Num/pow(10,iDigits)) iDigits++;
// fill missing number of digits with 0
for (i=iDigits;i<digits;i++) {
dst[index]='0';
printf("char: %c",'0'); // printf current char
index++;
}
for (i=0;i<iDigits;i++) {
iThis=Num/pow(10,(iDigits-(i+1)));
Num=Num-iThis*pow(10,iDigits-i-1);
dst[index]=iThis+48;
printf("char: %c",iThis+48); // printf current char
index++;
}
dst[index]='\0';
return index;
}
Output:
- Code: Select all
GVM Output: char: m
GVM Output: char: a
GVM Output: char: c
GVM Output: char: _
GVM Output: char: 0
GVM Output: char: 0
GVM Output: char: 0
GVM Output: char: 1
GVM Output: char: .
GVM Output: char: g
GVM Output: char: m
GVM Output: char: k
GVM Output: macname/dst:
GVM Output: mac_0001.gmk
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
Re: Conditional File Name
If I understand correctly
Should create and record to the file mac_0001.gmk and that's all based on the current grp variable?
- Code: Select all
macro_record(dst);
Should create and record to the file mac_0001.gmk and that's all based on the current grp variable?
-
USER101 - Sergeant Major
- Posts: 100
- Joined: Mon Dec 03, 2018 5:45 pm
Re: Conditional File Name
You have to use:
after you called
as this write the name into MacName.
When grp is 1 it will result in "mac_0001.gmk"
When grp is 123 it will result in "mac_0123.gmk"
This code record 10 seconds and creates the file mac_0001.gmk:
If you have trouble using this PM me your code and I'll try to put that into it.
- Code: Select all
macro_record(&MacName);
after you called
- Code: Select all
NameCreate("mac_",grp,4,".gmk",&MacName);
as this write the name into MacName.
When grp is 1 it will result in "mac_0001.gmk"
When grp is 123 it will result in "mac_0123.gmk"
This code record 10 seconds and creates the file mac_0001.gmk:
- Code: Select all
uint32 grp = 1;
char MacName[12];
init {
// create a name of src1, number, fill missing digits with zero (four = 0001), append src2
NameCreate("mac_",grp,4,".gmk",&MacName);
macro_rec(&MacName); // start recording
}
main {
if ( macro_time() > 10000 ) {
macro_stop();
printf("stopped recording");
}
}
void NameCreate(char *src1, uint32 grp, uint8 digits, char *src2, char *dst) {
uint8 i=0;
i=NameChar(i,src1,dst);
i=NameInt(i,grp,digits,dst);
i=NameChar(i,src2,dst);
printf("macname/dst:");
printf(dst);
}
// add string
uint8 NameChar(uint8 index, char *src, char *dst) {
uint8 i;
for (i=0;src[i]!='\0';i++) {
printf("char: %c",src[i]); // printf current char
//
dst[index]=src[i];
index++;
}
dst[index]='\0';
return index;
}
// number to char conversion and add to string
uint8 NameInt(uint8 index, int32 Num, uint8 digits, char *dst) {
uint8 i=0;
uint8 iThis;
// number of digits
uint8 iDigits=1;
while(Num/pow(10,iDigits)) iDigits++;
// fill missing number of digits with 0
for (i=iDigits;i<digits;i++) {
dst[index]='0';
printf("char: %c",'0'); // printf current char
index++;
}
for (i=0;i<iDigits;i++) {
iThis=Num/pow(10,(iDigits-(i+1)));
Num=Num-iThis*pow(10,iDigits-i-1);
dst[index]=iThis+48;
printf("char: %c",iThis+48); // printf current char
index++;
}
dst[index]='\0';
return index;
}
If you have trouble using this PM me your code and I'll try to put that into it.
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
Re: Conditional File Name
That worked perfect. Thank You!!!
-
USER101 - Sergeant Major
- Posts: 100
- Joined: Mon Dec 03, 2018 5:45 pm
Re: Conditional File Name
Scachi wrote:You have to use:
- Code: Select all
macro_record(&MacName);
after you called
- Code: Select all
NameCreate("mac_",grp,4,".gmk",&MacName);
as this write the name into MacName.
When grp is 1 it will result in "mac_0001.gmk"
When grp is 123 it will result in "mac_0123.gmk"
This code record 10 seconds and creates the file mac_0001.gmk:
- Code: Select all
uint32 grp = 1;
char MacName[12];
init {
// create a name of src1, number, fill missing digits with zero (four = 0001), append src2
NameCreate("mac_",grp,4,".gmk",&MacName);
macro_rec(&MacName); // start recording
}
main {
if ( macro_time() > 10000 ) {
macro_stop();
printf("stopped recording");
}
}
void NameCreate(char *src1, uint32 grp, uint8 digits, char *src2, char *dst) {
uint8 i=0;
i=NameChar(i,src1,dst);
i=NameInt(i,grp,digits,dst);
i=NameChar(i,src2,dst);
printf("macname/dst:");
printf(dst);
}
// add string
uint8 NameChar(uint8 index, char *src, char *dst) {
uint8 i;
for (i=0;src[i]!='\0';i++) {
printf("char: %c",src[i]); // printf current char
//
dst[index]=src[i];
index++;
}
dst[index]='\0';
return index;
}
// number to char conversion and add to string
uint8 NameInt(uint8 index, int32 Num, uint8 digits, char *dst) {
uint8 i=0;
uint8 iThis;
// number of digits
uint8 iDigits=1;
while(Num/pow(10,iDigits)) iDigits++;
// fill missing number of digits with 0
for (i=iDigits;i<digits;i++) {
dst[index]='0';
printf("char: %c",'0'); // printf current char
index++;
}
for (i=0;i<iDigits;i++) {
iThis=Num/pow(10,(iDigits-(i+1)));
Num=Num-iThis*pow(10,iDigits-i-1);
dst[index]=iThis+48;
printf("char: %c",iThis+48); // printf current char
index++;
}
dst[index]='\0';
return index;
}
If you have trouble using this PM me your code and I'll try to put that into it.
Quick question about your code. I see that your void statement and some other stuff are below your main. Is that because you are treating them like a function? Does that stuff need to be below the main and combo section for it to work properly? Mine is working baring a change to the the variable name but I have it after combo as well.
-
USER101 - Sergeant Major
- Posts: 100
- Joined: Mon Dec 03, 2018 5:45 pm
Re: Conditional File Name
Yes, they are functions.
You can mix the order with gpc2 / Titan Two. The T1 wasn't that flexible.
There is still an order on how they will be started when using all at the same time (init, same main loop run/same function):
Functions first, in the order how you start them, not how the are declared.
Combos in the order in how they are declared, not in the order how they are started in the main loop.
I call the cSecond combo first, it still runs the cFirst combo first as I have declared them in this order.
Output:
You can mix the order with gpc2 / Titan Two. The T1 wasn't that flexible.
There is still an order on how they will be started when using all at the same time (init, same main loop run/same function):
Functions first, in the order how you start them, not how the are declared.
Combos in the order in how they are declared, not in the order how they are started in the main loop.
I call the cSecond combo first, it still runs the cFirst combo first as I have declared them in this order.
- Code: Select all
init {
// order
printf("init entering"); // 1.
fSecond(); // 2.
fFirst(); // 3.
combo_run(cSecond); // 6.
combo_run(cFirst); // 5.
printf("init leaving"); // 4.
}
void fFirst() {
printf("function First");
}
void fSecond() {
printf("function Second");
}
void fThird() {
printf("function Third run by combo Second"); // 8.
}
combo cFirst {
printf("combo cFirst"); wait(0); // 5.
fFourth(); // 7.
}
combo cSecond {
printf("combo cSecond"); wait(0); // 6.
fThird(); // 8.
}
void fFourth() {
printf("function Fourth run by combo First"); // 7.
}
Output:
- Code: Select all
GVM Output: init entering
GVM Output: function Second
GVM Output: function First
GVM Output: init leaving
GVM Output: combo cFirst
GVM Output: combo cSecond
GVM Output: function Fourth run by combo First
GVM Output: function Third run by combo Second
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
Re: Conditional File Name
Thanks for the explanation, I have been reading both the T1 and T2 documentation so I can better tell when each type of code is being used in examples.
So I decided to try and make the entire name conditional.
Just trying to wrap my head around how you are composing the string. I made individual single digit variables for the first 4 digits and the last 4 digits dedicated to a variable from 0001-9999.. This compiles and runs but the output is not what I expected..
It is supposed to be "F1RA0001.gmk"
actual output/
GVM Output: XZ\
0001.gmk
So I decided to try and make the entire name conditional.
Just trying to wrap my head around how you are composing the string. I made individual single digit variables for the first 4 digits and the last 4 digits dedicated to a variable from 0001-9999.. This compiles and runs but the output is not what I expected..
It is supposed to be "F1RA0001.gmk"
actual output/
GVM Output: XZ\
0001.gmk
- Code: Select all
#pragma METAINFO("<author_name>", 1, 0, "")
#include "ColorLED.gph"
#include <xb1.gph>
#ifndef XB1_GPH_
#define XB1_GPH_
#define XB1_GUIDE BUTTON_1
#define XB1_VIEW BUTTON_2
#define XB1_MENU BUTTON_3
#define XB1_RB BUTTON_4
#define XB1_RT BUTTON_5
#define XB1_RS BUTTON_6
#define XB1_LB BUTTON_7
#define XB1_LT BUTTON_8
#define XB1_LS BUTTON_9
#define XB1_UP BUTTON_10
#define XB1_DOWN BUTTON_11
#define XB1_LEFT BUTTON_12
#define XB1_RIGHT BUTTON_13
#define XB1_Y BUTTON_14
#define XB1_B BUTTON_15
#define XB1_A BUTTON_16
#define XB1_X BUTTON_17
#define XB1_P1 BUTTON_18
#define XB1_P2 BUTTON_19
#define XB1_P3 BUTTON_20
#define XB1_P4 BUTTON_21
#define XB1_RX STICK_1_X
#define XB1_RY STICK_1_Y
#define XB1_LX STICK_2_X
#define XB1_LY STICK_2_Y
#endif /* XB1_GPH_ */
#define irand(a,b) (((int)(rand() * (fix32)(b+1 - a))) + a)
bool activity(){ // Checks if any buttons I/O, (1 thru 24) are active.
int i = 25;
while(i--){
if(is_active(i)) return TRUE;
}
return FALSE;
}
bool sleep = FALSE;
uint8 com;
uint32 sleep_timer;
uint32 grp = 1;// up to four digit number
uint32 mode = "F";// going to be a single letter
uint32 group = 1; // going to be a number from 1-9
uint32 type = "R";// going to be a single letter
uint32 set = "A"; // going to be a single letter
char MacName[12];
init {
// create a name of src1, number, fill missing digits with zero (four = 0001), append src2
NameCreate(mode,group,type,set,grp,4,".gmk",&MacName);
}
main {
if(activity()){ // Check if any button is being pressed.
printf(&MacName);}
}
void NameCreate(uint8 mode, uint8 group, uint8 type, uint8 set, uint32 grp, uint8 digits, char *src2, char *dst) {
uint8 i=0;
i=NameMode(i,mode,dst);
i=NameGroup(i,group,dst);
i=NameType(i,type,dst);
i=NameSet(i,set,dst);
i=NameInt(i,grp,digits,dst);
i=NameChar(i,src2,dst);
printf("macname/dst:");
printf(dst);
}
// add string
uint8 NameChar(uint8 index, char *src, char *dst) {
uint8 i;
for (i=0;src[i]!='\0';i++) {
printf("char: %c",src[i]); // printf current char
//
dst[index]=src[i];
index++;
}
dst[index]='\0';
return index;
}
uint8 NameMode(uint8 index, char mode, char *dst) {
uint8 i;
for (i=0;mode[i]!='\0';i++) {
printf("char: %c",mode[i]); // printf current char
//
dst[index]=mode[i];
index++;
}
dst[index]='\0';
return index;
}
uint8 NameSet(uint8 index, char set, char *dst) {
uint8 i;
for (i=0;set[i]!='\0';i++) {
printf("char: %c",set[i]); // printf current char
//
dst[index]=set[i];
index++;
}
dst[index]='\0';
return index;
}
uint8 NameGroup(uint8 index, char group, char *dst) {
uint8 i;
for (i=0;group[i]!='\0';i++) {
printf("char: %c",group[i]); // printf current char
//
dst[index]=group[i];
index++;
}
dst[index]='\0';
return index;
}
uint8 NameType(uint8 index, char type, char *dst) {
uint8 i;
for (i=0;type[i]!='\0';i++) {
printf("char: %c",type[i]); // printf current char
//
dst[index]=type[i];
index++;
}
dst[index]='\0';
return index;
}
// number to char conversion and add to string
uint8 NameInt(uint8 index, int32 Num, uint8 digits, char *dst) {
uint8 i=0;
uint8 iThis;
// number of digits
uint8 iDigits=1;
while(Num/pow(10,iDigits)) iDigits++;
// fill missing number of digits with 0
for (i=iDigits;i<digits;i++) {
dst[index]='0';
printf("char: %c",'0'); // printf current char
index++;
}
for (i=0;i<iDigits;i++) {
iThis=Num/pow(10,(iDigits-(i+1)));
Num=Num-iThis*pow(10,iDigits-i-1);
dst[index]=iThis+48;
printf("char: %c",iThis+48); // printf current char
index++;
}
dst[index]='\0';
return index;
}
-
USER101 - Sergeant Major
- Posts: 100
- Joined: Mon Dec 03, 2018 5:45 pm
Re: Conditional File Name
I have uploaded a header file for easier usage and example code to the Online Resource, search for "StrCreate"
Here is some information about it: viewtopic.php?f=23&t=11154&p=76624#p76624
You don't want to use a separate function for each segment of your file name, you may only need a different/additional function when the data type of variable is a different one.
The header file provides three different functions for adding: char*, char, uint32
Examples to recognize the different types used:
char *stringlike = ".gmk";
char singlechar = 'c';
uint32 number = 1421;
You may need to read some general information about functions and data types to get a better understanding of it and their usage. I am not very good at explaining stuff like that.
Here is some information about it: viewtopic.php?f=23&t=11154&p=76624#p76624
You don't want to use a separate function for each segment of your file name, you may only need a different/additional function when the data type of variable is a different one.
The header file provides three different functions for adding: char*, char, uint32
Examples to recognize the different types used:
char *stringlike = ".gmk";
char singlechar = 'c';
uint32 number = 1421;
You may need to read some general information about functions and data types to get a better understanding of it and their usage. I am not very good at explaining stuff like that.
-
Scachi - Brigadier General
- Posts: 3044
- Joined: Wed May 11, 2016 6:25 am
- Location: Germany
14 posts
• Page 1 of 2 • 1, 2
Return to GPC2 Script Programming
Who is online
Users browsing this forum: Baidu [Spider] and 109 guests