Insmod string parameter with whitespace - module

hi I learn module these day. Today I write a simple module that would take string parameter when using insmod here is code snippet:
....
static char *city = "NULL";
MODULE_PARM(city, charp,0000);
....
when I type in command line
insmod modulename.ko city="newyork"
it's ok
but when I type
insmod modulename.ko city="new york"
there is error
insmod: error inserting 'modulename.ko': -1 Unknown symbol in module
do you know why?

Related

Using a default value for a function parameter which depends of other parameter

I'd like to create an script which takes an input file and optionally an output file. When you don't pass an output file, the script uses the same filename as the input but with the extension changed. I don't know how to write a default parameter which changes the extension.
#!/usr/bin/env raku
unit sub MAIN(
Str $input where *.IO.f, #= input file
Str $output = $input.IO.extension("txt"), #= output file
Bool :$copy, #= copy file
Bool :$move, #= move file
);
Unfortunately that doesn't work:
No such method 'IO' for invocant of type 'VMNull'
in block <unit> at ./copy.raku line 5
How can I do something like that?
error message is less than awesome but program not working is expected because you have in the signature
Str $output = $input.IO.extension("txt")
but the right hand side returns an IO::Path object with that extension but $output is typed to be a String. That is an error:
>>> my Str $s := "file.csv".IO.extension("txt")
Type check failed in binding; expected Str but got IO::Path (IO::Path.new("file.t...)
in block <unit> at <unknown file> line 1
>>> sub fun(Str $inp, Str $out = $inp.IO.extension("txt")) { }
&fun
>>> fun "file.csv"
Type check failed in binding to parameter '$out'; expected Str but got IO::Path (IO::Path.new("file.t...)
in sub fun at <unknown file> line 1
in block <unit> at <unknown file> line 1
Sometimes compiler detects incompatible default values:
>>> sub yes(Str $s = 3) { }
===SORRY!=== Error while compiling:
Default value '3' will never bind to a parameter of type Str
------> sub yes(Str $s = 3⏏) { }
expecting any of:
constraint
but what you have is far from a literal, so runtime detection.
To fix it, you can either
change to Str() $output = $inp.IO.extension("txt") where Str() means "accept Any object and then cast it to Str". So $output will end up being a string like "file.txt" available in MAIN.
similar alternative: Str $output = $inp.IO.extension("txt").Str but it's repetitive in Str.
change to IO::Path() $output = $inp.IO.extension("txt"). Similarly, this casts to whatever recieved to an IO::Path object, so, e.g., you'll have "file.txt".IO available in $output. If you do this, you might want to do the same for $input as well for consistency. Since IO::Path objects are idempotent to .IO (in eqv sense), no other part of the code needs changing.

Paste mode / multi-line-snippet in the kotlin REPL?

Given the skeleton of a function:
fun reformat(
str: String,
normalizeCase: Boolean = true,
upperCaseFirstLetter: Boolean = true,
divideByCamelHumps: Boolean = false,
wordSeparator: Char = ' ',
) {
println("In reformat")
}
I pasted this into the kotlin REPL and it complained - clearly due to not realizing it were being sent a multi-line snippet:
>>> fun reformat(
error: parameter name expected
fun reformat(
^
error: expecting comma or ')'
fun reformat(
^
error: expecting ')'
fun reformat(
^
>>> str: String,
error: expecting an element
str: String,
^
error: expecting an element
str: String,
^
etc ..
What is the equivalent of the :paste in the scala REPL?
It looks like the Kotlin REPL is intended to be used from the IDE. If you open it from within Intellij IDEA, it works like a charm and allows you multiline input. I don't see any options which allow you to do the same from the regular shell. So, in your case, you can either declare a function in one line or load the script from a file.
I found a project - apparently actively improved by the JetBrains kotlin developers themselves that addresses this and other issues: https://github.com/Kotlin/kotlin-interactive-shell . Here is their help
[0] :h
:quit or q quit the shell
:load or l <path> load file and evaluate
:type or t <expr> display the type of an expression without evaluating it
:list or ls list defined symbols
:help or h [command] print this summary or command-specific help
:paste or p enter paste mode
:syntax {on | off} syntax highlighting
:prompt [pattern] customize prompt
:set set configuration parameter
:conf list configuration parameters
:dependsOn <artifact coordinates> Load dependency from the current set of repositories
:repository <repository coordinates> Add repository for the artifacts lookup
:classpath or cp Show current script compilation classpath
Notice in particular :
[1] :p
// Entering paste mode (ctrl-D to finish)
That is precisely what I am looking for. It does work (though with ugly JDK9 warnings):
[1] :p
// Entering paste mode (ctrl-D to finish)
fun reformat(
str: String,
normalizeCase: Boolean = true,
upperCaseFirstLetter: Boolean = true,
divideByCamelHumps: Boolean = false,
wordSeparator: Char = ' ',
) {
println("In reformat")
}
// Exiting paste mode, now interpreting.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jetbrains.kotlin.com.intellij.util.ReflectionUtil (file:/Users/steve/git/kotlin-interactive-shell/ki-shell/target/ki-shell-0.4-SNAPSHOT-shaded.jar) to method java.util.ResourceBundle.setParent(java.util.ResourceBundle)
WARNING: Please consider reporting this to the maintainers of org.jetbrains.kotlin.com.intellij.util.ReflectionUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[2] reformat()
ERROR No value passed for parameter 'str' (Line_3.kts:1:10)
[3] reformat("abc")
In reformat

Kotlin: "Type inference failed"

Does someone know what goes I'm doing wrong here?
// ...
val alphabet = ('a'..'z').toList()
val str = "testing"
val arr = str.split("")
for (char in arr) {
var i = alphabet.indexOf(char) // Throws Error!
// ...
}
The indexOf-method results in
"Error:(10, 26) Kotlin: Type inference failed. The value of the type parameter T should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly."
I have tried to "var i: Int = alphabet.indexOf(char)" and "alphabet.indexOf(char) as Int". The same result ...
What's the issue here?
I believe that your problem is that you think that the variable char contains a Char value. If that were the case, then it would make sense for indexOf to be accepting a Char value and then finding the location of that character in a list of characters (List<Char>) on which the method is being called.
But str.split() is returning a List<String>, not a List<Char>, and so char is a String. So you are asking the indexOf method to tell you the location of a String in a list of characters (List<Char>). That doesn't make sense. That's the basis of the problem. Exactly how that translates to the error you're getting from the Kotlin compiler, I'm not sure.
To make it clear, this line produces the same error:
var i = alphabet.indexOf("c") // Throws Error!
but the compiler likes this line just fine:
var i = alphabet.indexOf('c') // No Error!!!

MessageBoxW cannot convert

I am using wxWidgets 2.9.4 in Visual Studio 2012 and I keep getting these two error messages:
Error 1 error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR'
IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
My code is:
#ifdef _WIN32
std::string msg;
StringFromFormatV(&msg, format, args);
retval = IDYES == MessageBox(0, msg.c_str(), "ERROR! Continue?", MB_ICONQUESTION | MB_YESNO);
You are compiling your project using multi-byte characters as default. You can change that in your project's properties, or you can use msg.wc_str(), or even enforce the use of MessageBoxA instead of using the macro MessageBox.

C++ CLI Class problem

----- hello, world 2.cpp -----
// Hello, World 2.cpp : main project file.
#include "stdafx.h"
#include "hello.h"
#include <string>
using namespace System;
using namespace std;
int main(array<System::String ^> ^args)
{
hello hi = new hello("Bob", "Blacksmith");
Console::WriteLine(L"Hello, " + hi.getName + "!");
return 0;
}
----- hello.h -----
#include <string>
using namespace std;
#ifndef HELLO_H
#define HELLO_H
class hello
{
private:
string _fname;
string _lname;
//hello() { } // private default constructor
public:
hello(string fname, string lname);
void SetName(string fname, string lname);
string GetName();
};
#endif
----- hello.cpp -----
#include "stdafx.h"
#include "hello.h"
#include <string>
using namespace std;
hello::hello(string fname, string lname)
{
SetName(fname, lname);
}
void hello::SetName(string fname, string lname)
{
_fname = fname;
_lname = lname;
}
string hello::getName()
{
return _fname + _lname;
}
----- The errors -----
------ Build started: Project: Hello, World 2, Configuration: Debug Win32 ------
Hello, World 2.cpp
Hello, World 2.cpp(12): error C2440: 'initializing' : cannot convert from 'hello *' to 'hello'
No constructor could take the source type, or constructor overload resolution was ambiguous
Hello, World 2.cpp(13): error C2039: 'getName' : is not a member of 'hello'
\documents\visual studio 2010\projects\cpp\hello, world 2\hello, world 2\hello.h(8) : see declaration of 'hello'
hello.cpp
hello.cpp(17): error C2039: 'getName' : is not a member of 'hello'
\documents\visual studio 2010\projects\cpp\hello, world 2\hello, world 2\hello.h(8) : see declaration of 'hello'
hello.cpp(19): error C2065: '_fname' : undeclared identifier
hello.cpp(19): error C2065: '_lname' : undeclared identifier
Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Error messages tell you exactly where and what the problems are, though they can be a bit intimidating at first. Maybe I can help demystify them a bit:
Hello, World 2.cpp(12): error C2440: 'initializing' : cannot convert from 'hello *' to 'hello'
This means that on line 12 in Hello, World 2.cpp, you're trying to put a pointer to hello (the return from new) inside hi which is not a pointer type. Since you don't need a dynamically allocated object here, just drop the new.
In situations where you do need a dynamically allocated object, you would change the hi variable to hello * and add a corresponding delete.
Hello, World 2.cpp(13): error C2039: 'getName' : is not a member of 'hello'
C++ is case sensitive. In one file you have GetName, in the other you have getName. Pick one.
hello.cpp(19): error C2065: '_fname' : undeclared identifier
hello.cpp(19): error C2065: '_lname' : undeclared identifier
Line 19 of hello.cpp is the definition of the lower case getName. Since getName wasn't declared in the class (see previous error), the compiler has no idea what _fname or _lname are. These errors will go away once the original problems are solved.
Edit
See #Sergey's answer for some other more general observations of things to fix.
The new keyword creates a pointer - if you do it this way, 'hi' should be declared as hello*, or you should reqrite the declaration as:
hello hi(...);
The second error is just due to case sensitivity (getName, GetName).
hello hi = new hello("William", "Dyson");
Must be
hello* hi = new hello(...);
Or
hello hi("William", "Dyson"); ;
Console::WriteLine(L"Hello, " + hi.getName + "!");
Must be
Console::WriteLine(L"Hello, " + hi.getName() + "!");
There may be other failures but i have to go now.
There are several errors.
File names with whitespaces. Not critical, but can lead to problems
Console::WriteLine(L"Hello, " + hi.getName + "!");
this should be something like this:
string s("Hello, ");
s += hi->getName();
s += "!"
Console::WriteLine(s);
delete objects allocated with new: delete hi;
string hello::getName() should be string hello::GetName()
Never use the use namespace ... in header files
Include other files within guard block
If you allocate something with new you have to call delete after you don't need it anymore.
In case you want to use the garbage collector the clean up for you you have to declare the class as ref class hello and then instantiate it with:
hello^ hi = gcnew hello(...);
Some comments that undoubtedly will be posted before I'm finished writing this:
Put you public members first
the namespace System implies Managed C++, if you don't know what that is and think you are using plain C++, please read about printf and std::cout.
The line
hello hi = new hello("William", "Dyson");
should read
hello hi("William", "Dyson"); or hello* hi = new hello("William", "Dyson");
The first creates an object on the stack (which will be automatically destructed as it goes out of scope. The second creates a pointer to an object on the heap, which you will have to delete before the pointer goes out of scope (after you've finished with it)
about hello::getName(): you have typos with regards to capitalization (getname vs getName) and it should be declared like this (in header and accordingly in the source file):
const string getName() const;
The first const is optional, but I like it that way, the last one lets you call this function from a const hello object, and tells the reader that the function does not modify the object.
You should pass string by reference (here is a correcter constructor):
hello( const string &fname, const string& lname);
Sources and headers should have names without spaces (or special characters) in them, this will be problematic for UNIX<->Windows if you're not careful.
That's all I can see right now.