danbaron
10-07-2011, 07:52
You can make your own string functions in C.
It's easy to do.
Below is an implementation of MID$ (without any error checking).
' code --------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define long unsigned long int
//-------------------------------------------------------------------------------------------------------
char* makestring(char buffer[])
{
char* dummy;
long stringlength = strlen(buffer);
dummy = malloc(stringlength + 1);
memcpy(dummy, buffer, stringlength + 1);
return dummy;
}
//-------------------------------------------------------------------------------------------------------
char* makeemptystring(long numchars)
{
char* dummy;
dummy = malloc(numchars);
return dummy;
}
//-------------------------------------------------------------------------------------------------------
char* mid(char* s, long start, long length)
{
long i;
char* dummy = makeemptystring(length + 1);
for(i = 0; i < length; i++) dummy[i] = s[start + i];
dummy[length] = '\0';
return dummy;
}
//-------------------------------------------------------------------------------------------------------
int main()
{
long maxchars = 1000;
char stringbuffer[maxchars];
char c;
char* s1;
char* s2;
strcpy(stringbuffer, "abcdefghijklmnopqrstuvwxyz");
s1 = makestring(stringbuffer);
s2 = mid(s1, 1, 24);
printf("s1 = %s\n", s1);
printf("s2 = %s\n", s2);
c = getchar();
return 0;
}
' output ------------------------------------------------------------------------------------------------------------
s1 = abcdefghijklmnopqrstuvwxyz
s2 = bcdefghijklmnopqrstuvwxy
Press any key to continue...
REDEBOLT
11-07-2011, 13:00
Hi Dan,
Your post above reminded me of BCX.
Here is the BASIC code:
dim a$
dim b$
a$ = "Basic is good -- C is better!"
b$ = mid$(a$,1,13)
a$ = mid$(a$,17,len(a$))
? b$
? a$
and here is the generated C code:
// *************************************************************
// Created with BCX -- The BASIC To C Translator (ver 5.12-08.08.22)
// BCX (c) 1999 - 2008 by Kevin Diggins
// *************************************************************
// Translated for compiling with a C Compiler
// *************************************************************
#include <windows.h> // Win32 Header File
#include <windowsx.h> // Win32 Header File
#include <commctrl.h> // Win32 Header File
#include <commdlg.h> // Win32 Header File
#include <mmsystem.h> // Win32 Header File
#include <shellapi.h> // Win32 Header File
#include <shlobj.h> // Win32 Header File
#include <richedit.h> // Win32 Header File
#include <wchar.h> // Win32 Header File
#include <objbase.h> // Win32 Header File
#include <ocidl.h> // Win32 Header File
#include <winuser.h> // Win32 Header File
#include <olectl.h> // Win32 Header File
#include <oaidl.h> // Win32 Header File
#include <ole2.h> // Win32 Header File
#include <oleauto.h> // Win32 Header File
#include <conio.h>
#include <direct.h>
#include <ctype.h>
#include <io.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <setjmp.h>
#include <time.h>
#include <stdarg.h>
#include <process.h>
// ***************************************************
// Compiler Defines
// ***************************************************
// C++
#if defined( __cplusplus )
#define overloaded
#define C_EXPORT EXTERN_C __declspec(dllexport)
#define C_IMPORT EXTERN_C __declspec(dllimport)
#else
#define C_EXPORT __declspec(dllexport)
#define C_IMPORT __declspec(dllimport)
#endif
// Open Watcom defs
#if defined( __WATCOM_CPLUSPLUS__ ) || defined( __TINYC__ )
#define atanl atan
#define sinl sin
#define cosl cos
#define tanl tan
#define asinl asin
#define acosl acos
#define log10l log10
#define logl log
#define _fcloseall fcloseall
#endif
// Borland C++ 5.5.1 defs - bcc32.exe
#if defined( __BCPLUSPLUS__ )
// ===== Borland Libraries ==========
#include <dos.h>
#pragma comment(lib,"import32.lib")
#pragma comment(lib,"cw32.lib")
// ==================================
#endif
// Microsoft VC++
#ifndef DECLSPEC_UUID
#if (_MSC_VER >= 1100) && defined ( __cplusplus )
#define DECLSPEC_UUID(x) __declspec(uuid(x))
#else
#define DECLSPEC_UUID(x)
#endif
#endif
#if !defined( __LCC__ )
// *************************************************
// Instruct Linker to Search Object/Import Libraries
// *************************************************
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"gdi32.lib")
#pragma comment(lib,"comctl32.lib")
#pragma comment(lib,"advapi32.lib")
#pragma comment(lib,"winspool.lib")
#pragma comment(lib,"shell32.lib")
#pragma comment(lib,"ole32.lib")
#pragma comment(lib,"oleaut32.lib")
#pragma comment(lib,"uuid.lib")
#pragma comment(lib,"odbc32.lib")
#pragma comment(lib,"odbccp32.lib")
#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"comdlg32.lib")
#pragma comment(lib,"imagehlp.lib")
#pragma comment(lib,"version.lib")
#else
#pragma lib <winspool.lib>
#pragma lib <shell32.lib>
#pragma lib <ole32.lib>
#pragma lib <oleaut32.lib>
#pragma lib <uuid.lib>
#pragma lib <odbc32.lib>
#pragma lib <odbccp32.lib>
#pragma lib <winmm.lib>
#pragma lib <imagehlp.lib>
#pragma lib <version.lib>
// *************************************************
// End of Object/Import Libraries To Search
// *************************************************
#endif
// *************************************************
// System Variables
// *************************************************
// *************************************************
// User Global Variables
// *************************************************
static char a[2048];
static char b[2048];
// *************************************************
// Standard Prototypes
// *************************************************
char* BCX_TmpStr(size_t);
char* mid (char*, int, int=-1);
// *************************************************
// User Global Initialized Arrays
// *************************************************
// *************************************************
// Main Program
// *************************************************
int main(int argc, char *argv[])
{
strcpy(a,"Basic is good -- C is better!");
strcpy(b,mid(a,1,13));
strcpy(a,mid(a,17,strlen(a)));
printf("%s\n",b);
printf("%s\n",a);
return 0; // End of main program
}
// *************************************************
// Runtime Functions
// *************************************************
char *BCX_TmpStr (size_t Bites)
{
static int StrCnt;
static char *StrFunc[2048];
StrCnt=(StrCnt + 1) & 2047;
if(StrFunc[StrCnt]) free (StrFunc[StrCnt]);
return StrFunc[StrCnt]=(char*)calloc(Bites+128,sizeof(char));
}
char *mid (char *S, int start, int length)
{
char *strtmp;
register int tmplen = strlen(S);
if(start>tmplen||start<1) return BCX_TmpStr(1);
if (length<0 || length>(tmplen-start)+1)
length = (tmplen-start)+1;
strtmp = BCX_TmpStr(length);
return (char*)memcpy(strtmp,&S[start-1],length*sizeof(char));
}
If you ignore the "boilerplate" code, the essential code begins under " Main Program."
Regards,
Bob
danbaron
11-07-2011, 19:38
Wow, he did a nice job, Bob.
Unlike me, he apparently really understands C.
:oops: :neutral: :twisted:
Dan