View Single Post
Old 03 August 2017, 17:31   #3
chocsplease
Registered User

 
Join Date: Dec 2016
Location: london
Posts: 157
Quote:
Originally Posted by Cylon View Post
Easy way is to use GimmeZeroZero (GZZ) flags on your window, which results in a different Rastport for the window's borders - the system (Intuition) will handle clipping at the edges, or,
you install your own clipping regions.
Third option:
you determine the pixel width of the text and cut the string accordingly.
Many thanks for the reply - I'll check out the gzz option. I have been working on the 3rd, and wrote a function to count the number of chars I was going to output in preparation. Thing is it doesn't work

This is the code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <proto/dos.h>   
#include <proto/exec.h>  



long count_chars(char *format, ...) /* variable number of args*/
{
    va_list valist;
    unsigned int i,longcount=0,spcfound=1,nosign=0,ok=0,ii=0,error=0;
    long count=0;
    long long number;
/*    char *format;*/
    char letter1,letter2,letter3;
    char* string;

    va_start(valist,format);

/*    if (num<1)/
    {
        error=-2;
        va_end(valist);
        return error;    
    }*/

    /*format=(char*)va_arg(valist,char);*/ /*get format string*/

    if (strlen(format)==0) /*if dud format string*/
    {
        va_end(valist); /*free memory*/
        return -3;    /*quit*/
    }

    printf("format is %d long\n",strlen(format));

    /*now parse format string, counting normal chars and calulating size of numbers*/

    for (i=0;i<strlen(format);i++)
    {

        printf("i=%d char=%c\n",i,(char)format[i]);

        if (CheckSignal (SIGBREAKF_CTRL_C))
        { 
            printf("*BREAK main loop*\n");
            break;
        }
                    

        letter1=(char)format[i];
        if (letter1 !='%')
        {
            if (letter1==' ')
                spcfound=1;

            if (spcfound==1)    /*normal character ie not in format% <something>*/
                count++;
        }
        else if ((i+1) <(strlen(format)))
        {
            letter2=(char)format[i+1];
            spcfound=0;        /*now we hunt for a space or %*/
    
            if (letter2=='%') /*want to output a percent sign*/
            {
                count++;    /*just add to count*/
                i++;        /*inc place*/
                spcfound=1;    /*now looking for a space*/
            }
            else if (letter2=='u')    /*signifies unsigned so set flag*/
            {
                nosign=1;
                i++;
            }
            
            else if (letter2=='l')    /*long integer so add to longcount*/
            {
                longcount++;
                i++;
            }

            else if ((letter2=='d') ||(letter2=='x'))    /*work out number of chars in dec or hex number*/
            {
                ok=0;
                i++;
                if (nosign)
                {
                    if (longcount==0)
                    {
                        ok=1;
                        number=va_arg(valist,unsigned int);
                    }
                    if (longcount==1)
                    {
                        ok=1;
                        number=va_arg(valist,unsigned long int);
                    }
                    if (longcount==2)
                    {
                        ok=1;
                        number=va_arg(valist,unsigned long long int);
                    }
                    if (longcount>2)
                    {
                        error=-1;    /*formatting error too many l's*/
                        break;
                    }
                
        
                    if (ok)        /* have something in number*/
                    {
                        while (number!=0)
                        {
                            printf("unsigned  number %d\n",number);
                            if (CheckSignal (SIGBREAKF_CTRL_C))
                            { 
                                printf("*BREAK unsigned loop*\n");
                                break;
                            }
    
                            if (letter2=='d')
                                number=number/10;
                            else
                                number=number/16;

                            count++;
                        }
                    }
                    longcount=0;    /*clear count of l's*/
                                
                }
                else     /*signed*/
                {
                    printf("signed longcount=%d\n",longcount);
                    if (longcount==0)
                    {
                        ok=1;
                        printf("int\n");
                        number=va_arg(valist, int);
                    }
                    if (longcount==1)
                    {
                        ok=1;
                        printf("long int\n");

                        number=(long long int)va_arg(valist, long int);
                    }
                    if (longcount==2)
                    {
                        ok=1;
                        printf("long long int\n");

                        number=(long long int)va_arg(valist, long long int);
                    }
                    if (longcount>2)
                    {
                        error=-1;    /*formatting error too many l's*/
                        break;
                    }
                
                    if(ok)
                    {
                        printf("number starts %d\n",number);
                        if (number<0)    /*negetive number so allow for sign*/
                            count++;
                        while (number!=0)
                        {
                            printf("signed number %d\n",number);

                            if (CheckSignal (SIGBREAKF_CTRL_C))
                            { 
                                printf("*BREAK signed loop*\n");
                                break;
                            }
                    
                            if (letter2=='d')
                                number=number/10;
                            else
                                number=number/16;
                            
                            count++;
                        }

                    }
                    longcount=0;    /*clear count of l's*/
                }
            }    /*end of if (letter1=='d')*/
            
            else if(letter2=='s') /*string*/
            {
                string =va_arg(valist,char*);
                count=count+strlen(string);
            }
        }
        else    /* % at end of string - dud format*/
        {
            printf("dud string\n");
            error=-1;
            break;
        }
    }    /* end for*/

    va_end(valist);    /*clear va memory*/

printf("error=%d\n",error);

    if (error!=0)
        return error;

    return count;
}

int main()
{
    int num1=-500;
    unsigned int num2 =45;
    long int num3 = -5087654;
    unsigned long int num4 = 3637363783;
    long long int num5= -82344826482342;
    unsigned long long int num6 =88268966344;

    long result=0;

    result=count_chars("This is a test %d",num1);

    printf("The number of chars is %d\n",result);
return 0;
}
What I am trying to do is parse a format string like "this is a number %d" in the 1st argument have the actual number, or numbers in subsequent arguments and spit out the total number of characters.

However the line -

number=va_arg(valist, int);

is always setting number to -1. I have tried casting as - number=(long long int)va_arg(valist, int); but this also returns -1.

I'm using this page - https://en.wikibooks.org/wiki/C%2B%2...nctions/va_arg

for my inspiration, but this is C++ and appears to be defining the vars used to pull the arguments out on the fly (eg -
int d = va_arg( argptr, int ); )

Once
again I am stuck. What really baffles me is the loop -

Code:
while (number!=0)                         
{ 
       printf("signed number %d\n",number);                              
       if (CheckSignal (SIGBREAKF_CTRL_C))                             
       {                                  
              printf("*BREAK signed loop*\n");                                 
             break;                             
       }                                                  
       if (letter2=='d')                                 
             number=number/10;                             
       else                                 
             number=number/16;                                                          
       count++;                         
}
Actually works and loops 3 times for the number -500. Even though number is printf-ing as -1 and changing the %d to %ld in my printfs has no effect it still spits out -1.

Can anyone see where I am going wrong?
chocsplease is offline  
 
Page generated in 0.07608 seconds with 10 queries