COCKOS
CONFEDERATED FORUMS
Cockos : REAPER : NINJAM : Forums
Forum Home : Register : FAQ : Members List : Search :
Old 12-07-2018, 02:40 AM   #1
Lex248
Human being with feelings
 
Join Date: Apr 2017
Location: Russia
Posts: 22
Default Text to array and save a path.

Hi, all. Two questions on a C++/WDL.

1)
I loaded the text file (char* buffer). How to make the numerical array of it? Special function?

text:
Code:
0.1
0.27
1.358
result:
Code:
ary[0] = 0.1
ary[1] = 0.27
ary[2] = 1.358
2)
Whether it is possible to save a path to the file in the preset manager (MakePreset)?

Thanks.
__________________
Sorry, I bad speak english.
---
My plug-ins http://nalexsoft.blogspot.ru

Last edited by Lex248; 12-07-2018 at 12:02 PM. Reason: corrected a lexical error
Lex248 is offline   Reply With Quote
Old 12-07-2018, 09:57 PM   #2
David S
Human being with feelings
 
Join Date: Jan 2017
Posts: 11
Default use chunks

Please read the following post
https://forum.cockos.com/showthread....th+to+filename
David S is offline   Reply With Quote
Old 12-08-2018, 08:32 AM   #3
Lex248
Human being with feelings
 
Join Date: Apr 2017
Location: Russia
Posts: 22
Default

Quote:
Originally Posted by David S View Post
OK, thanks, I will try.
__________________
Sorry, I bad speak english.
---
My plug-ins http://nalexsoft.blogspot.ru
Lex248 is offline   Reply With Quote
Old 12-08-2018, 08:51 AM   #4
Lex248
Human being with feelings
 
Join Date: Apr 2017
Location: Russia
Posts: 22
Default

And my version on the first question. It is tested on a Windows with array size to 20000. It is free and the WTFPL license.

Code:
#include <fileread.h>
#include <vector>

float mSign[20];
float mArray[100000];

...

WDL_FileRead infile(path);
if (infile.IsOpen()==true)
{
	std::vector<char> buffer(infile.GetSize()+1); // +1 to have space for the terminating zero
	infile.Read(buffer.data(),infile.GetSize());
	buffer[infile.GetSize()]='\0'; // put in the string terminating zero
	
	// text to numerical array - start
	int len = infile.GetSize();
	int i = 0;
	int k = 0;
	
	while (i < len)
	{
		float mSum = 0.0;
		float nSum = 0.0;
		int mInv = 1;
		
		int j = 0;
		int mStop = 0;
		while (mStop == 0)
		{
			if (buffer[i] == 48) mSign[j] = 0;
			if (buffer[i] == 49) mSign[j] = 1;
			if (buffer[i] == 50) mSign[j] = 2;
			if (buffer[i] == 51) mSign[j] = 3;
			if (buffer[i] == 52) mSign[j] = 4;
			if (buffer[i] == 53) mSign[j] = 5;
			if (buffer[i] == 54) mSign[j] = 6;
			if (buffer[i] == 55) mSign[j] = 7;
			if (buffer[i] == 56) mSign[j] = 8;
			if (buffer[i] == 57) mSign[j] = 9;
			if (buffer[i] == 45) // minus
			{
				mSign[j] = 0;
				mInv = -1;
			}
			if (buffer[i] == 46) mStop = 2; // dot
			if (buffer[i] == 13) mStop = 1; // enter
			if (i == len) mStop = 1; // end buffer
			i += 1;
			j += 1;
		}
		
		j -= 2;
		int mul = 1;
		while (j >= 0)
		{
			mSum = mSum + mSign[j] * mul;
			j -= 1;
			mul *= 10;
		}
		
		if (mStop == 2)
		{
			j = 0;
			mStop = 0;
			while (mStop == 0)
			{
				if (buffer[i] == 48) mSign[j] = 0;
				if (buffer[i] == 49) mSign[j] = 1;
				if (buffer[i] == 50) mSign[j] = 2;
				if (buffer[i] == 51) mSign[j] = 3;
				if (buffer[i] == 52) mSign[j] = 4;
				if (buffer[i] == 53) mSign[j] = 5;
				if (buffer[i] == 54) mSign[j] = 6;
				if (buffer[i] == 55) mSign[j] = 7;
				if (buffer[i] == 56) mSign[j] = 8;
				if (buffer[i] == 57) mSign[j] = 9;
				if (buffer[i] == 13) mStop = 1; // enter
				if (i == len) mStop = 1; // end buffer
				i += 1;
				j += 1;
			}
			
			int oldj = j-1;
			j = 0;
			mul = 10;
			while (j != oldj)
			{
				nSum = nSum + mSign[j] / mul;
				j += 1;
				mul *= 10;
			}
		}
		
		i += 1; // 10 compensation
		
		mArray[k] = (mSum + nSum) * mInv;
		k += 1;
	}
	// text to numerical array - end
	
}
__________________
Sorry, I bad speak english.
---
My plug-ins http://nalexsoft.blogspot.ru
Lex248 is offline   Reply With Quote
Old 12-12-2018, 12:58 AM   #5
Lex248
Human being with feelings
 
Join Date: Apr 2017
Location: Russia
Posts: 22
Default

I made saving a path to the file. "openFile" is my void with WDL_FileRead. To create blobs, set settings and calling DumpPresetBlob(filename), like in IPlugChunks.

Code:
  WDL_String filepath;
  IFileSelectorControl *mFileDialog;

...

  MakePresetFromBlob("Preset 1", "AAAAAAAAAA", 512);
  MakePresetFromBlob("Preset 2", "AAAAAAAAAA", 512);

  // File Dialog
  IRECT tmpRect0(136, 65, 176, 105);
  //mFileDialog = new IFileSelectorControl(this, tmpRect0, -1, &toggle2, kFileOpen, "C:\\","txt");
  mFileDialog = new IFileSelectorControl(this, tmpRect0, -1, &toggle2, kFileOpen, "C:\\","*");
  pGraphics->AttachControl(mFileDialog);

  RestorePreset(0);

...

bool MyEffect::SerializeState(ByteChunk* pChunk)
{
	TRACE;
	IMutexLock lock(this);

	mFileDialog->GetLastSelectedFileForPlug(&filepath);
	openFile(filepath.Get());

	int plugVersion = GetEffectVersion(false);
	pChunk->Put(&plugVersion);
	
	// for controls
    int i, n = kNumParams;
	double v = 0.0;
    for (i = 0; i < n; ++i)
    {
      v = GetParam(i)->Type();
      pChunk->Put(&v);
    }

	pChunk->PutStr(filepath.Get()); // for file path

	return IPlugBase::SerializeParams(pChunk);
}

int MyEffect::UnserializeState(ByteChunk* pChunk, int startPos)
{
  TRACE;
  IMutexLock lock(this);
  
  int plugVersionInState;
  startPos = pChunk->Get(&plugVersionInState, startPos);

  // for controls
  double v = 0.0;
  int i, n = kNumParams;
    for (i = 0; i < n; ++i)
    {
		startPos = pChunk->Get(&v, startPos);
	}
  
	startPos = pChunk->GetStr(&filepath, startPos); // for file path

	mFileDialog->SetLastSelectedFileFromPlug(filepath.Get());
	openFile(filepath.Get());

  return IPlugBase::UnserializeParams(pChunk, startPos);
}

Also added a line to controls (knob, switch):

Code:
  void OnMouseUp(int x, int y, IMouseMod* pMod)
  {
    //TODO: check this isn't going to cause problems... this will happen from the gui thread
    mPlug->ModifyCurrentPreset();
    mPlug->DirtyPTCompareState();
  }

And to void IFileSelectorControl:

Code:
...
    mPlug->GetGUI()->PromptForFile(&mFile, mFileAction, &mDir, mExtensions.Get());
    //TODO: check this isn't going to cause problems... this will happen from the gui thread
    mPlug->ModifyCurrentPreset();
    mPlug->DirtyPTCompareState();
...

And to resource.h:

Code:
#define PLUG_DOES_STATE_CHUNKS 1
__________________
Sorry, I bad speak english.
---
My plug-ins http://nalexsoft.blogspot.ru

Last edited by Lex248; 12-12-2018 at 10:36 AM. Reason: edited the message
Lex248 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 06:43 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.