B4R Question inline c, string declaration

Mostez

Well-Known Member
Licensed User
Longtime User
hello,
in this code i want to call replace function from inline c code, but i get the error listed below, any ideas how to fix this?

thanks

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Private BC As ByteConverter
   
    Private Original As String
    Private SearchFor As String
    Private ReplaceWith As String
    Private Replaced As String
   
   
End Sub

Public Sub ReplaceString(sOriginal() As Byte, sSearchFor() As Byte, sReplaceWith() As Byte) As Byte()
    BC.ArrayCopy(sOriginal,Original)
    BC.ArrayCopy(sSearchFor,SearchFor)
    BC.ArrayCopy(sReplaceWith,ReplaceWith)
    RunNative("replace",Null)
    Return Replaced.GetBytes
End Sub

#If C
String myoriginal = b4r_str::_original;
void replace(B4R::Object* o) {
b4r_str::_replaced = myoriginal.replace(b4r_str::_searchfor,b4r_str::_replacewith);
}
#End If


B4R version: 1.80
Parsing code. (0.02s)
Compiling code. (0.05s)
Building project (0.05s)
Compiling & deploying Ino project (Arduino/Genuino Mega or Mega 2560 - COM35) Error
Loading configuration...
Initializing packages...
Preparing boards...
Verifying...
b4r_str.cpp:14: error: conversion from 'B4R::B4RString*' to non-scalar type 'String' requested
String myoriginal = b4r_str::_original;
^
sketch\b4r_str.cpp: In function 'void replace(B4R::Object*)':
sketch\b4r_str.cpp:16:82: warning: invalid conversion from 'B4R::B4RString*' to 'char' [-fpermissive]
b4r_str::_replaced = myoriginal.replace(b4r_str::_searchfor,b4r_str::_replacewith);
^
In file included from C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:231:0,
from sketch\B4RDefines.h:8,
from sketch\b4r_str.cpp:1:
C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/WString.h:182:7: note: initializing argument 1 of 'void String::replace(char, char)'
void replace(char find, char replace);
^
sketch\b4r_str.cpp:16:82: warning: invalid conversion from 'B4R::B4RString*' to 'char' [-fpermissive]
b4r_str::_replaced = myoriginal.replace(b4r_str::_searchfor,b4r_str::_replacewith);
^
In file included from C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:231:0,
from sketch\B4RDefines.h:8,
from sketch\b4r_str.cpp:1:
C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/WString.h:182:7: note: initializing argument 2 of 'void String::replace(char, char)'
void replace(char find, char replace);
^
b4r_str.cpp:16: error: void value not ignored as it ought to be
b4r_str::_replaced = myoriginal.replace(b4r_str::_searchfor,b4r_str::_replacewith);
^
exit status 1
 

Mostez

Well-Known Member
Licensed User
Longtime User
Yes Erel, I used your code and it works OK, but when I use it in loop, even in separate sub, it makes program hang. it seems that my code has stack problem again :(
Thanks so much for help
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
sure, here it's
B4X:
private Sub PrintLabel(ItemID As String,LabelFileName As String,PrintAsReceipt As Boolean) As String
    Dim Inf As String = GetItemInfo(ItemID)
    If Str.IsErr(Inf) Then Return Inf
  
    Dim InfoArray(10) As String 'split item information
    Dim ptr As Byte = 0
    For Each s() As Byte In BC.Split(Inf.GetBytes,Array As Byte(SYS_DATA_FILES_DELIMITION_CHR))
        If ptr > 9 Then Exit
        InfoArray(ptr) = BC.StringFromBytes(s)
        ptr = ptr + 1
    Next
  
    Dim RAF As RandomAccessFile  
    Dim Ia As String = InfoArray(4) 'is active
    If Ia = 0 Then Return ERR_INACTIVE_OR_DIABLED
    Dim iid () As Byte = InfoArray(0).GetBytes 'item id
    'Dim ibrc() As Byte = BC.trim(InfoArray(1).GetBytes) 'item barcode
    Dim itn () As Byte  = BC.trim(InfoArray(2).GetBytes) 'item name
    Dim ipr () As Byte = BC.Trim(InfoArray(3).GetBytes) 'item price
    Dim rawweight As String = NumberFormat(HX711.GetUnits(2),5,0)
    Dim weight As String = GetWeight(rawweight)'Str.JoinTwoString(GetWeight(2),SYS_UNITS_NAMES(UnitofMeasure - 1))
    Dim grsd As Double = weight * BC.StringFromBytes(ipr)
    Dim grs As String = NumberFormat(grsd,1,2)
    Dim strprice () As Byte  = Str.ReplaceString(NumberFormat(grs,3,2).GetBytes,".".GetBytes,"") 'remove dot from gross amount
  
    Dim ItemAndWeight (12) As Byte
    RAF.Initialize(ItemAndWeight,False)
    RAF.WriteBytes(NumberFormat(PosID,2,0),0,2,RAF.CurrentPosition)
    RAF.WriteBytes(iid,0,iid.Length,RAF.CurrentPosition)
    RAF.WriteBytes(rawweight.GetBytes,0,rawweight.Length,RAF.CurrentPosition)
  
    Dim ItemAndPrice (12) As Byte
    RAF.Initialize(ItemAndPrice,False)
    RAF.WriteBytes(NumberFormat(PosID,2,0),0,2,RAF.CurrentPosition)
    RAF.WriteBytes(iid,0,iid.Length,RAF.CurrentPosition)
    RAF.WriteBytes(strprice,0,strprice.Length,RAF.CurrentPosition)
  
    If PrintAsReceipt Then
        PrintItemReceipt(itn,weight,ipr,grs,ItemAndWeight,ItemAndPrice)
        Return ERR_NO_ERR
    End If

    Dim FullFileName As String = (GetLabelFileName(LabelFileName))
    Dim fz As String  = GetFileSize(FullFileName)
    If Str.IsErr(fz) Then Return fz
  
    SetSD(True)
    If Not (SD.OpenRead(FullFileName)) Then Return ERR_FILE_OPEN
    Dim tmp(1) As Byte
    Dim Original(80) As Byte
    Dim parm () As Byte
    Dim ArrayIndex As Byte = 0
  
    'RAF.Initialize(Buffer,False)
    Dim read As Int
    SD.Position = 0
    'For F = 1 To fz
    Dim counter As ULong
    Do While counter < fz
        'Log (counter)
        read =  SD.Stream.ReadBytes(tmp,0,1)
        'If read = 0 Then Return ERR_FILE_EOF
        Select tmp(0)
            Case 0x0D
'<<<< replace parameters call >>>>>>
                parm = ReplaceLabelParameters(Original,itn,weight,ipr,grs,ItemAndWeight,ItemAndPrice)
                '                Log (Original)
                '                Log(parm)
              
                LabelPrinter.Write(parm)
                ArrayIndex = 0
                For x = 0 To Original.Length - 1
                    Original(x) = 0
                Next
            Case 0x0A
  
            Case Else
                Original(ArrayIndex) = tmp(0)
                ArrayIndex = ArrayIndex + 1
        End Select
        counter = counter + 1
    Loop
    'Next
  
    SD.Close
    SetSD(False)
    Return ERR_NO_ERR
End Sub

B4X:
private Sub ReplaceLabelParameters(Original() As Byte,ItemName () As Byte,weight As String ,Price () As Byte,Amount As Double,ItemAndWeight() As Byte, ItemAndPrice () As Byte ) As Byte()
  
    Dim Replaced() As Byte
    If BC.IndexOf(Original,"oooo".GetBytes) > 0 Then
        Replaced = Str.ReplaceString(Original,"oooo".GetBytes,OwnerName) 'owner name field
    else If  BC.IndexOf(Original,"nnnn".GetBytes) > 0 Then
        Replaced = Str.ReplaceString(Original,"nnnn".GetBytes,ItemName) 'item name field
    else if BC.IndexOf(Original,"wwww".GetBytes) > 0 Then
        Replaced = Str.ReplaceString(Original,"wwww".GetBytes,weight.GetBytes) 'weight
    else If BC.IndexOf(Original, "pppp".GetBytes) > 0 Then
        Replaced = Str.ReplaceString(Original, "pppp".GetBytes,Price) ' item unit price
    else If BC.IndexOf(Original, "gggg".GetBytes) > 0 Then
        Replaced = Str.ReplaceString(Original, "gggg".GetBytes,NumberFormat(Amount,1,2)) 'gross amount
    else If BC.IndexOf(Original, "cccc".GetBytes) > 0 Then
        LabelCounter = LabelCounter + 1
        Replaced = Str.ReplaceString(Original, "cccc".GetBytes,NumberFormat(LabelCounter,1,0)) 'label counter
    else if BC.IndexOf(Original,"tttt".GetBytes) > 0 Then
        Replaced= Str.ReplaceString(Original,"tttt".GetBytes,"16:25".GetBytes) 'time, DS1302 not implemnted yet
    else if BC.IndexOf(Original,"dddd".GetBytes) > 0 Then
        Replaced= Str.ReplaceString(Original,"dddd".GetBytes,"06/06/17".GetBytes) 'date
    else if BC.IndexOf(Original, "888888888888".GetBytes) > 0 Then
        Replaced = Str.ReplaceString(Original, "888888888888".GetBytes,ItemAndWeight) 'item and weight barcode
    else if BC.IndexOf(Original, "999999999999".GetBytes) > 0 Then
        Replaced = Str.ReplaceString(Original, "999999999999".GetBytes,ItemAndPrice) 'item and price barcode
    Else
        Replaced = (Original) 'not parametric line, send it to printer as it is
    End If
    Return Replaced
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
Thank you so much Erel for this great and very effective solution
StackBufferUsage was 674~2220 in small label files and , 674~2641 in larger files. Now after using global store it is fixed to 590
What is the length of Original?
original length is 80 bytes

it works great, thanks again:)
 
Upvote 0
Top