Shell - c Full Example

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Shell - c Full Example

Richmond Mathewson via use-livecode
The following shows you how to compile c code
from the terminal and run it in Livecode.

1.  Open a text editor and paste the following
c code;


#include<stdio.h>
#define MAX 100
char* getReverse(char const []);

int main(int argc, const char * argv[]) {
   
    char *rev;
    char const *str = argv[1];
   
    if (argc != 2)
    {
        printf("Enter 1 arguments and try again.\n");
        return 1;
    }
   
    rev = getReverse(str);
    printf("%s\n",rev);
   
    return 0;
}


char* getReverse(char const str[]){
    static int i=0;
    static char rev[MAX];
   
    if(*str){
        getReverse(str+1);
        rev[i++] = *str;
    }
   
    return rev;
}


Save the above code on the desktop as stringR.c


2.  Open the terminal and type the following;

cd ~/Desktop
gcc -o stringR stringR.c

The compiled code should be on your desktop now.

3.  Open Livecode and paste the following into a button.
Make sure you have a field as shown below.

on mouseUp
   set the defaultFolder to "~/Desktop"
   put quote & ".detrela erew sgod gniffins bmob yhw wenk snruB & stnaR oidar ORIK elttaeS ylno ... ...gof eht ni god eht gniklaw saw ehS" & quote into tFILE1
   put shell( "./stringR" && tFILE1) into pData
   put pData into fld id <YOURFIELD>
   beep 2
end mouseUp

enjoy,
JB


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Shell - c Full Example

Richmond Mathewson via use-livecode
On 06/29/2017 07:40 PM, JB via use-livecode wrote:

> char* getReverse(char const str[]){
>      static int i=0;
>      static char rev[MAX];
>      
>      if(*str){
>          getReverse(str+1);
>          rev[i++] = *str;
>      }
>      
>      return rev;
> }

Nice use of recursion, but note that you're susceptible to buffer
overflow if you don't limit str to MAX-1 chars. And rev should be
null-terminated.

--
  Mark Wieder
  [hidden email]


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Shell - c Full Example

Richmond Mathewson via use-livecode
Thanks for the code tip, Mark.

JB


> On Jun 29, 2017, at 8:20 PM, Mark Wieder via use-livecode <[hidden email]> wrote:
>
> On 06/29/2017 07:40 PM, JB via use-livecode wrote:
>
>> char* getReverse(char const str[]){
>>     static int i=0;
>>     static char rev[MAX];
>>          if(*str){
>>         getReverse(str+1);
>>         rev[i++] = *str;
>>     }
>>          return rev;
>> }
>
> Nice use of recursion, but note that you're susceptible to buffer overflow if you don't limit str to MAX-1 chars. And rev should be null-terminated.
>
> --
> Mark Wieder
> [hidden email]
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Shell - c Full Example

Richmond Mathewson via use-livecode
In reply to this post by Richmond Mathewson via use-livecode
Sow how would you do that?     static char rev[MAX -1]?        rev[i++] = *str & NULL?
>
>>

Bob S

>>
> On Jun 29, 2017, at 20:20 , Mark Wieder via use-livecode <[hidden email]> wrote:
>
>> char* getReverse(char const str[]){
>>     static int i=0;
>>          if(*str){
>>         getReverse(str+1);
>>     }
>>          return rev;
>> }
>
> Nice use of recursion, but note that you're susceptible to buffer overflow if you don't limit str to MAX-1 chars. And rev should be null-terminated.
>
> --
> Mark Wieder


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Shell - c Full Example

Richmond Mathewson via use-livecode
Hi
Is C# any different in terms of calls and compiling?

Thanks JB

Sean Cole
*Pi Digital Productions Ltd*

On 30 June 2017 at 15:35, Bob Sneidar via use-livecode <
[hidden email]> wrote:

> Sow how would you do that?     static char rev[MAX -1]?        rev[i++] =
> *str & NULL?
> >
> >>
>
> Bob S
> >>
> > On Jun 29, 2017, at 20:20 , Mark Wieder via use-livecode <
> [hidden email]> wrote:
> >
> >> char* getReverse(char const str[]){
> >>     static int i=0;
> >>          if(*str){
> >>         getReverse(str+1);
> >>     }
> >>          return rev;
> >> }
> >
> > Nice use of recursion, but note that you're susceptible to buffer
> overflow if you don't limit str to MAX-1 chars. And rev should be
> null-terminated.
> >
> > --
> > Mark Wieder
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Shell - c Full Example

Richmond Mathewson via use-livecode
You are welcome!

I have never used c#.

JB


> On Jul 6, 2017, at 9:40 AM, Sean Cole (Pi) via use-livecode <[hidden email]> wrote:
>
> Hi
> Is C# any different in terms of calls and compiling?
>
> Thanks JB
>
> Sean Cole
> *Pi Digital Productions Ltd*
>
> On 30 June 2017 at 15:35, Bob Sneidar via use-livecode <
> [hidden email]> wrote:
>
>> Sow how would you do that?     static char rev[MAX -1]?        rev[i++] =
>> *str & NULL?
>>>
>>>>
>>
>> Bob S
>>>>
>>> On Jun 29, 2017, at 20:20 , Mark Wieder via use-livecode <
>> [hidden email]> wrote:
>>>
>>>> char* getReverse(char const str[]){
>>>>    static int i=0;
>>>>         if(*str){
>>>>        getReverse(str+1);
>>>>    }
>>>>         return rev;
>>>> }
>>>
>>> Nice use of recursion, but note that you're susceptible to buffer
>> overflow if you don't limit str to MAX-1 chars. And rev should be
>> null-terminated.
>>>
>>> --
>>> Mark Wieder
>>
>>
>> _______________________________________________
>> use-livecode mailing list
>> [hidden email]
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Loading...